第三课
webpack-dev-server
- 安装
yarn add webpack-dev-server -D
- 启动
npx webpack-dev-server
- 配置 webpack
module.exports = {
devServer: {
port: 3000,
progress: true,
contentBase: './build',
compress: true,
},
entry: './src/index.js',
...
}
- 修改 package.json文件
"script": {
"dev": "webpack-dev-server"
}
webpack-plugin
- 安装插件
yarn add html-webpack-plugin -D
- 在
webpack.config.js
中引入和使用插件
let HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
...,
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
minify: {
removeAttributeQuotes: true,
collapseWhitespace: true
},
hash: true
})
],
output: {
...,
filename: 'index.[hash:8].js'
}
}
最终webpack.config.js
let path = require('path');
let HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
devServer: {
port: 3000,
progress: true,
contentBase: './build',
compress: true
},
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.[hash:8].js',
path: path.resolve(__dirname, 'build')
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
minify: {
removeAttributeQuotes: true,
collapseWhitespace: true
},
hash: true
})
]
}