Skip to content

Commit

Permalink
Throw better exception when failing to load plugin (#447)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryan Mishkin <[email protected]>
  • Loading branch information
JoshuaKGoldberg and bmish authored Nov 13, 2023
1 parent 4e45841 commit e133737
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function loadPlugin(path: string): Promise<Plugin> {
try {
// Try require first which should work for CJS plugins.
return require(pluginRoot) as Plugin; // eslint-disable-line import/no-dynamic-require
} catch {
} catch (error) {
// Otherwise, for ESM plugins, we'll have to try to resolve the exact plugin entry point and import it.
const pluginPackageJson = loadPackageJson(path);
let pluginEntryPoint;
Expand All @@ -61,8 +61,10 @@ export async function loadPlugin(path: string): Promise<Plugin> {
}
}

// If the ESM export doesn't exist, fall back to throwing the CJS error
// (if the ESM export does exist, we'll validate it next)
if (!pluginEntryPoint) {
throw new Error('Unable to determine plugin entry point.');
throw error;
}

const pluginEntryPointAbs = join(pluginRoot, pluginEntryPoint);
Expand Down
2 changes: 1 addition & 1 deletion test/lib/generate/cjs-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('generate (cjs)', function () {
'cjs-main-file-does-not-exist'
);
await expect(generate(FIXTURE_PATH)).rejects.toThrow(
'Unable to determine plugin entry point.'
/Cannot find module/u
);
});
});
Expand Down

0 comments on commit e133737

Please sign in to comment.