Skip to content

Commit

Permalink
feat(dynamic-import-vars): Error when files not found (#1611)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
KristjΓ‘n Oddsson and shellscape committed Oct 25, 2023
1 parent dcd8da5 commit a9b9cd3
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
run: |
git pull --force --no-tags origin master:master
git checkout master
git fetch --tags
- name: Setup Node
uses: actions/setup-node@v3
Expand All @@ -38,7 +39,7 @@ jobs:
id: pnpm-setup
run: |
corepack enable
corepack prepare pnpm@latest --activate
corepack prepare pnpm@8 --activate
pnpm config set script-shell "/usr/bin/bash"
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint:js": "eslint --cache packages scripts shared util --ext .js,.ts,.mjs",
"lint:json": "prettier --write .github/**/*.yml **/tsconfig.json tsconfig.*.json pnpm-workspace.yaml",
"lint:package": "prettier --write **/package.json",
"plugin:release": "ts-node ./scripts/release.ts",
"package:release": "versioner --stripShortName='^@.+/plugin-' --target",
"preinstall": "node scripts/disallow-npm.js",
"prepare": "husky install",
"prettier": "prettier --write .",
Expand All @@ -18,7 +18,7 @@
},
"devDependencies": {
"@ava/babel": "2.0.0",
"@dot/versioner": "^0.2.0",
"@dot/versioner": "^0.3.1",
"@rollup/plugin-typescript": "^9.0.1",
"@types/conventional-commits-parser": "^3.0.2",
"@types/node": "14.18.30",
Expand Down
7 changes: 7 additions & 0 deletions packages/dynamic-import-vars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ Default: `[]`

Files to exclude in this plugin (default none).

#### `errorWhenNoFilesFound`

Type: `Boolean`<br>
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`<br>
Expand Down
10 changes: 9 additions & 1 deletion packages/dynamic-import-vars/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function importModule(name) {
return import(`./module-dir-c/${name}.js`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 };␊
`
Binary file not shown.
6 changes: 6 additions & 0 deletions packages/dynamic-import-vars/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9b9cd3

Please sign in to comment.