diff --git a/.changeset/dirty-phones-chew.md b/.changeset/dirty-phones-chew.md new file mode 100644 index 0000000..43c19e9 --- /dev/null +++ b/.changeset/dirty-phones-chew.md @@ -0,0 +1,5 @@ +--- +"@proload/core": patch +--- + +Gracefully support empty config files (again) diff --git a/fixtures/empty-export/test.config.mjs b/fixtures/empty-export/test.config.mjs new file mode 100644 index 0000000..661ce2f --- /dev/null +++ b/fixtures/empty-export/test.config.mjs @@ -0,0 +1 @@ +export const other = true; diff --git a/fixtures/empty/test.config.mjs b/fixtures/empty/test.config.mjs new file mode 100644 index 0000000..e69de29 diff --git a/packages/core/lib/esm/index.mjs b/packages/core/lib/esm/index.mjs index 98d43c1..2e4cce0 100644 --- a/packages/core/lib/esm/index.mjs +++ b/packages/core/lib/esm/index.mjs @@ -237,6 +237,19 @@ async function load(namespace, opts = {}) { let rawValue = await requireOrImportWithMiddleware(filePath); if (filePath.endsWith('package.json')) rawValue = rawValue[namespace]; + // Important: "empty" config files will be returned as `Module {}` + // We should handle them here + if (rawValue && !(rawValue instanceof Object)) { + if (mustExist) { + assert( + true, + `Resolved a ${namespace} configuration, but no configuration was exported`, + "ERR_PROLOAD_NOT_FOUND" + ); + } else { + return; + } + } const resolvedValue = await resolveExtensions(namespace, { filePath, value: rawValue, diff --git a/packages/core/test/index.mjs b/packages/core/test/index.mjs index ec65209..f3102c5 100644 --- a/packages/core/test/index.mjs +++ b/packages/core/test/index.mjs @@ -32,6 +32,36 @@ test('missing but not mustExist', async () => { type(mdl, 'undefined') }); +test('empty but not mustExist', async () => { + let mdl = await load('test', { cwd: resolve(`fixtures/empty`), mustExist: false }); + type(mdl, 'undefined') +}); + +test('empty but mustExist (default)', async () => { + let err = 0; + try { + let mdl = await load('test', { cwd: resolve(`fixtures/empty`) }); + } catch (e) { + err += 1; + } + is(err, 1); +}); + +test('empty export but not mustExist', async () => { + let mdl = await load('test', { cwd: resolve(`fixtures/empty-export`), mustExist: false }); + type(mdl, 'undefined') +}); + +test('empty export but mustExist (default)', async () => { + let err = 0; + try { + let mdl = await load('test', { cwd: resolve(`fixtures/empty-export`) }); + } catch (e) { + err += 1; + } + is(err, 1); +}); + const throwFixtures = ['ts', 'ts-config', 'json', 'json-config']; for (const fixture of throwFixtures) {