npm
每次我们打包或者启动服务的时候,都需要在命令行输入一长串命令,这对我们开发体验也是极其不友好的,那么我们怎么去优化一下呢?
配置启动命令
我们在package.json中配置如下命令:
"scripts": {
"dev": "webpack serve -c ./config/webpack.config.dev.js", // 开发环境
"build": "webpack -c ./config/webpack.config.prod.js" // 生产环境
},
运行
然后我们在控制台输入 npm run dev 来启动服务,发现服务器可以正常启动
优化
有时候我们会发现在生产环境下进行打包会出现很多警告,告诉我们有些包超过预期,如果我们不希望出现这个警告可以做如下配置:
performance: {
hints: false
}
提取公共配置
我们虽然已经将 开发环境 和 生产环境 配置拆分开来了,但是我们发现两份配置中有很多重复代码,我们来提取一下公共的配置,并改造一下开发环境和生产环境。
公共配置(webpack.config.common.js)
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 引入 html-webpack-plugin 插件
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); // 引入 mini-css-extract-plugin 插件
module.exports = {
entry: {
index: './src/index.js', // 配置 index.js 打包入口
another: './src/another.js', // 配置 another.js 打包入口
}, // 入口文件路径
output: { // 出口文件
path: path.resolve(__dirname, '../dist'), // 出口文件路径
clean: true, // 生成新文件的同时是否删除旧文件
},
plugins: [
new HtmlWebpackPlugin({ // 实例化 HtmlWebpackPlugin,它将帮我们生成一个 dist/index.html 文件
template: './index.html', // 指定模板
filename: 'app.html', // 生成的文件名
inject: 'body' // script标签的生成位置
}),
new MiniCssExtractPlugin({
filename: 'styles/[contenthash].css'
})
],
module: {
rules: [ // 配置资源模块
{
test: /\.css$/, // 利用正则匹配以 .css 结尾的文件
use: [MiniCssExtractPlugin.loader, 'css-loader'] // 使用的 loader
},
{
test: /\.js$/, // 匹配 js 文件
exclude: /node_modules/, // 去除编译 node_modules 包
use: {
loader: 'babel-loader', // 使用 loader
options: {
presets: ['@babel/preset-env'], // 添加预设
plugins: [
[
'@babel/plugin-transform-runtime'
]
]
}
}
}
]
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
},
},
}
}
}
开发环境(webpack.config.dev.js)
module.exports = {
output: { // 出口文件
filename: './script/[name].js', // 出口文件名
},
mode: 'development', // 设置模式 'none' | 'development' | 'production'
devtool: 'inline-source-map',
devServer: {
static: './dist' // 路径
},
}
生产环境(webpack.config.prod.js)
const CssMinizerPlugin = require('css-minimizer-webpack-plugin'); // 引入 css-minimizer-webpack-plugin 插件
const TerserPlugin = require('terser-webpack-plugin') // 引入 terser-webpack-plugin 插件
module.exports = {
output: { // 出口文件
filename: './script/[name].[contenthash].js', // 出口文件名
publicPath: 'http://localhost:8080/'
},
mode: 'production', // 设置模式 'none' | 'development' | 'production'
optimization: {
minimizer: [
new CssMinizerPlugin(),
new TerserPlugin()
],
},
performance: {
hints: false
}
}
合并配置文件(webpack-merge)
我们将配置文件拆分为三分,需要让他们之间产生联系,这个工作我们可以借助 webpack-merge 来完成
安装
npm install webpack-merge -D
配置
我们在 config文件 下新建一个 webpack.config.js 文件,配置如下:
const { merge } = require('webpack-merge')
const commonConfig = require('./webpack.config.common')
const prodConfig = require('./webpack.config.prod')
const devConfig = require('./webpack.config.dev')
module.exports = (env) => {
if (env.production) {
return merge(commonConfig, prodConfig)
} else if (env.development) {
return merge(commonConfig, devConfig)
}
return new Error('No matching configuration was found')
}
然后我们改造一下我们的 package.json 如下:
"scripts": {
"dev": "webpack serve -c ./config/webpack.config.js --env development",
"build": "webpack -c ./config/webpack.config.js --env production"
},
运行
分别执行 npm run dev 和 npm run build,效果和我们预期的一样,说明我们合并配置文件生效了