1
0
Эх сурвалжийг харах

chore: extract ListBackgroundScriptsPlugin

+ include manifest-helper.js in eslint
tophf 4 жил өмнө
parent
commit
567950977b

+ 1 - 0
.eslintignore

@@ -2,6 +2,7 @@
 !src/**
 !src/**
 !test/**
 !test/**
 !scripts/fake-dep-loader.js
 !scripts/fake-dep-loader.js
+!scripts/manifest-helper.js
 !scripts/plaid.conf.js
 !scripts/plaid.conf.js
 !scripts/webpack.conf.js
 !scripts/webpack.conf.js
 !gulpfile.js
 !gulpfile.js

+ 29 - 2
scripts/manifest-helper.js

@@ -16,7 +16,9 @@ async function buildManifest() {
   }
   }
   if (isBeta()) {
   if (isBeta()) {
     // Do not support i18n in beta version
     // Do not support i18n in beta version
-    data.name = data.browser_action.default_title = 'Violentmonkey BETA';
+    const name = 'Violentmonkey BETA';
+    data.name = name;
+    data.browser_action.default_title = name;
   }
   }
   return data;
   return data;
 }
 }
@@ -32,12 +34,37 @@ async function buildUpdatesList(version, url) {
             update_link: url,
             update_link: url,
           },
           },
         ],
         ],
-      }
+      },
     },
     },
   };
   };
   return data;
   return data;
 }
 }
 
 
+class ListBackgroundScriptsPlugin {
+  constructor({ minify } = {}) {
+    this.minify = minify;
+  }
+
+  apply(compiler) {
+    compiler.hooks.afterEmit.tap(this.constructor.name, async compilation => {
+      const dist = compilation.outputOptions.path;
+      const path = `${dist}/manifest.json`;
+      const manifest = JSON.parse(await fs.readFile(path, { encoding: 'utf8' }));
+      const bgId = 'background/index';
+      const bgEntry = compilation.entrypoints.get(bgId);
+      const scripts = bgEntry.chunks.map(c => c.files[0]);
+      if (`${manifest.background.scripts}` !== `${scripts}`) {
+        manifest.background.scripts = scripts;
+        await fs.writeFile(path,
+          JSON.stringify(manifest, null, this.minify ? 0 : 2),
+          { encoding: 'utf8' });
+      }
+      await fs.unlink(`${dist}/${bgId}.html`).catch(() => {});
+    });
+  }
+}
+
 exports.readManifest = readManifest;
 exports.readManifest = readManifest;
 exports.buildManifest = buildManifest;
 exports.buildManifest = buildManifest;
 exports.buildUpdatesList = buildUpdatesList;
 exports.buildUpdatesList = buildUpdatesList;
+exports.ListBackgroundScriptsPlugin = ListBackgroundScriptsPlugin;

+ 21 - 34
scripts/webpack.conf.js

@@ -6,6 +6,7 @@ const WrapperWebpackPlugin = require('wrapper-webpack-plugin');
 const HTMLInlineCSSWebpackPlugin = isProd && require('html-inline-css-webpack-plugin').default;
 const HTMLInlineCSSWebpackPlugin = isProd && require('html-inline-css-webpack-plugin').default;
 const TerserPlugin = isProd && require('terser-webpack-plugin');
 const TerserPlugin = isProd && require('terser-webpack-plugin');
 const deepmerge = isProd && require('deepmerge');
 const deepmerge = isProd && require('deepmerge');
+const { ListBackgroundScriptsPlugin } = require('./manifest-helper');
 const projectConfig = require('./plaid.conf');
 const projectConfig = require('./plaid.conf');
 const mergedConfig = shallowMerge(defaultOptions, projectConfig);
 const mergedConfig = shallowMerge(defaultOptions, projectConfig);
 
 
@@ -23,7 +24,7 @@ const WEBPACK_OPTS = {
     maxAssetSize: 0.5e6,
     maxAssetSize: 0.5e6,
   },
   },
 };
 };
-const minimizerOptions = {
+const MIN_OPTS = {
   cache: true,
   cache: true,
   parallel: true,
   parallel: true,
   sourceMap: true,
   sourceMap: true,
@@ -33,6 +34,20 @@ const minimizerOptions = {
     },
     },
   },
   },
 };
 };
+const MIN_OPTS_PUBLIC = isProd && {
+  chunkFilter: ({ name }) => name.startsWith('public/'),
+  ...MIN_OPTS,
+};
+const MIN_OPTS_MAIN = isProd && deepmerge.all([{}, MIN_OPTS, {
+  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
+    },
+  },
+}]);
 
 
 const pickEnvs = (items) => {
 const pickEnvs = (items) => {
   return Object.assign({}, ...items.map(x => ({
   return Object.assign({}, ...items.map(x => ({
@@ -97,20 +112,8 @@ const modify = (page, entry, init) => modifyWebpackConfig(
       m.constructor.name === 'TerserPlugin' && arr.splice(i, 1)
       m.constructor.name === 'TerserPlugin' && arr.splice(i, 1)
     ));
     ));
     config.optimization.minimizer.push(...!isProd ? [] : [
     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
-          },
-        },
-      }])),
+      new TerserPlugin(MIN_OPTS_PUBLIC),
+      new TerserPlugin(MIN_OPTS_MAIN),
     ]);
     ]);
     if (!entry) init = page;
     if (!entry) init = page;
     if (init) init(config);
     if (init) init(config);
@@ -145,32 +148,16 @@ module.exports = Promise.all([
         && Object.assign(p.options, { ignoreOrder: true })
         && Object.assign(p.options, { ignoreOrder: true })
       ));
       ));
     }
     }
-    config.plugins.push(new class ListBackgroundScripts {
-      apply(compiler) {
-        compiler.hooks.afterEmit.tap(this.constructor.name, compilation => {
-          const dist = compilation.outputOptions.path;
-          const path = `${dist}/manifest.json`;
-          const manifest = JSON.parse(fs.readFileSync(path, { encoding: 'utf8' }));
-          const bgId = 'background/index';
-          const bgEntry = compilation.entrypoints.get(bgId);
-          const scripts = bgEntry.chunks.map(c => c.files[0]);
-          if (`${manifest.background.scripts}` !== `${scripts}`) {
-            manifest.background.scripts = scripts;
-            fs.writeFileSync(path,
-              JSON.stringify(manifest, null, isProd ? 0 : 2),
-              { encoding: 'utf8' });
-          }
-          fs.promises.unlink(`${dist}/${bgId}.html`).catch(() => {});
-        });
-      }
-    }());
+    config.plugins.push(new ListBackgroundScriptsPlugin());
   }),
   }),
+
   modify('injected', './src/injected', (config) => {
   modify('injected', './src/injected', (config) => {
     addWrapper(config, getGlobals => ({
     addWrapper(config, getGlobals => ({
       header: () => `${skipReinjectionHeader} { ${getGlobals()}`,
       header: () => `${skipReinjectionHeader} { ${getGlobals()}`,
       footer: '}',
       footer: '}',
     }));
     }));
   }),
   }),
+
   modify('injected-web', './src/injected/web', (config) => {
   modify('injected-web', './src/injected/web', (config) => {
     config.output.libraryTarget = 'commonjs2';
     config.output.libraryTarget = 'commonjs2';
     addWrapper(config, getGlobals => ({
     addWrapper(config, getGlobals => ({