Skip to content

Commit

Permalink
fix(dynamic-import-vars): Allow a "no files found" error to be emitte…
Browse files Browse the repository at this point in the history
…d as warning (#1625)

* allow no files error to be emitted as warning

* Update packages/dynamic-import-vars/README.md

Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com>

---------

Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com>
  • Loading branch information
koddsson and shellscape committed Nov 28, 2023
1 parent b17e0c7 commit bacfedb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/dynamic-import-vars/README.md
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
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
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);
});

0 comments on commit bacfedb

Please sign in to comment.