我不认为这是重复的问题。我只将@ polymer / polymer作为依赖项安装并导入到我的供应商捆绑包中(没有@ polymer / paper-input)。我正在使用v3.0.5,甚至在依赖项树中也没有看到Iron-meta(通过npm列表),并且我的堆栈跟踪看起来不同-它指向polymer / lib / elements / dom-module.js
dom-module.js:178未捕获的DOMException:无法在以下对象上执行“定义” 'CustomElementRegistry':此名称已与此名称一起使用 注册表
跟踪指向此行customElements.define('dom-module', DomModule);
在@polymer/polymer/lib/elements/dom-module.js?:178:16
我正在尝试建立一个基本的Polymer 3项目。我正在将Webpack与babel-loader一起使用以编译到es5。因为我要编译到es5,所以根据webcomponentsjs repo上的指令,我将custom-elements-es5-adapter.js
和webcomponents-bundle.js
一起包括在内。这些脚本只是简单地从node_modules复制到输出目录,并且脚本标记包含在html头中。
对于我的组件代码,我正在为每个聚合物组件创建单独的js块,以及为共享导入(当前仅包含Polymer)创建一个单独的块。编译和代码拆分工作没有错误,并且将结果块添加到了body结束标记之前的html中。
Webpack SplitChunks插件将@polymer/polymer
导入拉到单独的块中,因此它们仅被包含一次。
目标是将所有必需的供应商代码放入一个通用脚本中,并且每个组件中的很小一部分都可以有选择地包含进来。
在我当前的设置下,这些块似乎已正确创建。
从理论上讲,根据我到目前为止所读的内容,这应该可以工作,但是我完全陷在了这个错误上。
如果我将组件文件捆绑在一起,则一切正常。
以下是我的一个非常简单的组件文件的示例:
import { html, PolymerElement } from '@polymer/polymer';
export default class MyButton extends PolymerElement {
constructor() {
super();
}
static get template() {
return html`
<slot></slot>
`;
}
static get properties() {
return { }
}
}
customElements.define('my-button', MyButton);
以下是我为此概念验证创建的Webpack配置:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const SRC_PATH = path.resolve(__dirname, './src');
const DIST_PATH = path.resolve(__dirname, './dist');
module.exports = {
entry: {
'my-button': `${SRC_PATH}/js/components/my-button.js`,
'my-tabs': `${SRC_PATH}/js/components/my-tabs.js`
},
output: {
filename: 'js/[name].js',
path: DIST_PATH
},
resolve: {
extensions: ['.js']
},
module: {
rules: [{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: [[
'env',
{
targets: {
browsers: [
'last 2 versions',
'ie > 10'
]
},
debug: true
}
]]
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: `${SRC_PATH}/index.html`,
filename: 'index.html',
inject: 'head'
}),
new CopyWebpackPlugin([{
from: './node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js',
to: 'js/vendor',
toType: 'dir'
}, {
from: './node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js',
to: 'js/vendor',
toType: 'dir'
}, {
from: './node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js',
to: 'js/vendor',
toType: 'dir'
}]),
new BundleAnalyzerPlugin()
],
optimization: {
splitChunks: {
cacheGroups: {
default: false,
commons: {
name: 'my-common',
chunks: 'all',
minChunks: 2
}
}
},
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
ie8: false,
safari10: false,
compress: {
warnings: false,
drop_console: true
},
output: {
ascii_only: true,
beautify: false
}
}
})
]
},
devServer: {
contentBase: DIST_PATH,
compress: false,
overlay: {
errors: true
},
port: 8080,
host: '127.0.0.1'
}
};
这是html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title>polymer-3-sandbox</title>
<meta name="description" content="A polymer 3 sandbox">
<link rel="manifest" href="/manifest.json">
<script src="/js/vendor/webcomponents-bundle.js"></script>
<script src="/js/vendor/custom-elements-es5-adapter.js"></script>
<script type="text/javascript" src="js/my-common.js"></script>
<script type="text/javascript" src="js/my-button.js"></script>
<script type="text/javascript" src="js/my-tabs.js"></script>
</head>
<body>
<p>
<my-button>Learn More</my-button>
</p>
</body>
</html>
答案 0 :(得分:-2)
我们已使用嵌套的聚合物移除脚本解决了此问题,请检查original github issue here。
技巧是通过将以下内容添加到package.json文件中来使npm运行preinstall.sh脚本:
"scripts": {
"preinstall": "../preinstall.sh"
}
然后运行以下脚本,该脚本无需脚本即可两次安装npm来解决安装错误:
#!/bin/bash
# Author: Flatmax developers
# Date : 2018 10 17
# License : free
npm i --ignore-scripts || true
if [ `ls node_modules/ | wc -l` -eq "0" ]; then
zenity --error --text="ERROR : cb() never called\nrm node_modules and pacakge-lock.json and try again"
fi
npm i --ignore-scripts || true
if [ `ls node_modules/ | wc -l` -eq "0" ]; then
zenity --error --text="ERROR : cb() never called\nrm node_modules and pacakge-lock.json and try again"
fi
. ../fixNestings.sh
最后,实际的嵌套删除脚本如下:
#!/bin/bash
# Author: Flatmax developers
# Date : 2018 10 17
# License : free
# The following function will remove nested directories, where $1 exists like so
# node_modules/.*/node_modules/$1
# @param $1 the module name to remove nestings of
function rmNestedMod(){
name=$1
paths=`find -L node_modules -name $1 | sed "s|^node_modules/||;s|/\$name$||" | grep node_modules`
for p in $paths; do
echo rm -rf node_modules/$p/$name
rm -rf node_modules/$p/$name
done
}
# remove all nested polymer namespaces
namespaces=`ls node_modules/@polymer/`
for n in $namespaces; do
rmNestedMod "$n"
done