From 6f41f686d5cac60ff6e195000a97aeb92b54c380 Mon Sep 17 00:00:00 2001 From: Nigel Sirisomphone Date: Tue, 20 Aug 2024 11:07:11 +1200 Subject: [PATCH 1/2] 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 6527ac60c83d8..c12bb1507e717 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); From a0f2358a136d79f43354294133979f3cde1cdda3 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Wed, 11 Dec 2024 10:07:19 +0000 Subject: [PATCH 2/2] fix(react): ensure rspack rule.test exists before .toString --- packages/rspack/src/plugins/utils/apply-react-config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rspack/src/plugins/utils/apply-react-config.ts b/packages/rspack/src/plugins/utils/apply-react-config.ts index acd6edc4024ac..4d116cedc4de7 100644 --- a/packages/rspack/src/plugins/utils/apply-react-config.ts +++ b/packages/rspack/src/plugins/utils/apply-react-config.ts @@ -47,7 +47,7 @@ function removeSvgLoaderIfPresent( config: Partial ) { const svgLoaderIdx = config.module.rules.findIndex( - (rule) => typeof rule === 'object' && rule.test.toString().includes('svg') + (rule) => typeof rule === 'object' && rule.test?.toString().includes('svg') ); if (svgLoaderIdx === -1) return; config.module.rules.splice(svgLoaderIdx, 1);