我的src文件夹内容如下:
index.html
index.css
index.less
index.js
接下来配置webpack.dev.config.js
const path=require('path');
const HtmlWebpackPlugin=require('html-webpack-plugin');
const {
CleanWebpackPlugin}=require('clean-webpack-plugin');//清理上次记录
const MiniCssExtractPlugin=require('mini-css-extract-plugin');//抽离css
const dev_dir='dev_folder';
module.exports={
mode:'development',
entry:{
index:'./src/index.js'
},
module:{
rules:[
{
test:/\.css$/,
use:[MiniCssExtractPlugin.loader,'css-loader']
},
{
test:/\.less$/,
use:[MiniCssExtractPlugin.loader,'css-loader','less-loader']
},
{
test:/\.(jpg|png|svg)$/,
use:'file-loader'
}
]
},
output:{
filename:'[name]_dev.js',
path:path.resolve(__dirname,dev_dir)
},
plugins:[
new MiniCssExtractPlugin(),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template:'./src/index.html'
})
]
}
const HtmlWebpackPlugin=require('html-webpack-plugin');
const MiniCssExtractPlugin=require('mini-css-extract-plugin');
module.exports={
entry:{
index:'./src/index.js'
},
module:{
rules:[
{
test:/\.css$/,
use:[MiniCssExtractPlugin.loader,'css-loader']
},
{
test:/\.less$/,
use:[MiniCssExtractPlugin.loader,'css-loader','less-loader']
},
{
test:/\.(jpg|png|svg)$/,
use:'file-loader'
}
]
},
plugins:[
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin({
template:'./src/index.html'
})
]
}
const path=require('path');
const {
CleanWebpackPlugin}=require('clean-webpack-plugin');
const {
merge}=require('webpack-merge');
const commonconfig=require('./common.config');
const dev_dir='dev_folder';
module.exports=merge(commonconfig,{
mode:'development',
output:{
filename:'[name]_dev.js',
path:path.resolve(__dirname,dev_dir)
},
plugins:[
new CleanWebpackPlugin(),
]
})
const path=require('path');
const {
CleanWebpackPlugin}=require('clean-webpack-plugin');
const {
merge}=require('webpack-merge');
const commonconfig=require('./common.config');
const dev_dir='prod_folder';
module.exports=merge(commonconfig,{
mode:'production',
output:{
filename:'[name]_prod.js',
path:path.resolve(__dirname,dev_dir)
},
plugins:[
new CleanWebpackPlugin(),
]
})
index.html
总结:配置dev—>抽取common—>合并(webpackMerge)—>copy到prod稍作修改。