From b65f57843553278b282a469f5b889c5762f60e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Mon, 6 Nov 2023 10:50:03 +0100 Subject: [PATCH 1/2] allow no files error to be emitted as warning --- packages/dynamic-import-vars/README.md | 2 ++ packages/dynamic-import-vars/src/index.js | 11 ++++++---- .../rollup-plugin-dynamic-import-vars.test.js | 20 ++++++++++++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/dynamic-import-vars/README.md b/packages/dynamic-import-vars/README.md index bd3eb82bd..51f9e4554 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. +When used in combination for `warnOnError` it will throw a warning instead. + #### `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); +}); From 09eaffc384bc5e98da918d20005eda402a1396f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Tue, 28 Nov 2023 14:30:29 +0100 Subject: [PATCH 2/2] Update packages/dynamic-import-vars/README.md Co-authored-by: Andrew Powell --- packages/dynamic-import-vars/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dynamic-import-vars/README.md b/packages/dynamic-import-vars/README.md index 51f9e4554..ae663cdf2 100644 --- a/packages/dynamic-import-vars/README.md +++ b/packages/dynamic-import-vars/README.md @@ -68,7 +68,7 @@ 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. -When used in combination for `warnOnError` it will throw a warning instead. +⚠️ _Important:_ Enabling this option when `warnOnError` is set to `true` will result in a warning and _not_ an error #### `warnOnError`