Skip to content

Commit

Permalink
Merge pull request #31 from jane0009/jane0009/crash-fix
Browse files Browse the repository at this point in the history
jane0009/crash-fix
  • Loading branch information
Cynosphere authored Feb 7, 2024
2 parents 7336c98 + 5b94b9d commit 6d7d285
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
14 changes: 8 additions & 6 deletions packages/core/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ function findManifests(dir: string): string[] {
const path = requireImport("path");
const ret = [];

for (const file of fs.readdirSync(dir)) {
if (file === "manifest.json") {
ret.push(path.join(dir, file));
}
if (fs.existsSync(dir)) {
for (const file of fs.readdirSync(dir)) {
if (file === "manifest.json") {
ret.push(path.join(dir, file));
}

if (fs.statSync(path.join(dir, file)).isDirectory()) {
ret.push(...findManifests(path.join(dir, file)));
if (fs.statSync(path.join(dir, file)).isDirectory()) {
ret.push(...findManifests(path.join(dir, file)));
}
}
}

Expand Down
31 changes: 24 additions & 7 deletions packages/core/src/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ function patchModules(entry: WebpackJsonpEntry[1]) {
const wrapped =
`(${moduleString}).apply(this, arguments)\n` +
`//# sourceURL=Webpack-Module-${id}`;
entry[id] = new Function(
"module",
"exports",
"require",
wrapped
) as WebpackModuleFunc;
entry[id] = wrapTry(
new Function(
"module",
"exports",
"require",
wrapped
) as WebpackModuleFunc,
id
);
entry[id].__moonlight = true;
}
}
Expand Down Expand Up @@ -198,6 +201,20 @@ function handleModuleDependencies() {
webpackModules = new Set(sorted.map((x) => x.data));
}

function wrapTry(
fn: WebpackModuleFunc,
moduleName?: string
): WebpackModuleFunc {
const tryFn: WebpackModuleFunc = function (module, exports, require) {
try {
return fn(module, exports, require);
} catch (e) {
logger.error("Failed to run Webpack module", moduleName, e);
}
};
return tryFn;
}

const injectedWpModules: IdentifiedWebpackModule[] = [];
function injectModules(entry: WebpackJsonpEntry[1]) {
const modules: Record<string, WebpackModuleFunc> = {};
Expand Down Expand Up @@ -246,7 +263,7 @@ function injectModules(entry: WebpackJsonpEntry[1]) {

inject = true;

if (wpModule.run) modules[id] = wpModule.run;
if (wpModule.run) modules[id] = wrapTry(wpModule.run, id);
if (wpModule.entrypoint) entrypoints.push(id);
}
if (!webpackModules.size) break;
Expand Down

0 comments on commit 6d7d285

Please sign in to comment.