webpack優(yōu)化

webpack.config 按照production和dev分開配置,
wepback.base.config.js

npm install webpack-merge -D

const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = {
entry: {
  index: "./src/index.js",
},
output: {
  path: path.resolve(__dirname, "./dist"),
  filename: "[name]-[hash:6].js",
},
module: {
  rules: [
    {
      test: /\.(png|jpe?g|gif)$/,
      use: {
        loader: "url-loader",
        options: {
          name: "[name].[ext]",
          outputPath: "images",
          limit: 1024 * 3, //3kb
        },
      },
    },
    {
      test: /\.(eot|woff|woff2)$/,
      use: "file-loader",
    },
    {
      test: /\.js$/,
      use: {
        loader: "babel-loader",
      },
    },
  ],
},

plugins: [new CleanWebpackPlugin()],
};


webpack.dev.config.js

//webpack內(nèi)置插件
const { HotModuleReplacementPlugin } = require("webpack");
const htmlWebpackPlugin = require("html-webpack-plugin");

const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base.config.js");

const devConfig = {
  mode: "development",
  module: {
    rules: [
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "postcss-loader", "less-loader"],
      },
    ],
  },
  devtool: "inline-source-map",
  devServer: {
    contentBase: "./dist",
    open: true,
    hot: true,
    hotOnly: true,
    port: 8081,
    proxy: {
      "/api": {
        target: "http://localhost:9092",
      },
    },
  },
  plugins: [
    new htmlWebpackPlugin({
      template: "./src/index.html",
      filename: "index.html",
    }),
    new HotModuleReplacementPlugin(),
  ],
};
// module.exports = (env) => {
//   //
//   if(porcess.env.NODE_ENV==='env'){
//     return merge(baseConfig,devConfig)
//   }else{
//     return merge(baseConfig,prodConfig)
//   }
// };
module.exports = merge(baseConfig, devConfig);

webpack.prod.config.js

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const htmlWebpackPlugin = require("html-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const path = require("path");
const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base.config.js");

const PurifyCSS = require("purifycss-webpack");
const glob = require("glob-all");

const prodConfig = {
  mode: "production",
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              publicPath: "../",
            },
          },
          "css-loader",
          "postcss-loader",
          "less-loader",
        ],
      },
    ],
  },
  optimization: {
    usedExports: true,
  },
  plugins: [
    new htmlWebpackPlugin({
      template: "./src/index.html",
      filename: "index.html",
      minify: {
        // 壓縮HTML文件
        removeComments: true, // 移除HTML中的注釋
        collapseWhitespace: true, // 刪除空白符與換行符
        minifyCSS: true, // 壓縮內(nèi)聯(lián)css
      },
    }),
    new MiniCssExtractPlugin({
      filename: "css/[name]-[contenthash:6].css",
    }),
    new OptimizeCSSAssetsPlugin({
      cssProcessor: require("cssnano"), // 這里制定了引擎,不指定默認(rèn)也是 cssnano
    }),
    new PurifyCSS({
      paths: glob.sync([
        // 要做 CSS Tree Shaking 的路徑文件
        path.resolve(__dirname, "./src/*.html"), // 請注意,我們同樣需要對 html 文件進(jìn)行 tree shaking
        path.resolve(__dirname, "./src/*.js"),
      ]),
    }),
  ],
};

module.exports = merge(baseConfig, prodConfig);

附加配置文件
postcss.config.js

module.exports = {
  plugins: [
    require("autoprefixer")({
      overrideBrowserslist: ["last 2 versions", ">1%"],
    }),
    // require("cssnano"),
  ],
};
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容