From e133737ffe864db87cdd7ef131c9b30cccc94300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Mon, 13 Nov 2023 00:51:37 -0500 Subject: [PATCH] Throw better exception when failing to load plugin (#447) Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com> --- lib/package-json.ts | 6 ++++-- test/lib/generate/cjs-test.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/package-json.ts b/lib/package-json.ts index 4beb6c80..1b328d60 100644 --- a/lib/package-json.ts +++ b/lib/package-json.ts @@ -36,7 +36,7 @@ export async function loadPlugin(path: string): Promise { 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; @@ -61,8 +61,10 @@ export async function loadPlugin(path: string): Promise { } } + // 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); diff --git a/test/lib/generate/cjs-test.ts b/test/lib/generate/cjs-test.ts index 88eb1a2f..577e6ccd 100644 --- a/test/lib/generate/cjs-test.ts +++ b/test/lib/generate/cjs-test.ts @@ -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 ); }); });