···
for (const [id, func] of Object.entries(entry)) {
if (func.__moonlight === true) continue;
106
-
let moduleString = moduleCache[id];
107
+
// Clone the module string so finds don't get messed up by other extensions
108
+
const origModuleString = moduleCache[id];
109
+
let moduleString = origModuleString;
110
+
const patchedStr = [];
111
+
const mappedName = moonlight.moonmap.modules[id];
for (let i = 0; i < patches.length; i++) {
const patch = patches[i];
···
patch.find.lastIndex = 0;
119
-
const match = testFind(moduleString, patch.find);
124
+
const match = testFind(origModuleString, patch.find) || patch.find === mappedName;
// Global regexes apply to all modules
const shouldRemove = typeof patch.find === "string" ? true : !patch.find.global;
129
+
let replaced = moduleString;
130
+
let hardFailed = false;
125
-
// We ensured all arrays get turned into normal PatchReplace objects on register
126
-
const replace = patch.replace as PatchReplace;
132
+
// We ensured normal PatchReplace objects get turned into arrays on register
133
+
const replaces = patch.replace as PatchReplace[];
128
-
if (replace.type === undefined || replace.type === PatchReplaceType.Normal) {
129
-
// tsc fails to detect the overloads for this, so I'll just do this
130
-
// Verbose, but it works
132
-
if (typeof replace.replacement === "string") {
133
-
replaced = moduleString.replace(replace.match, replace.replacement);
135
-
replaced = moduleString.replace(replace.match, replace.replacement);
135
+
for (let i = 0; i < replaces.length; i++) {
136
+
const replace = replaces[i];
137
+
let patchId = `${patch.ext}#${patch.id}`;
138
+
if (replaces.length > 1) patchId += `#${i}`;
139
+
patchedStr.push(patchId);
138
-
if (replaced === moduleString) {
139
-
logger.warn("Patch replacement failed", id, patch);
141
+
if (replace.type === undefined || replace.type === PatchReplaceType.Normal) {
142
+
// tsc fails to detect the overloads for this, so I'll just do this
143
+
// Verbose, but it works
144
+
if (typeof replace.replacement === "string") {
145
+
replaced = replaced.replace(replace.match, replace.replacement);
147
+
replaced = replaced.replace(replace.match, replace.replacement);
143
-
if (patchModule(id, `${patch.ext}#${patch.id}`, replaced)) {
144
-
moduleString = replaced;
150
+
if (replaced === moduleString) {
151
+
logger.warn("Patch replacement failed", id, patch);
152
+
if (patch.hardFail) {
159
+
} else if (replace.type === PatchReplaceType.Module) {
160
+
// Directly replace the module with a new one
161
+
const newModule = replace.replacement(replaced);
162
+
entry[id] = newModule;
163
+
entry[id].__moonlight = true;
164
+
replaced = replaced.toString().replace(/\n/g, "") + `//# sourceURL=Webpack-Module-${id}`;
146
-
} else if (replace.type === PatchReplaceType.Module) {
147
-
// Directly replace the module with a new one
148
-
const newModule = replace.replacement(moduleString);
149
-
entry[id] = newModule;
150
-
entry[id].__moonlight = true;
151
-
moduleString = newModule.toString().replace(/\n/g, "") + `//# sourceURL=Webpack-Module-${id}`;
168
+
if (!hardFailed) moduleString = replaced;
moonlight.unpatched.delete(patch);
156
-
if (shouldRemove) {
157
-
patches.splice(i--, 1);
171
+
if (shouldRemove) patches.splice(i--, 1);
175
+
patchModule(id, patchedStr.join(", "), moduleString);
moduleCache[id] = moduleString;