Skip to content

Commit a9b9cd3

Browse files
KristjΓ‘n Oddssonshellscape
andcommitted
feat(dynamic-import-vars): Error when files not found (#1611)
* Add test for throwing if there are no files found * Throw error if no files were found * update error message with more context * chore: update readme and types --------- Co-authored-by: shellscape <[email protected]>
1 parent dcd8da5 commit a9b9cd3

File tree

10 files changed

+87
-8
lines changed

10 files changed

+87
-8
lines changed

β€Ž.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
run: |
2828
git pull --force --no-tags origin master:master
2929
git checkout master
30+
git fetch --tags
3031
3132
- name: Setup Node
3233
uses: actions/setup-node@v3
@@ -38,7 +39,7 @@ jobs:
3839
id: pnpm-setup
3940
run: |
4041
corepack enable
41-
corepack prepare pnpm@latest --activate
42+
corepack prepare pnpm@8 --activate
4243
pnpm config set script-shell "/usr/bin/bash"
4344
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"
4445

β€Žpackage.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"lint:js": "eslint --cache packages scripts shared util --ext .js,.ts,.mjs",
1010
"lint:json": "prettier --write .github/**/*.yml **/tsconfig.json tsconfig.*.json pnpm-workspace.yaml",
1111
"lint:package": "prettier --write **/package.json",
12-
"plugin:release": "ts-node ./scripts/release.ts",
12+
"package:release": "versioner --stripShortName='^@.+/plugin-' --target",
1313
"preinstall": "node scripts/disallow-npm.js",
1414
"prepare": "husky install",
1515
"prettier": "prettier --write .",
@@ -18,7 +18,7 @@
1818
},
1919
"devDependencies": {
2020
"@ava/babel": "2.0.0",
21-
"@dot/versioner": "^0.2.0",
21+
"@dot/versioner": "^0.3.1",
2222
"@rollup/plugin-typescript": "^9.0.1",
2323
"@types/conventional-commits-parser": "^3.0.2",
2424
"@types/node": "14.18.30",

β€Žpackages/dynamic-import-vars/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ Default: `[]`
6161

6262
Files to exclude in this plugin (default none).
6363

64+
#### `errorWhenNoFilesFound`
65+
66+
Type: `Boolean`<br>
67+
Default: `false`
68+
69+
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.
70+
6471
#### `warnOnError`
6572

6673
Type: `Boolean`<br>

β€Žpackages/dynamic-import-vars/src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { createFilter } from '@rollup/pluginutils';
99

1010
import { dynamicImportToGlob, VariableDynamicImportError } from './dynamic-import-to-glob';
1111

12-
function dynamicImportVariables({ include, exclude, warnOnError } = {}) {
12+
function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFilesFound } = {}) {
1313
const filter = createFilter(include, exclude);
1414

1515
return {
@@ -55,6 +55,14 @@ function dynamicImportVariables({ include, exclude, warnOnError } = {}) {
5555
r.startsWith('./') || r.startsWith('../') ? r : `./${r}`
5656
);
5757

58+
if (errorWhenNoFilesFound && paths.length === 0) {
59+
this.error(
60+
new Error(
61+
`No files found in ${glob} when trying to dynamically load concatted string from ${id}`
62+
)
63+
);
64+
}
65+
5866
// create magic string if it wasn't created already
5967
ms = ms || new MagicString(code);
6068
// unpack variable dynamic import into a function with import statements per file, rollup
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function importModule(name) {
2+
return import(`./module-dir-c/${name}.js`);
3+
}

β€Žpackages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,35 @@ test('dynamic imports assertions', async (t) => {
204204
);
205205
t.snapshot(output[0].code);
206206
});
207+
208+
test("doesn't throw if no files in dir when option isn't set", async (t) => {
209+
let thrown = false;
210+
try {
211+
await rollup({
212+
input: 'fixture-no-files.js',
213+
plugins: [dynamicImportVars()]
214+
});
215+
} catch (_) {
216+
thrown = true;
217+
}
218+
t.false(thrown);
219+
});
220+
221+
test('throws if no files in dir when option is set', async (t) => {
222+
let thrown = false;
223+
try {
224+
await rollup({
225+
input: 'fixture-no-files.js',
226+
plugins: [dynamicImportVars({ errorWhenNoFilesFound: true })]
227+
});
228+
} catch (error) {
229+
t.deepEqual(
230+
error.message,
231+
`No files found in ./module-dir-c/*.js when trying to dynamically load concatted string from ${require.resolve(
232+
'./fixtures/fixture-no-files.js'
233+
)}`
234+
);
235+
thrown = true;
236+
}
237+
t.true(thrown);
238+
});

β€Žpackages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,25 @@ Generated by [AVA](https://avajs.dev).
234234
␊
235235
export { importModule };␊
236236
`
237+
238+
## no files in dir
239+
240+
> Snapshot 1
241+
242+
`function __variableDynamicImportRuntime0__(path) {␊
243+
switch (path) {␊
244+
␊
245+
default: return new Promise(function(resolve, reject) {␊
246+
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
247+
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
248+
);␊
249+
})␊
250+
}␊
251+
}␊
252+
␊
253+
function importModule(name) {␊
254+
return __variableDynamicImportRuntime0__(\`./module-dir-c/${name}.js\`);␊
255+
}␊
256+
␊
257+
export { importModule };␊
258+
`

β€Žpackages/dynamic-import-vars/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ interface RollupDynamicImportVariablesOptions {
1515
* By default no files are ignored.
1616
*/
1717
exclude?: FilterPattern;
18+
/**
19+
* By default, the plugin will not throw errors when target files are not found.
20+
* Setting this option to true will result in errors thrown when encountering files which don't exist.
21+
* @default false
22+
*/
23+
errorWhenNoFilesFound?: boolean;
1824
/**
1925
* By default, the plugin quits the build process when it encounters an error.
2026
* If you set this option to true, it will throw a warning instead and leave the code untouched.

β€Žpnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)