From d94316af5201c8222136e1d5e1bb706c99f1512c Mon Sep 17 00:00:00 2001 From: adryd Date: Thu, 16 Nov 2023 21:47:50 -0500 Subject: [PATCH] aaa --- src/index.js | 33 ++---------------- src/patcher.js | 53 ++++++++++++++++++++++++----- src/{magicrequire.js => wpTools.js} | 4 +-- 3 files changed, 49 insertions(+), 41 deletions(-) rename src/{magicrequire.js => wpTools.js} (87%) diff --git a/src/index.js b/src/index.js index a0d5506..b8533f6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,5 @@ import config from "./config"; -import {patchModules, injectModules} from "./patcher"; +import {interceptWebpack} from "./patcher"; -const chunkObjectName = config.chunkObject; - -// This is necesary since some sites (twitter) define the chunk object earlier -let realChunkObject = window[chunkObjectName]; - -Object.defineProperty(window, chunkObjectName, { - set: function set(value) { - // Don't infinitely re-wrap .push() - if (!value.push.__wpt_injected) { - realChunkObject = value; - const webpackPush = value.push; - - value.push = function (chunk) { - if (!webpackPush.__wpt_injected) { - patchModules(chunk[1]); - injectModules(chunk); - } - return webpackPush.apply(this, arguments); - }; - - value.push.__wpt_injected = true; - console.log("injected " + chunkObjectName); - } - }, - get: function get() { - return realChunkObject; - }, - configurable: true, -}); +// todo: magicrequire everywhere impl +interceptWebpack(); diff --git a/src/patcher.js b/src/patcher.js index 82bcdbb..b1175d7 100644 --- a/src/patcher.js +++ b/src/patcher.js @@ -1,16 +1,48 @@ import config from "./config"; -import magicrequire from "./magicrequire" +import wpTools from "./wpTools"; + +export function interceptWebpack() { + const chunkObjectName = config.chunkObject; + + // This is necesary since some sites (twitter) define the chunk object earlier + let realChunkObject = window[chunkObjectName]; + + Object.defineProperty(window, chunkObjectName, { + set: function set(value) { + // Don't infinitely re-wrap .push() + if (!value.push.__wpt_injected) { + realChunkObject = value; + const webpackPush = value.push; + + value.push = function (chunk) { + if (!webpackPush.__wpt_injected) { + patchModules(chunk[1]); + injectModules(chunk); + } + return webpackPush.apply(this, arguments); + }; + + value.push.__wpt_injected = true; + console.log("injected " + chunkObjectName); + } + }, + get: function get() { + return realChunkObject; + }, + configurable: true, + }); +} export function matchModule(moduleStr, find) { const findArray = find instanceof Array ? find : [find]; return findArray.some((query) => { // we like our microoptimizations https://jsben.ch/Zk8aw if (query instanceof RegExp) { - return moduleStr.match(query) + return query.test(moduleStr); } else { - return moduleStr.includes(query) + return moduleStr.includes(query) } - }) + }); } const patchesToApply = new Set(); @@ -30,7 +62,10 @@ export function patchModules(modules) { } for (let patchToApply of patchesToApply) { - funcStr = funcStr.replace(patchToApply.replace.match, patchToApply.replace.replacement); + funcStr = funcStr.replace( + patchToApply.replace.match, + patchToApply.replace.replacement + ); } if (patchesToApply.length > 0 || config.inspectAll) { @@ -57,10 +92,9 @@ for (const module of config.modules) { modulesToInject.add(module); } -// add placeholder magicrequire whatever modulesToInject.add({ - name: "magicrequire", - run: magicrequire, + name: "wpTools", + run: wpTools, entry: true, }); @@ -101,7 +135,8 @@ export function injectModules(chunk) { } } - chunk[1] = Object.assign(chunk[1], injectModules); + // Patch our own modules, for fun :) + chunk[1] = Object.assign(chunk[1], patchModules(injectModules)); if (injectEntries.length > 0) { switch (config.webpackVersion) { case 5: diff --git a/src/magicrequire.js b/src/wpTools.js similarity index 87% rename from src/magicrequire.js rename to src/wpTools.js index c2d89c9..a91de00 100644 --- a/src/magicrequire.js +++ b/src/wpTools.js @@ -1,6 +1,6 @@ import { matchModule } from "./patcher"; -export default function magicrequire(module, exports, webpackRequire) { +export default function wpTools(module, exports, webpackRequire) { // https://github.com/webpack/webpack/blob/main/lib/RuntimeGlobals.js // modules: webpackRequire.m // exports: webpackRequire.c @@ -22,7 +22,7 @@ export default function magicrequire(module, exports, webpackRequire) { // some cyn magic - window.magicrequire = module.exports.default = exports.default = { + window.wpTools = module.exports.default = exports.default = { findModulesByExports, findModulesByMatches, inspectModule,