From 8b4a6babd99fe8f27fafb6c122889cb68c0e085e Mon Sep 17 00:00:00 2001 From: Tianyu Yao Date: Fri, 18 Nov 2022 18:21:23 -0800 Subject: [PATCH] Fix Jest cache for transform-react-version-pragma --- scripts/jest/devtools/preprocessor.js | 20 --------------- scripts/jest/preprocessor.js | 36 +++++++++++++++++++-------- 2 files changed, 25 insertions(+), 31 deletions(-) delete mode 100644 scripts/jest/devtools/preprocessor.js diff --git a/scripts/jest/devtools/preprocessor.js b/scripts/jest/devtools/preprocessor.js deleted file mode 100644 index b9dfb9937bc43..0000000000000 --- a/scripts/jest/devtools/preprocessor.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -const pathToTransformReactVersionPragma = require.resolve( - '../../babel/transform-react-version-pragma' -); - -function getDevToolsPlugins(filePath) { - const plugins = []; - if ( - process.env.REACT_VERSION || - filePath.match(/\/transform-react-version-pragma-test/) - ) { - plugins.push(pathToTransformReactVersionPragma); - } - return plugins; -} - -module.exports = { - getDevToolsPlugins, -}; diff --git a/scripts/jest/preprocessor.js b/scripts/jest/preprocessor.js index 43e8f18d02b82..d8f04b5bcbfb6 100644 --- a/scripts/jest/preprocessor.js +++ b/scripts/jest/preprocessor.js @@ -7,7 +7,6 @@ const coffee = require('coffee-script'); const tsPreprocessor = require('./typescript/preprocessor'); const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction'); -const {getDevToolsPlugins} = require('./devtools/preprocessor.js'); const pathToBabel = path.join( require.resolve('@babel/core'), @@ -26,6 +25,9 @@ const pathToTransformInfiniteLoops = require.resolve( const pathToTransformTestGatePragma = require.resolve( '../babel/transform-test-gate-pragma' ); +const pathToTransformReactVersionPragma = require.resolve( + '../babel/transform-react-version-pragma' +); const pathToBabelrc = path.join(__dirname, '..', '..', 'babel.config.js'); const pathToErrorCodes = require.resolve('../error-codes/codes.json'); @@ -83,8 +85,13 @@ module.exports = { const plugins = (isTestFile ? testOnlyPlugins : sourceOnlyPlugins).concat( babelOptions.plugins ); - if (isTestFile && isInDevToolsPackages) { - plugins.push(...getDevToolsPlugins(filePath)); + if ( + isTestFile && + isInDevToolsPackages && + (process.env.REACT_VERSION || + filePath.match(/\/transform-react-version-pragma-test/)) + ) { + plugins.push(pathToTransformReactVersionPragma); } return babel.transform( src, @@ -103,12 +110,19 @@ module.exports = { return src; }, - getCacheKey: createCacheKeyFunction([ - __filename, - pathToBabel, - pathToBabelrc, - pathToTransformInfiniteLoops, - pathToTransformTestGatePragma, - pathToErrorCodes, - ]), + getCacheKey: createCacheKeyFunction( + [ + __filename, + pathToBabel, + pathToBabelrc, + pathToTransformInfiniteLoops, + pathToTransformTestGatePragma, + pathToTransformReactVersionPragma, + pathToErrorCodes, + ], + [ + (process.env.REACT_VERSION != null).toString(), + (process.env.NODE_ENV === 'development').toString(), + ] + ), };