我正在测试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 Repo中找到代码,以便快速查看:https://github.com/janrembold/webcomponent-examples/tree/master/06-frameworks/04-polymer
这个错误是否因为Polymer3的早期状态而发生?或者我在这里想念什么?
顺便说一句,我不想使用聚合物-cli,因为我们的项目有一些特殊要求。
提前感谢您的帮助!
答案 0 :(得分:2)
我有同样的问题:
我使用babel-plugin-syntax-dynamic-import
为支持本机自定义元素(safari和chrome)的浏览器解决了这个问题。
并使用babel-plugin-dynamic-import-webpack
进行转换。
没有转发:
plugins: ['syntax-dynamic-import']
transpiling:
plugins: ['dynamic-import-webpack']