现在如果我们想要打包一个生产环境的代码就得需要:
1、更改mode: "production"
2、更改devtool: "cheap-module-source-map"
3、删除Tree Shaking配置

  1. module.exports = {
  2. //
  3. optimization: {
  4. usedExports: true
  5. }
  6. }

很繁琐有没有?当我们要在DevelomentProduction模式进行区分打包怎么才能简化呢?

分解配置文件

package.json文件中新增一个命令,使用--config告诉Webpack不同环境的配置文件。

  1. {
  2. "name": "chapter01",
  3. "sideEffects": false,
  4. "version": "1.0.0",
  5. "description": "",
  6. "main": "index.js",
  7. "scripts": {
  8. "dev": "webpack serve --config webpack.dev.js",
  9. "build": "webpack --config webpack.prod.js"
  10. },
  11. "author": "",
  12. "license": "ISC",
  13. "devDependencies": {
  14. // ...
  15. },
  16. "dependencies": {
  17. // ...
  18. }
  19. }

新建开发环境的配置文件:

  1. const path = require("path");
  2. const HtmlWebpackPlugin = require("html-webpack-plugin");
  3. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  4. module.exports = {
  5. mode: "development",
  6. devtool: "eval-cheap-module-source-map",
  7. entry: "./src/index.js",
  8. output: {
  9. filename: "[hash].js",
  10. path: path.resolve(__dirname, "dist"),
  11. },
  12. devServer: {
  13. contentBase: "./dist",
  14. open: true,
  15. hot: true, // 让 webpack-dev-server 开启 module-replacement 的功能
  16. hotOnly: true, // 即便css样式没有生效也不让浏览器自动刷新页面
  17. },
  18. optimization: {
  19. usedExports: true
  20. },
  21. module: {
  22. rules: [{
  23. test: /\.jpg|.jpeg|.png$/,
  24. use: {
  25. loader: "url-loader",
  26. options: {
  27. limit: 54000,
  28. name: "[name].[ext]",
  29. outputPath: "/images",
  30. }
  31. }
  32. }, {
  33. test: /\.scss$/,
  34. use: ["style-loader", {
  35. loader: "css-loader",
  36. options: {
  37. importLoaders: 2,
  38. modules: true
  39. }
  40. }, "sass-loader", "postcss-loader"]
  41. }, {
  42. test: /\.css$/,
  43. use: ["style-loader", "css-loader", "postcss-loader"]
  44. }, {
  45. test: /\.m?js$/,
  46. exclude: /node_modules/, // 排除node_modules下的代码
  47. use: {
  48. loader: "babel-loader"
  49. }
  50. }]
  51. },
  52. plugins: [
  53. new HtmlWebpackPlugin({
  54. template: "./src/index.html"
  55. }),
  56. new CleanWebpackPlugin(),
  57. ]
  58. }

新建生产环境的配置文件:

  1. const path = require("path");
  2. const HtmlWebpackPlugin = require("html-webpack-plugin");
  3. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  4. module.exports = {
  5. mode: "production",
  6. devtool: "cheap-module-source-map",
  7. entry: "./src/index.js",
  8. output: {
  9. filename: "[hash].js",
  10. path: path.resolve(__dirname, "dist"),
  11. },
  12. module: {
  13. rules: [{
  14. test: /\.jpg|.jpeg|.png$/,
  15. use: {
  16. loader: "url-loader",
  17. options: {
  18. limit: 54000,
  19. name: "[name].[ext]",
  20. outputPath: "/images",
  21. }
  22. }
  23. }, {
  24. test: /\.scss$/,
  25. use: ["style-loader", {
  26. loader: "css-loader",
  27. options: {
  28. importLoaders: 2,
  29. modules: true
  30. }
  31. }, "sass-loader", "postcss-loader"]
  32. }, {
  33. test: /\.css$/,
  34. use: ["style-loader", "css-loader", "postcss-loader"]
  35. }, {
  36. test: /\.m?js$/,
  37. exclude: /node_modules/, // 排除node_modules下的代码
  38. use: {
  39. loader: "babel-loader"
  40. }
  41. }]
  42. },
  43. plugins: [
  44. new HtmlWebpackPlugin({
  45. template: "./src/index.html"
  46. }),
  47. new CleanWebpackPlugin(),
  48. ],
  49. }

这样运行npm run dev就是开发环境且执行开发环境的配置文件,运行npm run prod就是生产环境且执行生产环境的配置文件啦。

优化配置文件

现在虽然可以区分开发环境和生产环境了,但是我们会发现webpack.dev.jswebpack.prod.js有大量相同冗余的代码,我们应该把公共的配置代码抽出来实现公用。

我们需要用到webpack-merge帮助我们进行合并Webpack配置文件的内容,安装:

  1. $ npm install webpack-merge -D

新建webpack.common.js里面存放webpack.dev.jswebpack.prod.js公共的配置:

  1. const path = require("path");
  2. const HtmlWebpackPlugin = require("html-webpack-plugin");
  3. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  4. module.exports = {
  5. entry: "./src/index.js",
  6. output: {
  7. filename: "[hash].js",
  8. path: path.resolve(__dirname, "dist"),
  9. },
  10. // optimization 优化
  11. optimization: {
  12. usedExports: true
  13. },
  14. module: {
  15. rules: [{
  16. test: /\.jpg|.jpeg|.png$/,
  17. use: {
  18. loader: "url-loader",
  19. options: {
  20. limit: 54000,
  21. name: "[name].[ext]",
  22. outputPath: "/images",
  23. }
  24. }
  25. }, {
  26. test: /\.scss$/,
  27. use: ["style-loader", {
  28. loader: "css-loader",
  29. options: {
  30. importLoaders: 2,
  31. modules: true
  32. }
  33. }, "sass-loader", "postcss-loader"]
  34. }, {
  35. test: /\.css$/,
  36. use: ["style-loader", "css-loader", "postcss-loader"]
  37. }, {
  38. test: /\.m?js$/,
  39. exclude: /node_modules/, // 排除node_modules下的代码
  40. use: {
  41. loader: "babel-loader"
  42. }
  43. }]
  44. },
  45. plugins: [
  46. // 自动引入打包好的js文件
  47. new HtmlWebpackPlugin({
  48. template: "./src/index.html"
  49. }),
  50. // 清除dist文件夹
  51. new CleanWebpackPlugin(),
  52. ]
  53. }

删除webpack.dev.js冗余的代码,使用webpack-merge进行合并:

  1. const { merge } = require("webpack-merge");
  2. const commonConfig = require("./webpack.common.js");
  3. const devConfig = {
  4. mode: "development",
  5. devtool: "eval-cheap-module-source-map",
  6. devServer: {
  7. contentBase: "./dist",
  8. open: true,
  9. hot: true, // 让 webpack-dev-server 开启 module-replacement 的功能
  10. hotOnly: true, // 即便css样式没有生效也不让浏览器自动刷新页面
  11. }
  12. }
  13. module.exports = merge(commonConfig, devConfig);

和上面一样,删除冗余的代码,使用webpack-merge进行合并:

  1. const { merge } = require("webpack-merge");
  2. const commonConfig = require("./webpack.common.js");
  3. const prodConfig = module.exports = {
  4. mode: "production",
  5. devtool: "cheap-module-source-map",
  6. entry: "./src/index.js",
  7. }
  8. module.exports = merge(commonConfig, prodConfig);

到此完美解决。