Skip to content

Commit

Permalink
Filter out falsy values in plugins and middleware array
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Sep 5, 2021
1 parent 3537eb5 commit d75a2da
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-olives-call.md
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Fix error when `plugins` or `middleware` array contains falsy values in the parsed configuration. This typically happens due to conditionally setting items. All falsy values are now filtered out by default.
17 changes: 12 additions & 5 deletions packages/wmr/src/lib/normalize-options.js
Expand Up @@ -204,16 +204,23 @@ export async function normalizeOptions(options, mode, configWatchFiles = []) {
// Sort plugins by "enforce" phase. Default is "normal".
// The execution order is: "pre" -> "normal" -> "post"
if (options.plugins) {
options.plugins = options.plugins.flat().sort((a, b) => {
const aScore = a.enforce === 'post' ? 1 : a.enforce === 'pre' ? -1 : 0;
const bScore = b.enforce === 'post' ? 1 : b.enforce === 'pre' ? -1 : 0;
return aScore - bScore;
});
options.plugins = options.plugins
.flat()
// Filter out falsy values caused by conditionals
.filter(Boolean)
.sort((a, b) => {
const aScore = a.enforce === 'post' ? 1 : a.enforce === 'pre' ? -1 : 0;
const bScore = b.enforce === 'post' ? 1 : b.enforce === 'pre' ? -1 : 0;
return aScore - bScore;
});
}

await runConfigHook('config', options.plugins);
await runConfigHook('configResolved', options.plugins);

// Filter out falsy values caused by conditionals
options.middleware = options.middleware.filter(Boolean);

if (prevPublicFolder !== options.public) {
// If the CWD has a public/ directory, all files are assumed to be within it.
// From here, everything except node_modules and `out` are relative to public:
Expand Down
@@ -0,0 +1,2 @@
<p>Nothing to see here!</p>
<script src="index.js" type="module"></script>
Empty file.
17 changes: 17 additions & 0 deletions packages/wmr/test/fixtures/plugin-config-falsy/wmr.config.mjs
@@ -0,0 +1,17 @@
export default {
middleware: [false, undefined, null, 0],
plugins: [
false,
undefined,
null,
0,
{
name: 'foo',
configResolved() {
return {
middleware: [false, undefined, null, 0]
};
}
}
]
};
11 changes: 11 additions & 0 deletions packages/wmr/test/plugins/plugins.test.js
Expand Up @@ -76,4 +76,15 @@ describe('config', () => {
await waitForMessage(instance.output, 'configResolved() A');
await waitForMessage(instance.output, 'configResolved() C');
});

it('should filter out falsy values in arrays inside configuration', async () => {
await loadFixture('plugin-config-falsy', env);
instance = await runWmrFast(env.tmp.path);
await getOutput(env, instance);

await waitForMessage(
instance.output,
/plugins: \[ { name: 'foo', configResolved: \[Function: configResolved\] } \]/
);
});
});

0 comments on commit d75a2da

Please sign in to comment.