diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index d5a0b898a..adb78d2ec 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -38,7 +38,7 @@ jobs:
id: pnpm-setup
run: |
corepack enable
- corepack prepare pnpm@latest --activate
+ corepack prepare pnpm@8.8.0 --activate
pnpm config set script-shell "/usr/bin/bash"
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"
diff --git a/packages/dynamic-import-vars/README.md b/packages/dynamic-import-vars/README.md
index cd14fdfc8..bd3eb82bd 100644
--- a/packages/dynamic-import-vars/README.md
+++ b/packages/dynamic-import-vars/README.md
@@ -61,6 +61,13 @@ Default: `[]`
Files to exclude in this plugin (default none).
+#### `errorWhenNoFilesFound`
+
+Type: `Boolean`
+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.
+
#### `warnOnError`
Type: `Boolean`
diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js
index a8b036518..3d631b729 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,6 +55,14 @@ function dynamicImportVariables({ include, exclude, warnOnError } = {}) {
r.startsWith('./') || r.startsWith('../') ? r : `./${r}`
);
+ if (errorWhenNoFilesFound && paths.length === 0) {
+ this.error(
+ new Error(
+ `No files found in ${glob} when trying to dynamically load concatted string from ${id}`
+ )
+ );
+ }
+
// create magic string if it wasn't created already
ms = ms || new MagicString(code);
// unpack variable dynamic import into a function with import statements per file, rollup
diff --git a/packages/dynamic-import-vars/test/fixtures/fixture-no-files.js b/packages/dynamic-import-vars/test/fixtures/fixture-no-files.js
new file mode 100644
index 000000000..361d07d60
--- /dev/null
+++ b/packages/dynamic-import-vars/test/fixtures/fixture-no-files.js
@@ -0,0 +1,3 @@
+export function importModule(name) {
+ return import(`./module-dir-c/${name}.js`);
+}
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 e0e1f7d63..6a0ae9448 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
@@ -204,3 +204,35 @@ test('dynamic imports assertions', async (t) => {
);
t.snapshot(output[0].code);
});
+
+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-no-files.js',
+ plugins: [dynamicImportVars({ errorWhenNoFilesFound: true })]
+ });
+ } catch (error) {
+ t.deepEqual(
+ error.message,
+ `No files found in ./module-dir-c/*.js when trying to dynamically load concatted string from ${require.resolve(
+ './fixtures/fixture-no-files.js'
+ )}`
+ );
+ thrown = true;
+ }
+ t.true(thrown);
+});
diff --git a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md
index 611c7f70f..0889053e5 100644
--- a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md
+++ b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md
@@ -234,3 +234,25 @@ Generated by [AVA](https://avajs.dev).
␊
export { importModule };␊
`
+
+## no files in dir
+
+> Snapshot 1
+
+ `function __variableDynamicImportRuntime0__(path) {␊
+ switch (path) {␊
+ ␊
+ default: return new Promise(function(resolve, reject) {␊
+ (typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
+ reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
+ );␊
+ })␊
+ }␊
+ }␊
+ ␊
+ function importModule(name) {␊
+ return __variableDynamicImportRuntime0__(\`./module-dir-c/${name}.js\`);␊
+ }␊
+ ␊
+ export { importModule };␊
+ `
diff --git a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap
index 1239582e0..8ee67b5d8 100644
Binary files a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap and b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap differ
diff --git a/packages/dynamic-import-vars/types/index.d.ts b/packages/dynamic-import-vars/types/index.d.ts
index ca4af9c4c..353e9f823 100644
--- a/packages/dynamic-import-vars/types/index.d.ts
+++ b/packages/dynamic-import-vars/types/index.d.ts
@@ -15,6 +15,12 @@ interface RollupDynamicImportVariablesOptions {
* By default no files are ignored.
*/
exclude?: FilterPattern;
+ /**
+ * 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.
+ * @default false
+ */
+ errorWhenNoFilesFound?: boolean;
/**
* By default, the plugin quits the build process when it encounters an error.
* If you set this option to true, it will throw a warning instead and leave the code untouched.