现在如果我们想要打包一个生产环境的代码就得需要:
1、更改mode: "production"
2、更改devtool: "cheap-module-source-map"
3、删除Tree Shaking
配置
module.exports = {
//
optimization: {
usedExports: true
}
}
很繁琐有没有?当我们要在Develoment
和Production
模式进行区分打包怎么才能简化呢?
分解配置文件
在package.json
文件中新增一个命令,使用--config
告诉Webpack
不同环境的配置文件。
{
"name": "chapter01",
"sideEffects": false,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack serve --config webpack.dev.js",
"build": "webpack --config webpack.prod.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
// ...
},
"dependencies": {
// ...
}
}
新建开发环境的配置文件:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
mode: "development",
devtool: "eval-cheap-module-source-map",
entry: "./src/index.js",
output: {
filename: "[hash].js",
path: path.resolve(__dirname, "dist"),
},
devServer: {
contentBase: "./dist",
open: true,
hot: true, // 让 webpack-dev-server 开启 module-replacement 的功能
hotOnly: true, // 即便css样式没有生效也不让浏览器自动刷新页面
},
optimization: {
usedExports: true
},
module: {
rules: [{
test: /\.jpg|.jpeg|.png$/,
use: {
loader: "url-loader",
options: {
limit: 54000,
name: "[name].[ext]",
outputPath: "/images",
}
}
}, {
test: /\.scss$/,
use: ["style-loader", {
loader: "css-loader",
options: {
importLoaders: 2,
modules: true
}
}, "sass-loader", "postcss-loader"]
}, {
test: /\.css$/,
use: ["style-loader", "css-loader", "postcss-loader"]
}, {
test: /\.m?js$/,
exclude: /node_modules/, // 排除node_modules下的代码
use: {
loader: "babel-loader"
}
}]
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html"
}),
new CleanWebpackPlugin(),
]
}
新建生产环境的配置文件:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
mode: "production",
devtool: "cheap-module-source-map",
entry: "./src/index.js",
output: {
filename: "[hash].js",
path: path.resolve(__dirname, "dist"),
},
module: {
rules: [{
test: /\.jpg|.jpeg|.png$/,
use: {
loader: "url-loader",
options: {
limit: 54000,
name: "[name].[ext]",
outputPath: "/images",
}
}
}, {
test: /\.scss$/,
use: ["style-loader", {
loader: "css-loader",
options: {
importLoaders: 2,
modules: true
}
}, "sass-loader", "postcss-loader"]
}, {
test: /\.css$/,
use: ["style-loader", "css-loader", "postcss-loader"]
}, {
test: /\.m?js$/,
exclude: /node_modules/, // 排除node_modules下的代码
use: {
loader: "babel-loader"
}
}]
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html"
}),
new CleanWebpackPlugin(),
],
}
这样运行npm run dev
就是开发环境且执行开发环境的配置文件,运行npm run prod
就是生产环境且执行生产环境的配置文件啦。
优化配置文件
现在虽然可以区分开发环境和生产环境了,但是我们会发现webpack.dev.js
和webpack.prod.js
有大量相同冗余的代码,我们应该把公共的配置代码抽出来实现公用。
我们需要用到webpack-merge
帮助我们进行合并Webpack
配置文件的内容,安装:
$ npm install webpack-merge -D
新建webpack.common.js
里面存放webpack.dev.js
和webpack.prod.js
公共的配置:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
entry: "./src/index.js",
output: {
filename: "[hash].js",
path: path.resolve(__dirname, "dist"),
},
// optimization 优化
optimization: {
usedExports: true
},
module: {
rules: [{
test: /\.jpg|.jpeg|.png$/,
use: {
loader: "url-loader",
options: {
limit: 54000,
name: "[name].[ext]",
outputPath: "/images",
}
}
}, {
test: /\.scss$/,
use: ["style-loader", {
loader: "css-loader",
options: {
importLoaders: 2,
modules: true
}
}, "sass-loader", "postcss-loader"]
}, {
test: /\.css$/,
use: ["style-loader", "css-loader", "postcss-loader"]
}, {
test: /\.m?js$/,
exclude: /node_modules/, // 排除node_modules下的代码
use: {
loader: "babel-loader"
}
}]
},
plugins: [
// 自动引入打包好的js文件
new HtmlWebpackPlugin({
template: "./src/index.html"
}),
// 清除dist文件夹
new CleanWebpackPlugin(),
]
}
删除webpack.dev.js
冗余的代码,使用webpack-merge
进行合并:
const { merge } = require("webpack-merge");
const commonConfig = require("./webpack.common.js");
const devConfig = {
mode: "development",
devtool: "eval-cheap-module-source-map",
devServer: {
contentBase: "./dist",
open: true,
hot: true, // 让 webpack-dev-server 开启 module-replacement 的功能
hotOnly: true, // 即便css样式没有生效也不让浏览器自动刷新页面
}
}
module.exports = merge(commonConfig, devConfig);
和上面一样,删除冗余的代码,使用webpack-merge
进行合并:
const { merge } = require("webpack-merge");
const commonConfig = require("./webpack.common.js");
const prodConfig = module.exports = {
mode: "production",
devtool: "cheap-module-source-map",
entry: "./src/index.js",
}
module.exports = merge(commonConfig, prodConfig);
到此完美解决。