From 0f66f358d9a5bb495e32f410ebeddaa9112d7cd9 Mon Sep 17 00:00:00 2001 From: Nigel Sirisomphone Date: Tue, 20 Aug 2024 11:07:11 +1200 Subject: [PATCH] fix(nx-plugin): assert test property is defined on webpack rule in nx-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()`. --- .../nx-react-webpack-plugin/lib/apply-react-config.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/react/plugins/nx-react-webpack-plugin/lib/apply-react-config.ts b/packages/react/plugins/nx-react-webpack-plugin/lib/apply-react-config.ts index 9e913092569002..f4573ef312f9e8 100644 --- a/packages/react/plugins/nx-react-webpack-plugin/lib/apply-react-config.ts +++ b/packages/react/plugins/nx-react-webpack-plugin/lib/apply-react-config.ts @@ -101,7 +101,10 @@ function removeSvgLoaderIfPresent( config: Partial ) { 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);