Skip to content

Commit

Permalink
match module changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adryd325 committed Nov 16, 2023
1 parent 8d7c109 commit 0d67694
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as esbuild from 'esbuild'
const context = await esbuild.context({
entryPoints: ['src/index.js'],
bundle: true,
outfile: 'dist/webpackToolsRuntime.js',
outfile: 'dist/webpackToolsDev.user.js',
logLevel: 'info',
target: [
'es2020',
Expand Down
7 changes: 4 additions & 3 deletions dist/webpackTools.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// @version 0.1
// @description meow meow emwo
// @author adryd
// @match https://*
// @match http://*
// @include http://*
// @include https://*
// @grant GM_addElement
// @grant GM_getResourceText
// @run-at document-start
// @resource runtimeScript http://127.0.0.1:8080/webpackToolsRuntime.js
// @resource runtimeScript https://adryd325.github.io/webpackTools/webpackToolsRuntime.js
// ==/UserScript==

(() => {
Expand All @@ -21,6 +21,7 @@
webpackVersion: 5,
patches: [],
modules: [],
inspectAll: true
},
{
name: "twitter",
Expand Down
26 changes: 18 additions & 8 deletions src/patcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import config from "./config";

function matchModule(moduleStr, find) {
if (find instanceof RegExp) {
// todo
return false;
} else {
return moduleStr.indexOf(patch.find) != -1;
}
}

const patchesToApply = new Set();
for (const patch of config.patches) {
patchesToApply.add(patch);
Expand All @@ -11,14 +20,13 @@ export function patchModules(modules) {

const patchesToApply = [];
for (let patch of config.patches) {
// TODO: support regexp matches
if (funcStr.indexOf(patch.find) != -1) {
patchesToApply.push(patch.replace);
if (matchModule(funcStr, patch.find)) {
patchesToApply.push(patch);
}
}

for (let patchToApply of patchesToApply) {
funcStr = funcStr.replace(patchToApply.match, patchToApply.replacement);
funcStr = funcStr.replace(patchToApply.replace.match, patchToApply.replace.replacement);
}

if (patchesToApply.length > 0 || config.inspectAll) {
Expand Down Expand Up @@ -60,10 +68,12 @@ export function injectModules(chunk) {
for (let moduleToInject of modulesToInject) {
if (moduleToInject?.needs?.size > 0) {
for (const need of moduleToInject.needs) {
for (let wpModule of Object.values(chunk[1])) {
// TODO: optimize
// TODO: support regexp and moduleId
if (wpModule.__wpt_funcStr.includes(need)) {
for (let wpModule of Object.entries(chunk[1])) {
// match { moduleId: "id" } as well as strings and regex
if (
(need?.moduleId && wpModule[0] == need.moduleId) ||
matchModule(wpModule[1].__wpt_funcStr, need)
) {
moduleToInject.needs.delete(need);
if (moduleToInject.needs.size == 0) {
readyModules.add(moduleToInject);
Expand Down

0 comments on commit 0d67694

Please sign in to comment.