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

fix(dynamic-import-vars): Allow a "no files found" error to be emitted as warning #1625

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/dynamic-import-vars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ 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.

⚠️ _Important:_ Enabling this option when `warnOnError` is set to `true` will result in a warning and _not_ an error

#### `warnOnError`

Type: `Boolean`<br>
Expand Down
11 changes: 7 additions & 4 deletions packages/dynamic-import-vars/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFile
);

if (errorWhenNoFilesFound && paths.length === 0) {
this.error(
new Error(
`No files found in ${glob} when trying to dynamically load concatted string from ${id}`
)
const error = new Error(
`No files found in ${glob} when trying to dynamically load concatted string from ${id}`
);
if (warnOnError) {
this.warn(error);
} else {
this.error(error);
}
}

// create magic string if it wasn't created already
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ test("doesn't throw if no files in dir when option isn't set", async (t) => {
t.false(thrown);
});

test('throws if no files in dir when option is set', async (t) => {
test('throws if no files in dir when `errorWhenNoFilesFound` is set', async (t) => {
let thrown = false;
try {
await rollup({
Expand All @@ -236,3 +236,21 @@ test('throws if no files in dir when option is set', async (t) => {
}
t.true(thrown);
});

test('warns if no files in dir when `errorWhenNoFilesFound` and `warnOnError` are both set', async (t) => {
let warningEmitted = false;
await rollup({
input: 'fixture-no-files.js',
plugins: [dynamicImportVars({ errorWhenNoFilesFound: true, warnOnError: true })],
onwarn(warning) {
t.deepEqual(
warning.message,
`No files found in ./module-dir-c/*.js when trying to dynamically load concatted string from ${require.resolve(
'./fixtures/fixture-no-files.js'
)}`
);
warningEmitted = true;
}
});
t.true(warningEmitted);
});
Loading