浏览代码

fix: bootstrap guard, hopefully final

tophf 3 年之前
父节点
当前提交
9040fa5051
共有 3 个文件被更改,包括 12 次插入8 次删除
  1. 5 2
      scripts/plaid.conf.js
  2. 7 5
      scripts/webpack-protect-bootstrap-plugin.js
  3. 0 1
      scripts/webpack.conf.js

+ 5 - 2
scripts/plaid.conf.js

@@ -45,8 +45,11 @@ exports.optimization = {
           'node_modules/@violentmonkey/shortcut',
           'node_modules/@violentmonkey/shortcut',
           'node_modules/@?vue',
           'node_modules/@?vue',
         ].map(re => re.source || re).join('|').replace(/\\?\//g, '[/\\\\]')),
         ].map(re => re.source || re).join('|').replace(/\\?\//g, '[/\\\\]')),
-        /** Excluding the background entry because it uses only 4kB of common code */
-        chunks: c => c.name !== 'background/index',
+        chunks: c => ![
+          'background/index', // only 4kB of common code
+          'injected',
+          'injected-web',
+        ].includes(c.name),
       },
       },
       ...splitVendor('codemirror'),
       ...splitVendor('codemirror'),
       ...splitVendor('tldjs'),
       ...splitVendor('tldjs'),

+ 7 - 5
scripts/webpack-protect-bootstrap-plugin.js

@@ -66,13 +66,15 @@ class WebpackProtectBootstrapPlugin {
 }
 }
 
 
 function patchBootstrap(src) {
 function patchBootstrap(src) {
-  const props = src.match(new RegExp(`(?<=\\b${G.require}\\.)(\\w+)`, 'g'));
-  const uniq = [...new Set(props)].join('');
-  const guard = uniq
-    ? `for (let i = 0, props=${JSON.stringify(uniq)}; i < props.length; i++)
+  const props = src.match(new RegExp(`(?<=\\b${G.require}\\.)(\\w+)(?= = )`, 'g'));
+  const guard = props
+    ? `for (let i = 0, props="${[...new Set(props)].join('')}"; i < props.length; i++)
       defineProperty(${G.require}, props[i], {__proto__: null, value: 0, writable: 1});\n`
       defineProperty(${G.require}, props[i], {__proto__: null, value: 0, writable: 1});\n`
     : '';
     : '';
-  return guard + replace(BOOTSTRAP_RULES, src, this);
+  const res = replace(BOOTSTRAP_RULES, src, this);
+  // splicing the guard after the first line to handle `require = {}`
+  const i = res.indexOf('\n');
+  return res.slice(0, i) + guard + res.slice(i);
 }
 }
 
 
 function replace(rules, src, info) {
 function replace(rules, src, info) {

+ 0 - 1
scripts/webpack.conf.js

@@ -133,7 +133,6 @@ module.exports = Promise.all([
   }),
   }),
 
 
   modify('injected-web', './src/injected/web', (config) => {
   modify('injected-web', './src/injected/web', (config) => {
-    // TODO: replace WebPack's Object.*, .call(), .apply() with safe calls
     config.output.libraryTarget = 'commonjs2';
     config.output.libraryTarget = 'commonjs2';
     config.plugins.push(new ProtectWebpackBootstrapPlugin());
     config.plugins.push(new ProtectWebpackBootstrapPlugin());
     addWrapperWithGlobals('injected/web', config, defsObj, getGlobals => ({
     addWrapperWithGlobals('injected/web', config, defsObj, getGlobals => ({