Kaynağa Gözat

👌 打包优化

晓晓晓晓丶vv 4 yıl önce
ebeveyn
işleme
0a2574b062
2 değiştirilmiş dosya ile 23 ekleme ve 21 silme
  1. 22 18
      prod.config.js
  2. 1 3
      vue.config.js

+ 22 - 18
prod.config.js

@@ -1,3 +1,5 @@
+const webpack = require("webpack");
+const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
 const isProd = process.env.NODE_ENV === "production";
 
 // * 避免打包项
@@ -110,29 +112,31 @@ const assetsGzip = (config) => {
 };
 
 // * 代码压缩
-const codeUglify = (config) => {
-  config
-    .plugin("uglifyjs-webpack-plugin")
-    .use(require("uglifyjs-webpack-plugin"), [
-      {
-        uglifyOptions: {
-          //生产环境自动删除console
-          compress: {
-            drop_debugger: true,
-            drop_console: false,
-            pure_funcs: ["console.log"],
-          },
-        },
-        sourceMap: false,
-        parallel: true,
-      },
-    ]);
+const codeUglifyConfig = {
+  uglifyOptions: {
+    //生产环境自动删除console
+    compress: {
+      drop_debugger: true,
+      drop_console: false,
+      pure_funcs: ["console.log"],
+    },
+  },
+  sourceMap: false,
+  parallel: true,
 };
 
+const plugins = isProd
+  ? [
+      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
+      new webpack.HashedModuleIdsPlugin(),
+      new UglifyJsPlugin(codeUglifyConfig),
+    ]
+  : [];
+
 module.exports = {
   uploadAssetsToOSS,
   assetsGzip,
-  codeUglify,
+  plugins,
   externals,
   optimization,
   cdns,

+ 1 - 3
vue.config.js

@@ -1,4 +1,3 @@
-const webpack = require("webpack");
 const prodConfig = require("./prod.config");
 
 module.exports = {
@@ -36,7 +35,7 @@ module.exports = {
     devtool: "source-map",
     externals: prodConfig.externals,
     optimization: prodConfig.optimization,
-    plugins: [new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)],
+    plugins: prodConfig.plugins,
     resolve: {
       extensions: [".js", ".vue", ".json", ".ts"],
     },
@@ -50,7 +49,6 @@ module.exports = {
     config.plugins.delete("preload");
     if (process.env.NODE_ENV === "production") {
       // config.entry("index").add("babel-polyfill");
-      prodConfig.codeUglify(config);
       prodConfig.uploadAssetsToOSS(config);
       // prodConfig.assetsGzip(config);
       config.plugin("html").tap((args) => {