From 41b256a776f4c33f1824f9a2a3668ebc4cc79453 Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Sat, 27 Jan 2024 06:49:15 -0800 Subject: [PATCH] Babel-ignore `node_modules` on Windows consistently with others (#42681) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42681 CI failures in Windows JS tests recently (https://github.com/facebook/react-native/pull/41463) were caused by the triggering of Babel registration during tests, due to an import of `packages/dev-middleware` (index), breaking subsequent transformation of other tests. ## Root cause Example of a problematic import: https://github.com/facebook/react-native/blob/a5d8ea4579c630af1e4e0fe1d99ad9dc0915df86/packages/dev-middleware/src/__tests__/ServerUtils.js#L15 ..which triggers a Babel registration: https://github.com/facebook/react-native/blob/a5d8ea4579c630af1e4e0fe1d99ad9dc0915df86/packages/dev-middleware/src/index.js#L16-L18 That registration behaves differently on Windows due to the `ignore: [/\/node_modules\/\]`, which doesn't match against Windows path separators - Babel matches against system separators. In particular, this changed whether `node_modules/flow-parser` was transformed when loading the RN Babel transformer. Transforming this file causes a `console.warn` from Babel due to its size: > [BABEL] Note: The code generator has deoptimised the styling of /Users/robhogan/workspace/react-native/node_modules/flow-parser/flow_parser.js as it exceeds the max of 500KB. This throws due to our setup: https://github.com/facebook/react-native/blob/a5d8ea4579c630af1e4e0fe1d99ad9dc0915df86/packages/react-native/jest/local-setup.js#L27 This all manifests as the first test following a Babel registration (within the same Jest worker) that requires the RN Babel transformer throwing during script transformation. ## This change This is the minimally disruptive change that makes Babel registration behaviour consistent between Windows and other platforms. The more durable solution here would be *not* to rely on any Babel registration for Jest, which has its own `ScriptTransformer` mechanism for running code from source. Given the fragile way our internal+OSS Babel set up hangs together that's a higher-risk change, so I'll follow up separately. Changelog: [Internal] Reviewed By: huntie Differential Revision: D53124578 fbshipit-source-id: 074a8e139e506a5dceec13f07d412599fb292d92 --- scripts/build/babel-register.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build/babel-register.js b/scripts/build/babel-register.js index a9f99c4fdb429d..f2f1c4eeebb7a4 100644 --- a/scripts/build/babel-register.js +++ b/scripts/build/babel-register.js @@ -50,7 +50,7 @@ function registerPackage(packageName /*: string */) { const registerConfig = { ...getBabelConfig(packageName), root: packageDir, - ignore: [/\/node_modules\//], + ignore: [/[/\\]node_modules[/\\]/], }; require('@babel/register')(registerConfig);