Skip to content

Commit

Permalink
aaa
Browse files Browse the repository at this point in the history
  • Loading branch information
adryd325 committed Nov 17, 2023
1 parent 5b26908 commit d94316a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
33 changes: 3 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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();
53 changes: 44 additions & 9 deletions src/patcher.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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) {
Expand All @@ -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,
});

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/magicrequire.js → src/wpTools.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down

0 comments on commit d94316a

Please sign in to comment.