From 7cbb08a95fa2169056c11b57a8ed2f5871b75fa3 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 7 May 2023 17:19:57 -0400 Subject: [PATCH] feat: improve error for failing to import/require from package.json --- 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 ); }); });