HtmlWebpackPlugin
插件,可以把打包后的 CSS 或者 JS 文件引用直接注入到 HTML 模板中,这样就不用每次手动修改文件引用了。
安装
npm install --save-dev html-webpack-plugin
const path = require('path');
//一、导入插件包
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const autoprefixer = require('autoprefixer');
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
//二、增加一个hash参数
filename: 'main.[hash].js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader'
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: loader => [autoprefixer({ browsers: ['> 0.15% in CN'] })]
}
},
{
loader: 'sass-loader'
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name][hash].css',
chunkFilename: '[id][hash].css'
}),
//三、输出注入文件的一些指定内容
new HtmlWebpackPlugin({
title: '我是title', // 默认值:Webpack App
filename: 'main.html', // 默认值: 'index.html' //输出文件名
template: path.resolve(__dirname, 'src/index.html'), //模板地址
minify: {
collapseWhitespace: true, //去除空白
removeComments: true, //删除注释评论
removeAttributeQuotes: true // 移除属性的引号 //id="div" => id =div
}
})
],
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({})
]
}
};
最终生成的部分效果图
网友评论