const HtmlWebPackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
const bodyParser = require('body-parser');
module.exports = {
entry: './src/index.js',
output: {
filename: 'assets/[name].[hash:6].js',
publicPath: '/dist',
path: path.join(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.(sass|scss|css)$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: {
loader: 'url-loader',
options: {
limit: 10240,
name: 'assets/[name].[hash:6].[ext]',
}
}
},
{
test: /\.(eot|ttf|svg|woff2?)$/,
use: ['url-loader']
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebPackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: true
}),
new MiniCssExtractPlugin({
filename: 'assets/[name].css',
chunkFilename: 'assets/[id].css'
})
],
devServer: {
port: 10001,
host: '0.0.0.0',
hot: true,
before(app){
app.post('/loader/list', bodyParser.json(), (req, res, next)=>{
console.log('req...', req);
console.log('查询参数...', req.query);
console.log('body体...', req.body)
res.json({
code: 1,
msg: '请求成功',
data: ['node-sass', 'sass-loader', 'css-loader', 'file-loader', 'url-loader', 'style-loader']
})
})
}
},
resolve: {
alias: {
'@': path.join(__dirname, 'src')
}
},
optimization: {
minimizer: [
new TerserJSPlugin({}),
new OptimizeCSSAssetsPlugin({})
],
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
priority: 10
},
utils: {
chunks: 'initial',
name: 'utils',
minChunks: 2,
minSize: 0
}
}
}
}
}