Skip to content

Commit

Permalink
fix(nx-plugin): assert test property is defined on webpack rule in nx…
Browse files Browse the repository at this point in the history
…-react-webpack-plugin

The `removeSvgLoaderIfPresent` method in `apply-react-config.ts` iterates through each Webpack rule,
calls `toString` on each `test` property, and checks for the presence of the string `"svg"` to see
if any existing SVG plugins need to be removed.

Some of the Webpack rules in the config don't have the test property, and calling `toString` without
asserting the test property is defined first throws type errors.

This commit introduces a very small change that simply asserts that `rule.test` is not `undefined`
before calling `.toString()`.
  • Loading branch information
nigelwtf committed Aug 19, 2024
1 parent 7c45589 commit 0f66f35
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ function removeSvgLoaderIfPresent(
config: Partial<WebpackOptionsNormalized | Configuration>
) {
const svgLoaderIdx = config.module.rules.findIndex(
(rule) => typeof rule === 'object' && rule.test.toString().includes('svg')
(rule) =>
typeof rule === 'object' &&
typeof rule.test !== 'undefined' &&
rule.test.toString().includes('svg')
);
if (svgLoaderIdx === -1) return;
config.module.rules.splice(svgLoaderIdx, 1);
Expand Down

0 comments on commit 0f66f35

Please sign in to comment.