3

我正在测试 Polymer 3.0 预览代码,并希望设置一个简单的项目,使用 Webpack 和 Babel 将其转换回 ES5。

编译工作正常。我将 custom-elements-es5-adapter.js 添加到我的演示页面,但捆绑的代码执行时出现以下错误:

custom-component.js:13 Uncaught TypeError: Class constructor PolymerElement cannot be invoked without 'new'
    at HTMLElement.CustomComponent (custom-component.js:13)
    at new j (custom-elements-es5-adapter.js:4)
    at CustomElementRegistry.value (custom-elements-es5-adapter.js:4)
    at Object.defineProperty.value (custom-component.js:23)
    at __webpack_require__ (bootstrap 6c5b87648fe743e36ebc:19)
    at window.JSCompiler_renameProperty (bootstrap 6c5b87648fe743e36ebc:62)
    at bootstrap 6c5b87648fe743e36ebc:62

ES6 代码取自这里:https ://www.polymer-project.org/blog/2017-08-23-hands-on-30-preview

import {Element as PolymerElement} from '@polymer/polymer/polymer-element';

export class CustomComponent extends PolymerElement {
    static get template() {
        return `<div>This is my [[name]] app.</div>`
    }

    constructor() {
        super();
        this.name = '3.0 preview';
    }

    static get properties() {
        name: {
            Type: String
        }
    }
}

customElements.define('custom-component', CustomComponent);

webpack.config.js

const {resolve} = require('path');

module.exports = {
    context: resolve(__dirname, 'src'),
    entry: {
        "custom-component": './custom-component.js'
    },
    output: {
        filename: '[name].js',
        path: resolve(__dirname, 'dist'),
    },
    resolve: {
        extensions: ['.js']
    },
    devtool: 'source-map',
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            }
        ]
    }
};

您可以在我的 Github 存储库中找到代码以进行快速查看:https ://github.com/janrembold/webcomponent-examples/tree/master/06-frameworks/04-polymer

是不是因为 Polymer3 的早期状态才出现这个错误?还是我在这里错过了什么?

顺便说一句,我不想​​使用polymer-cli,因为我们的项目有一些特殊要求。

提前感谢您的帮助!

4

1 回答 1

2

我遇到了同样的问题:我通过使用babel-plugin-syntax-dynamic-import支持本机自定义元素(safari 和 chrome)的浏览器解决了这个问题。并babel-plugin-dynamic-import-webpack用于转译。

没有转译: plugins: ['syntax-dynamic-import']

转译: plugins: ['dynamic-import-webpack']

于 2018-03-09T12:58:05.903 回答