Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dynamic-import-vars): Error when no files found #1611

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 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,12 @@ function dynamicImportVariables({ include, exclude, warnOnError } = {}) {
r.startsWith('./') || r.startsWith('../') ? r : `./${r}`
);

if (errorWhenNoFilesFound && paths.length === 0) {
this.error(
new Error('No files found when trying to dynamically load concatted string')
koddsson marked this conversation as resolved.
Show resolved Hide resolved
);
}

// 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,29 @@ 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 (_) {
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.