Skip to content

Commit

Permalink
move error functionality behind option
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Oct 16, 2023
1 parent d41a503 commit 339dcee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions 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,7 +55,7 @@ function dynamicImportVariables({ include, exclude, warnOnError } = {}) {
r.startsWith('./') || r.startsWith('../') ? r : `./${r}`
);

if (paths.length === 0) {
if (errorWhenNoFilesFound && paths.length === 0) {
this.error(
new Error('No files found when trying to dynamically load concatted string')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,25 @@ test('dynamic imports assertions', async (t) => {
t.snapshot(output[0].code);
});

test('no files in dir', async (t) => {
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-extensionless.js',
plugins: [
dynamicImportVars({
exclude: ['fixture-excluded.js']
})
]
input: 'fixture-no-files.js',
plugins: [dynamicImportVars({ errorWhenNoFilesFound: true })]
});
} catch (_) {
thrown = true;
Expand Down

0 comments on commit 339dcee

Please sign in to comment.