Webpack中文:
    环境变量 | webpack 中文文档

    想要消除webpack.config.js在开发环境和生产环境之间的差异,你可能需要环境变量(environment variable)。
    Webpack命令行环境配置的--env参数,可以允许你传入任意数量的环境变量。而在webpack.config.js中可以访问到这些环境变量。例如:--env production--env NODE_ENV=local

    配置build脚本命令的环境变量:

    1. "scripts": {
    2. "dev": "webpack serve --config ./build/webpack.common.js",
    3. "build": "webpack --env production --config ./build/webpack.common.js"
    4. },

    引入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. const { merge } = require("webpack-merge");
    5. const webpack = require("webpack");
    6. const devConfig = require("./webpack.dev");
    7. const prodConfig = require("./webpack.prod");
    8. const commonConfig = {
    9. entry: {
    10. main: "./src/index.js",
    11. },
    12. output: {
    13. path: path.resolve(__dirname, "../dist"),
    14. },
    15. module: {
    16. rules: [{
    17. test: /\.jpg|.jpeg|.png$/,
    18. use: {
    19. loader: "url-loader",
    20. options: {
    21. limit: 54000,
    22. name: "[name].[ext]",
    23. outputPath: "/images",
    24. }
    25. }
    26. }, {
    27. test: /\.m?js$/,
    28. exclude: /node_modules/, // 排除node_modules下的代码
    29. use: {
    30. loader: "babel-loader"
    31. }
    32. }]
    33. },
    34. optimization: {
    35. // TreeShaking 的配置
    36. usedExports: true,
    37. // SplittingCode 的配置
    38. splitChunks: {
    39. // 分割引入代码库的方式,默认为 async 异步,可选 all:同步和异步
    40. chunks: 'all',
    41. cacheGroups: {
    42. vendors: {
    43. test: /[\\/]node_modules[\\/]/,
    44. priority: -10,
    45. name: "chunk"
    46. }
    47. }
    48. },
    49. },
    50. plugins: [
    51. new HtmlWebpackPlugin({
    52. template: "./src/index.html"
    53. }),
    54. new CleanWebpackPlugin({
    55. cleanOnceBeforeBuildPatterns: [
    56. path.resolve(__dirname, '../dist')
    57. ],
    58. }),
    59. // 在其他模块中发现使用了 $ 将自动引入 jquery,且赋值给 $
    60. new webpack.ProvidePlugin({
    61. $: "jquery"
    62. })
    63. ],
    64. }
    65. // 使用 env 对象进行判断
    66. module.exports = (env) => {
    67. if (env && env.production) {
    68. return merge(commonConfig, prodConfig)
    69. } else {
    70. return merge(commonConfig, devConfig)
    71. }
    72. }

    导出dev环境的配置文件:

    1. const devConfig = {
    2. mode: "development",
    3. devtool: "eval-cheap-module-source-map",
    4. output: {
    5. filename: "[name].js",
    6. },
    7. devServer: {
    8. contentBase: "./dist",
    9. open: true,
    10. hot: true, // 让 webpack-dev-server 开启 module-replacement 的功能
    11. hotOnly: true, // 即便css样式没有生效也不让浏览器自动刷新页面
    12. },
    13. module: {
    14. rules: [{
    15. test: /\.scss$/,
    16. use: ["style-loader", {
    17. loader: "css-loader",
    18. options: {
    19. importLoaders: 2,
    20. modules: true
    21. }
    22. }, "sass-loader", "postcss-loader"]
    23. }, {
    24. test: /\.css$/,
    25. use: ["style-loader", "css-loader", "postcss-loader"]
    26. }]
    27. }
    28. }
    29. module.exports = devConfig;

    导出prod环境的配置文件:

    1. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
    2. const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
    3. const prodConfig = {
    4. mode: "production",
    5. devtool: "cheap-module-source-map",
    6. output: {
    7. filename: "[name].[contenthash].js",
    8. },
    9. optimization: {
    10. // CSS代码压缩
    11. minimizer: [
    12. new CssMinimizerPlugin(),
    13. ],
    14. },
    15. module: {
    16. rules: [{
    17. test: /\.scss$/,
    18. use: [MiniCssExtractPlugin.loader, {
    19. loader: "css-loader",
    20. options: {
    21. importLoaders: 2,
    22. modules: true
    23. }
    24. }, "sass-loader", "postcss-loader"]
    25. }, {
    26. test: /\.css$/,
    27. use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"]
    28. }]
    29. },
    30. plugins: [new MiniCssExtractPlugin({
    31. filename: "[name].[hash].css"
    32. })]
    33. }
    34. module.exports = prodConfig;