前言:

我们面对生产环境和开发环境时,都需要不同的配置来满足我们的需求,因此,我们不能单单的再一个文件上改来改去,我们应该把这两个拆分开来,并提取出公共部分来便于我们的配置。

  1. const path = require("path")
  2. const HtmlWebpackPulgin = require("html-webpack-plugin")
  3. const MiniCssExtractPlugin = require("mini-css-extract-plugin")
  4. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
  5. const TerserPlugin = require("terser-webpack-plugin");
  6. module.exports = (env) => {
  7. console.log(env);
  8. return {
  9. // 通过 env 来区分 生产环境 和 开发环境
  10. mode: env.production ? 'production' : 'development',
  11. entry: {
  12. index: './src/index.js',
  13. another: './src/another-module.js',
  14. },
  15. output: {
  16. filename: 'scripts/[name].[contenthash].js',
  17. path: path.resolve(__dirname, './dist'),
  18. clean: true,
  19. assetModuleFilename: 'images/[contenthash][ext]',
  20. publicPath: "http://localhost:8080/"
  21. },
  22. devtool: 'inline-source-map',
  23. plugins: [
  24. new HtmlWebpackPulgin({
  25. template: './index.html',
  26. filename: 'app.html',
  27. inject: 'body'
  28. }),
  29. new MiniCssExtractPlugin({
  30. filename: 'style/[contenthash].css'
  31. }),
  32. new CssMinimizerPlugin()
  33. ],
  34. devServer: {
  35. static: './dist',
  36. hot: true,
  37. },
  38. module: {
  39. rules: [
  40. {
  41. test: /\.js$/,
  42. exclude: /(node_modules|bower_components)/,
  43. use: {
  44. loader: 'babel-loader',
  45. options: {
  46. presets: ['@babel/preset-env'],
  47. plugins: ['@babel/plugin-transform-runtime']
  48. }
  49. }
  50. },
  51. {
  52. test: /\.jpg$/i,
  53. type: 'asset/resource',
  54. generator: {
  55. filename: 'images/[contenthash][ext]'
  56. }
  57. },
  58. ]
  59. },
  60. optimization: {
  61. minimize: true,
  62. minimizer: [
  63. new CssMinimizerPlugin(),
  64. new TerserPlugin()
  65. ],
  66. runtimeChunk: 'single',
  67. splitChunks: {
  68. cacheGroups: {
  69. vendor: {
  70. test: /[\\/]node_modules[\\/]/,
  71. name: 'vendors',
  72. chunks: 'all',
  73. },
  74. },
  75. },
  76. }
  77. }
  78. }

1.提取webpack的公共配置部分

我们先把 两种环境中 相同的部分提取出来

  1. const path = require("path")
  2. const HtmlWebpackPulgin = require("html-webpack-plugin")
  3. const MiniCssExtractPlugin = require("mini-css-extract-plugin")
  4. module.exports = {
  5. entry: {
  6. index: './src/index.js',
  7. another: './src/another-module.js',
  8. },
  9. output: {
  10. path: path.resolve(__dirname, '../dist'),
  11. clean: true,
  12. assetModuleFilename: 'images/[contenthash][ext]',
  13. },
  14. plugins: [
  15. new HtmlWebpackPulgin({
  16. template: './index.html',
  17. filename: 'app.html',
  18. inject: 'body'
  19. }),
  20. new MiniCssExtractPlugin({
  21. filename: 'style/[contenthash].css'
  22. }),
  23. ],
  24. module: {
  25. rules: [
  26. {
  27. test: /\.js$/,
  28. exclude: /(node_modules|bower_components)/,
  29. use: {
  30. loader: 'babel-loader',
  31. options: {
  32. presets: ['@babel/preset-env'],
  33. plugins: ['@babel/plugin-transform-runtime']
  34. }
  35. }
  36. },
  37. {
  38. test: /\.jpg$/,
  39. type: 'asset/resource',
  40. generator: {
  41. filename: 'images/[contenthash][ext]'
  42. }
  43. },
  44. ]
  45. },
  46. optimization: {
  47. splitChunks: {
  48. cacheGroups: {
  49. vendor: {
  50. test: /[\\/]node_modules[\\/]/,
  51. name: 'vendors',
  52. chunks: 'all',
  53. },
  54. },
  55. },
  56. }
  57. }

2.编写development环境

  1. module.exports = {
  2. mode: 'development',
  3. output: {
  4. filename: 'scripts/[name].js',
  5. },
  6. devtool: 'inline-source-map',
  7. devServer: {
  8. static: './dist',
  9. hot: true,
  10. },
  11. }

3.编写production环境

  1. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
  2. const TerserPlugin = require("terser-webpack-plugin");
  3. module.exports = {
  4. mode: 'production',
  5. output: {
  6. filename: 'scripts/[name].[contenthash].js',
  7. publicPath: "http://localhost:8080/"
  8. },
  9. optimization: {
  10. minimize: true,
  11. minimizer: [
  12. new CssMinimizerPlugin(),
  13. new TerserPlugin()
  14. ],
  15. runtimeChunk: 'single',
  16. },
  17. // 配置如何展示性能提示。
  18. // 例如,如果一个资源超过 250kb,webpack 会对此输出一个警告来通知你。
  19. // performance.hints:关闭可以取消提示
  20. performance: {
  21. hints: false
  22. }
  23. }

4.使用merge合并两个环境

  1. const { merge } = require('webpack-merge')
  2. const commonConfig = require('./webpack.config.common')
  3. const productionConfig = require('./webpack.config.prod')
  4. const developmentConfig = require('./webpack.config.dev')
  5. module.exports = (env) => {
  6. switch (true) {
  7. case env.development:
  8. return merge(commonConfig, developmentConfig)
  9. case env.production:
  10. return merge(commonConfig, productionConfig)
  11. default:
  12. return new Error('No matching configuration was found')
  13. }
  14. }

5.使用npm脚本快速启动

  1. "scripts": {
  2. "start": "npx webpack serve --open -c ./config/webpack.config.js --env development",
  3. "build": "npx webpack -c ./config/webpack.config.js --env production"
  4. },

输入 npm run start
image.png
输入 npm run build
image.png

都运行成功,拆分环境成功