diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js index 5b18f59e0..b0d60df96 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -9,7 +9,7 @@ import { createFilter } from '@rollup/pluginutils'; import { dynamicImportToGlob, VariableDynamicImportError } from './dynamic-import-to-glob'; -function dynamicImportVariables({ include, exclude, warnOnError } = {}) { +function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFilesFound } = {}) { const filter = createFilter(include, exclude); return { @@ -55,7 +55,7 @@ function dynamicImportVariables({ include, exclude, warnOnError } = {}) { r.startsWith('./') || r.startsWith('../') ? r : `./${r}` ); - if (paths.length === 0) { + if (errorWhenNoFilesFound && paths.length === 0) { this.error( new Error('No files found when trying to dynamically load concatted string') ); diff --git a/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js b/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js index e7454859e..68be487af 100644 --- a/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js +++ b/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js @@ -205,20 +205,25 @@ test('dynamic imports assertions', async (t) => { t.snapshot(output[0].code); }); -test('no files in dir', async (t) => { +test("doesn't throw if no files in dir when option isn't set", async (t) => { let thrown = false; try { await rollup({ input: 'fixture-no-files.js', plugins: [dynamicImportVars()] }); + } catch (_) { + thrown = true; + } + t.false(thrown); +}); + +test('throws if no files in dir when option is set', async (t) => { + let thrown = false; + try { await rollup({ - input: 'fixture-extensionless.js', - plugins: [ - dynamicImportVars({ - exclude: ['fixture-excluded.js'] - }) - ] + input: 'fixture-no-files.js', + plugins: [dynamicImportVars({ errorWhenNoFilesFound: true })] }); } catch (_) { thrown = true;