说明,记录在操作过程中遇到的问题
1.前提条件:已安装node和npm
2.安装组件
npm i material-components-web
3.初始化生成package.json文件
npm init
3.更新json文件
{
"scripts": {
"start": "webpack-dev-server"
}
}
4.安装依赖
官网提供安装方法,但是安装node-sass会报错,解决办法一个一个安装即可
npm install --save-dev webpack webpack-cli webpack-dev-server css-loader sass-loader node-sass extract-loader file-loader
node-sass问题解决办法如下
npm install -g mirror-config-china --registry=http://registry.npm.taobao.org
npm install node-sass
5.实例一
index.html
Hello World
app.scss
body {
color: blue;
}
webpack.config.js,配置后实现的效果是打包app.scss生成bundle.css
module.exports = [{
entry: './app.scss',
output: {
// This is necessary for webpack to compile
// But we never use style-bundle.js
filename: 'style-bundle.js',
},
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options: {
name: 'bundle.css',
},
},
{ loader: 'extract-loader' },
{ loader: 'css-loader' },
{ loader: 'sass-loader' },
]
}
]
},
}];
测试
npm start
6.实例二
app.scss
@import "@material/button/mdc-button";
.foo-button {
@include mdc-button-ink-color(teal);
@include mdc-states(teal);
}
webpack.config.js改变{ loader: ‘sass-loader’ }
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: ['./node_modules']
}
}
}
安装依赖
npm install --save-dev autoprefixer postcss-loader
html中添加
webpack.config.js顶部添加
const autoprefixer = require('autoprefixer');
webpack.config.js内容添加
{ loader: 'extract-loader' },
{ loader: 'css-loader' },
{
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer()]
}
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: ['./node_modules']
}
}
},
测试
npm start
7.实例三
安装依赖
npm install --save-dev @babel/core babel-loader @babel/preset-env
html
app.js
console.log('hello world');
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: {
sassOptions: {
includePaths: ['./node_modules'],
}
},
}
],
},
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['@babel/preset-env'],
},
}
],
},
};
测试
npm start
8.实例4
app.js
import {MDCRipple} from '@material/ripple/index';
const ripple = new MDCRipple(document.querySelector('.foo-button'));
测试
npm start
9.生成dist文件,包括css,js,html
package.json文件修改
"scripts": {
"build": "webpack",
"start": "webpack-dev-server"
}
执行打包
npm run build
dist文件就包括js,css,可放在各种框架中
https://github.com/material-components/material-components-web/blob/master/docs/getting-started.md
https://material-components.github.io/material-components-web-catalog/#/
https://google.github.io/material-design-icons/
https://material.io/resources/icons/?style=outline
https://codepen.io/ningqing123/pen/MWwWvGW