diff --git a/packages/dynamic-import-vars/README.md b/packages/dynamic-import-vars/README.md index bd3eb82bd..ae663cdf2 100644 --- a/packages/dynamic-import-vars/README.md +++ b/packages/dynamic-import-vars/README.md @@ -68,6 +68,8 @@ Default: `false` By default, the plugin will not throw errors when target files are not found. Setting this option to true will result in errors thrown when encountering files which don't exist. +⚠️ _Important:_ Enabling this option when `warnOnError` is set to `true` will result in a warning and _not_ an error + #### `warnOnError` Type: `Boolean`
diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js index 3d631b729..b44770c45 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -56,11 +56,14 @@ function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFile ); if (errorWhenNoFilesFound && paths.length === 0) { - this.error( - new Error( - `No files found in ${glob} when trying to dynamically load concatted string from ${id}` - ) + const error = new Error( + `No files found in ${glob} when trying to dynamically load concatted string from ${id}` ); + if (warnOnError) { + this.warn(error); + } else { + this.error(error); + } } // create magic string if it wasn't created already 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 6a0ae9448..58c40091c 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 @@ -218,7 +218,7 @@ test("doesn't throw if no files in dir when option isn't set", async (t) => { t.false(thrown); }); -test('throws if no files in dir when option is set', async (t) => { +test('throws if no files in dir when `errorWhenNoFilesFound` is set', async (t) => { let thrown = false; try { await rollup({ @@ -236,3 +236,21 @@ test('throws if no files in dir when option is set', async (t) => { } t.true(thrown); }); + +test('warns if no files in dir when `errorWhenNoFilesFound` and `warnOnError` are both set', async (t) => { + let warningEmitted = false; + await rollup({ + input: 'fixture-no-files.js', + plugins: [dynamicImportVars({ errorWhenNoFilesFound: true, warnOnError: true })], + onwarn(warning) { + t.deepEqual( + warning.message, + `No files found in ./module-dir-c/*.js when trying to dynamically load concatted string from ${require.resolve( + './fixtures/fixture-no-files.js' + )}` + ); + warningEmitted = true; + } + }); + t.true(warningEmitted); +});