Browse Source

chore: override internal plaid's minimizer

tophf 4 years ago
parent
commit
7f9f01888b
2 changed files with 31 additions and 30 deletions
  1. 0 30
      scripts/plaid.conf.js
  2. 31 0
      scripts/webpack.conf.js

+ 0 - 30
scripts/plaid.conf.js

@@ -1,6 +1,5 @@
 const { isProd } = require('@gera2ld/plaid/util');
 const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
-const TerserPlugin = isProd && require('terser-webpack-plugin');
 
 /**
  * For each entry, `key` is the chunk name, `value` has following properties:
@@ -24,17 +23,6 @@ exports.pages = [
   },
 }), {});
 
-const minimizerOptions = {
-  cache: true,
-  parallel: true,
-  sourceMap: true,
-  terserOptions: {
-    output: {
-      ascii_only: true,
-    },
-  },
-};
-
 const splitVendor = prefix => ({
   [prefix]: {
     test: new RegExp(`node_modules[/\\\\]${prefix}`),
@@ -83,23 +71,5 @@ exports.optimization = {
         require('cssnano'),
       ]),
     }),
-    // Minifying Violentmonkey code
-    new TerserPlugin({
-      chunkFilter: ({ name }) => !name.startsWith('public/'),
-      ...minimizerOptions,
-      terserOptions: {
-        ...minimizerOptions.terserOptions,
-        compress: {
-          ecma: 6,
-          // 'safe' since we don't rely on function prototypes
-          unsafe_arrows: true,
-        },
-      },
-    }),
-    // Minifying non-Violentmonkey code
-    new TerserPlugin({
-      chunkFilter: ({ name }) => name.startsWith('public/'),
-      ...minimizerOptions,
-    }),
   ],
 };

+ 31 - 0
scripts/webpack.conf.js

@@ -4,6 +4,8 @@ const fs = require('fs');
 const webpack = require('webpack');
 const WrapperWebpackPlugin = require('wrapper-webpack-plugin');
 const HTMLInlineCSSWebpackPlugin = isProd && require('html-inline-css-webpack-plugin').default;
+const TerserPlugin = isProd && require('terser-webpack-plugin');
+const deepmerge = isProd && require('deepmerge');
 const projectConfig = require('./plaid.conf');
 const mergedConfig = shallowMerge(defaultOptions, projectConfig);
 
@@ -20,6 +22,16 @@ const WEBPACK_OPTS = {
     maxAssetSize: 0.5e6,
   },
 };
+const minimizerOptions = {
+  cache: true,
+  parallel: true,
+  sourceMap: true,
+  terserOptions: {
+    output: {
+      ascii_only: true,
+    },
+  },
+};
 
 const pickEnvs = (items) => {
   return Object.assign({}, ...items.map(x => ({
@@ -80,6 +92,25 @@ const modify = (page, entry, init) => modifyWebpackConfig(
   (config) => {
     Object.assign(config, WEBPACK_OPTS);
     config.plugins.push(definitions);
+    config.optimization.minimizer.find((m, i, arr) => (
+      m.constructor.name === 'TerserPlugin' && arr.splice(i, 1)
+    ));
+    config.optimization.minimizer.push(...!isProd ? [] : [
+      new TerserPlugin({
+        chunkFilter: ({ name }) => name.startsWith('public/'),
+        ...minimizerOptions,
+      }),
+      new TerserPlugin(deepmerge.all([{}, minimizerOptions, {
+        chunkFilter: ({ name }) => !name.startsWith('public/'),
+        terserOptions: {
+          compress: {
+            ecma: 8, // ES2017 Object.entries and so on
+            passes: 2, // necessary now since we removed plaid's minimizer
+            unsafe_arrows: true, // it's 'safe' since we don't rely on function prototypes
+          },
+        },
+      }])),
+    ]);
     if (!entry) init = page;
     if (init) init(config);
     return config;