Browse Source

fix: reuse plaid for `background`

because this way it participates in chunk splitting
tophf 4 years ago
parent
commit
bd990910f7
2 changed files with 13 additions and 11 deletions
  1. 2 0
      scripts/plaid.conf.js
  2. 11 11
      scripts/webpack.conf.js

+ 2 - 0
scripts/plaid.conf.js

@@ -9,6 +9,7 @@ const TerserPlugin = isProd && require('terser-webpack-plugin');
  * - value.html.inlineSource: if true, JS and CSS files will be inlined in HTML.
  */
 exports.pages = [
+  'background',
   'confirm',
   'options',
   'popup',
@@ -59,6 +60,7 @@ exports.optimization = {
         ].map(re => re.source || re).join('|').replace(/\\?\//g, '[/\\\\]')),
         chunks: 'all',
         minChunks: 2,
+        priority: 100,
       },
       common: {
         name: 'common',

+ 11 - 11
scripts/webpack.conf.js

@@ -58,16 +58,15 @@ const [globalsCommonHeader, globalsInjectedHeader] = [
   './src/injected/safe-injected-globals.js',
 ].map(path =>
   require('fs').readFileSync(path, {encoding: 'utf8'}).replace(/export const/g, 'const'));
-const globalWrapper = new WrapperWebpackPlugin({
-  header: `{ ${globalsCommonHeader}`,
-  footer: `}`,
-  test: /^(?!injected|public).*\.js$/,
-});
 
 module.exports = Promise.all([
   modify((config) => {
     config.output.publicPath = '/';
-    config.plugins.push(globalWrapper);
+    config.plugins.push(new WrapperWebpackPlugin({
+      header: `{ ${globalsCommonHeader}`,
+      footer: `}`,
+      test: /^(?!injected|public).*\.js$/,
+    }));
     /* Embedding as <style> to ensure uiTheme option doesn't cause FOUC.
      * Note that in production build there's no <head> in html but document.head is still
      * auto-created per the specification so our styles will be placed correctly anyway. */
@@ -77,21 +76,22 @@ module.exports = Promise.all([
         position: 'before',
       },
     }));
-  }),
-  modify('background', './src/background', (config) => {
-    config.plugins.push(globalWrapper);
     config.plugins.push(new class ListBackgroundScripts {
       apply(compiler) {
         compiler.hooks.afterEmit.tap(this.constructor.name, compilation => {
-          const path = `${compilation.outputOptions.path}/manifest.json`;
+          const dist = compilation.outputOptions.path;
+          const path = `${dist}/manifest.json`;
           const manifest = JSON.parse(fs.readFileSync(path, {encoding: 'utf8'}));
-          const scripts = [...compilation.entrypoints.values()][0].chunks.map(c => c.files[0]);
+          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.unlinkSync(`${dist}/${bgId}.html`);
         });
       }
     });