Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw better exception when failing to load plugin #447

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to construct a package.json test case to throw the existing default error... and couldn't figure it out.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to take a look at this so we can test the old exception still.

/Cannot find module/u
);
});
});
Expand Down
Loading