Skip to content

Commit bacfedb

Browse files
Kristján Oddssonshellscape
Kristján Oddsson
andauthored
fix(dynamic-import-vars): Allow a "no files found" error to be emitted as warning (#1625)
* allow no files error to be emitted as warning * Update packages/dynamic-import-vars/README.md Co-authored-by: Andrew Powell <[email protected]> --------- Co-authored-by: Andrew Powell <[email protected]>
1 parent b17e0c7 commit bacfedb

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

packages/dynamic-import-vars/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Default: `false`
6868

6969
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.
7070

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

7375
Type: `Boolean`<br>

packages/dynamic-import-vars/src/index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFile
5656
);
5757

5858
if (errorWhenNoFilesFound && paths.length === 0) {
59-
this.error(
60-
new Error(
61-
`No files found in ${glob} when trying to dynamically load concatted string from ${id}`
62-
)
59+
const error = new Error(
60+
`No files found in ${glob} when trying to dynamically load concatted string from ${id}`
6361
);
62+
if (warnOnError) {
63+
this.warn(error);
64+
} else {
65+
this.error(error);
66+
}
6467
}
6568

6669
// create magic string if it wasn't created already

packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ test("doesn't throw if no files in dir when option isn't set", async (t) => {
218218
t.false(thrown);
219219
});
220220

221-
test('throws if no files in dir when option is set', async (t) => {
221+
test('throws if no files in dir when `errorWhenNoFilesFound` is set', async (t) => {
222222
let thrown = false;
223223
try {
224224
await rollup({
@@ -236,3 +236,21 @@ test('throws if no files in dir when option is set', async (t) => {
236236
}
237237
t.true(thrown);
238238
});
239+
240+
test('warns if no files in dir when `errorWhenNoFilesFound` and `warnOnError` are both set', async (t) => {
241+
let warningEmitted = false;
242+
await rollup({
243+
input: 'fixture-no-files.js',
244+
plugins: [dynamicImportVars({ errorWhenNoFilesFound: true, warnOnError: true })],
245+
onwarn(warning) {
246+
t.deepEqual(
247+
warning.message,
248+
`No files found in ./module-dir-c/*.js when trying to dynamically load concatted string from ${require.resolve(
249+
'./fixtures/fixture-no-files.js'
250+
)}`
251+
);
252+
warningEmitted = true;
253+
}
254+
});
255+
t.true(warningEmitted);
256+
});

0 commit comments

Comments
 (0)