我遵循了https://material.io/develop/web/docs/getting-started/上的指南,但最终结果仍然有效,但是创建自定义元素以添加到混合中却没有。
my-button.js
import MDCRipple from '@material/ripple';
class MyButton extends HtmlElement {
static get tag() { return 'my-button'; }
}
customElements.define(MyButton.tag, MyButton);
但是webpack开发服务器错误:
未捕获的TypeError:无法解析模块说明符“ @ material / ripple”。相对引用必须以“ /”、“./”或“ ../”开头。
更改为相对路径只会导致MDCRipple内部导入发生错误。
我很确定webpack.config.js
可以解决此问题,但这对我来说是新手,为my-button.js添加条目没有帮助。
webpack.config.js
const autoprefixer = require('autoprefixer');
module.exports = {
entry: ['./app.scss', './app.js'],
output: {
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options: {
name: 'bundle.css',
},
},
{loader: 'extract-loader'},
{loader: 'css-loader'},
{loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer()],
},
},
{
loader: 'sass-loader',
options: {
includePaths: ['./node_modules'],
},
}
],
},
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015'],
plugins: ['transform-object-assign']
},
}
],
},
};
如果有问题,可以通过(index.html)包含按钮
<script type="module" src="my-button.js"></script>
目录结构:
node_modules/
my-button.js
index.html
package.json
webpack.config.js
package.json
devDependencies": {
"@webcomponents/webcomponentsjs": "^2.1.3",
"autoprefixer": "^9.2.1",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^1.0.0",
"extract-loader": "^3.0.0",
"file-loader": "^2.0.0",
"glob": "^7.1.3",
"lit-html": "^0.12.0",
"material-components-web": "^0.40.1",
"node-sass": "^4.9.4",
"postcss-loader": "^3.0.0",
"sass-loader": "^7.1.0",
"webpack": "^3.12.0",
"webpack-dev-server": "^2.11.3"
}