Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.

When used in combination for `warnOnError` it will throw a warning instead.

#### `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);
});