forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.js
More file actions
145 lines (132 loc) · 4.63 KB
/
preprocessor.js
File metadata and controls
145 lines (132 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
const path = require('path');
const babel = require('@babel/core');
const coffee = require('coffee-script');
const hermesParser = require('hermes-parser');
const tsPreprocessor = require('./typescript/preprocessor');
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
const {ReactVersion} = require('../../ReactVersions');
const semver = require('semver');
const pathToBabel = path.join(
require.resolve('@babel/core'),
'../..',
'package.json'
);
const pathToTransformInfiniteLoops = require.resolve(
'../babel/transform-prevent-infinite-loops'
);
const pathToTransformTestGatePragma = require.resolve(
'../babel/transform-test-gate-pragma'
);
const pathToTransformReactVersionPragma = require.resolve(
'../babel/transform-react-version-pragma'
);
const pathToTransformLazyJSXImport = require.resolve(
'../babel/transform-lazy-jsx-import'
);
const pathToBabelrc = path.join(__dirname, '..', '..', 'babel.config.js');
const pathToErrorCodes = require.resolve('../error-codes/codes.json');
const ReactVersionTestingAgainst = process.env.REACT_VERSION || ReactVersion;
const babelOptions = {
plugins: [
// For Node environment only. For builds, Rollup takes care of ESM.
require.resolve('@babel/plugin-transform-modules-commonjs'),
pathToTransformInfiniteLoops,
pathToTransformTestGatePragma,
// This optimization is important for extremely performance-sensitive (e.g. React source).
// It's okay to disable it for tests.
[
require.resolve('@babel/plugin-transform-block-scoping'),
{throwIfClosureRequired: false},
],
],
retainLines: true,
};
module.exports = {
process: function (src, filePath) {
if (filePath.match(/\.css$/)) {
// Don't try to parse CSS modules; they aren't needed for tests anyway.
return {code: ''};
}
if (filePath.match(/\.coffee$/)) {
return {code: coffee.compile(src, {bare: true})};
}
if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
return {code: tsPreprocessor.compile(src, filePath)};
}
if (filePath.match(/\.json$/)) {
return {code: src};
}
if (!filePath.match(/\/third_party\//)) {
// for test files, we also apply the async-await transform, but we want to
// make sure we don't accidentally apply that transform to product code.
const isTestFile = !!filePath.match(/\/__tests__\//);
const isInDevToolsPackages = !!filePath.match(
/\/packages\/react-devtools.*\//
);
const plugins = [].concat(babelOptions.plugins);
if (isTestFile && isInDevToolsPackages) {
plugins.push(pathToTransformReactVersionPragma);
}
// This is only for React DevTools tests with React 16.x
// `react/jsx-dev-runtime` and `react/jsx-runtime` are included in the package starting from v17
// Technically 16.14 and 15.7 have the new runtime but we're not testing those versions.
if (
semver.gte(ReactVersionTestingAgainst, '15.0.0') &&
semver.lt(ReactVersionTestingAgainst, '17.0.0')
) {
plugins.push(
[
require.resolve('@babel/plugin-transform-react-jsx'),
{runtime: 'classic'},
],
require.resolve('@babel/plugin-transform-react-jsx-source')
);
} else {
plugins.push([
process.env.NODE_ENV === 'development'
? require.resolve('@babel/plugin-transform-react-jsx-development')
: require.resolve('@babel/plugin-transform-react-jsx'),
// The "automatic" runtime corresponds to react/jsx-runtime. "classic"
// would be React.createElement.
{runtime: 'automatic'},
]);
}
plugins.push(pathToTransformLazyJSXImport);
let sourceAst = hermesParser.parse(src, {babel: true});
return {
code: babel.transformFromAstSync(
sourceAst,
src,
Object.assign(
{filename: path.relative(process.cwd(), filePath)},
babelOptions,
{
plugins,
sourceMaps: process.env.JEST_ENABLE_SOURCE_MAPS
? process.env.JEST_ENABLE_SOURCE_MAPS
: false,
}
)
).code,
};
}
return {code: src};
},
getCacheKey: createCacheKeyFunction(
[
__filename,
pathToBabel,
pathToBabelrc,
pathToTransformInfiniteLoops,
pathToTransformTestGatePragma,
pathToTransformReactVersionPragma,
pathToTransformLazyJSXImport,
pathToErrorCodes,
],
[
(process.env.REACT_VERSION != null).toString(),
(process.env.NODE_ENV === 'development').toString(),
]
),
};