/
index.js
245 lines (210 loc) · 7.08 KB
/
index.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall react_native
*/
// This file uses Flow comment syntax so that it may be used from source as a
// transformer without itself requiring transformation, such as in
// facebook/react-native's own tests.
'use strict';
/*::
import type {BabelCoreOptions, Plugins, TransformResult} from '@babel/core';
import type {
BabelTransformer,
MetroBabelFileMetadata,
} from 'metro-babel-transformer';
*/
const {parseSync, transformFromAstSync} = require('@babel/core');
const makeHMRConfig = require('@react-native/babel-preset/src/configs/hmr');
const crypto = require('crypto');
const fs = require('fs');
const nullthrows = require('nullthrows');
const path = require('path');
const cacheKeyParts = [fs.readFileSync(__filename)];
// TS detection conditions copied from @react-native/babel-preset
function isTypeScriptSource(fileName /*: string */) {
return !!fileName && fileName.endsWith('.ts');
}
function isTSXSource(fileName /*: string */) {
return !!fileName && fileName.endsWith('.tsx');
}
/**
* Return a memoized function that checks for the existence of a
* project level .babelrc file, and if it doesn't exist, reads the
* default RN babelrc file and uses that.
*/
const getBabelRC = (function () {
let babelRC /*: ?BabelCoreOptions */ = null;
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
return function _getBabelRC({
projectRoot,
extendsBabelConfigPath,
...options
}) {
if (babelRC != null) {
return babelRC;
}
babelRC = {
plugins: [],
extends: extendsBabelConfigPath,
};
if (extendsBabelConfigPath) {
return babelRC;
}
// Let's look for a babel config file in the project root.
let projectBabelRCPath;
// .babelrc
if (projectRoot) {
projectBabelRCPath = path.resolve(projectRoot, '.babelrc');
}
if (projectBabelRCPath) {
// .babelrc.js
if (!fs.existsSync(projectBabelRCPath)) {
projectBabelRCPath = path.resolve(projectRoot, '.babelrc.js');
}
// babel.config.js
if (!fs.existsSync(projectBabelRCPath)) {
projectBabelRCPath = path.resolve(projectRoot, 'babel.config.js');
}
// If we found a babel config file, extend our config off of it
// otherwise the default config will be used
if (fs.existsSync(projectBabelRCPath)) {
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
babelRC.extends = projectBabelRCPath;
}
}
// If a babel config file doesn't exist in the project then
// the default preset for react-native will be used instead.
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
// $FlowFixMe[incompatible-type] `extends` is missing in null or undefined.
if (!babelRC.extends) {
const {experimentalImportSupport, ...presetOptions} = options;
// $FlowFixMe[incompatible-use] `presets` is missing in null or undefined.
babelRC.presets = [
[
require('@react-native/babel-preset'),
{
projectRoot,
...presetOptions,
disableImportExportTransform: experimentalImportSupport,
enableBabelRuntime: options.enableBabelRuntime,
},
],
];
}
return babelRC;
};
})();
/**
* Given a filename and options, build a Babel
* config object with the appropriate plugins.
*/
function buildBabelConfig(
filename /*: string */,
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
options,
plugins /*:: ?: Plugins*/ = [],
) /*: BabelCoreOptions*/ {
const babelRC = getBabelRC(options);
const extraConfig /*: BabelCoreOptions */ = {
babelrc:
typeof options.enableBabelRCLookup === 'boolean'
? options.enableBabelRCLookup
: true,
code: false,
cwd: options.projectRoot,
filename,
highlightCode: true,
};
let config /*: BabelCoreOptions */ = {
...babelRC,
...extraConfig,
};
const withExtraPlugins = (config.plugins = [
...(config.plugins ?? []),
...plugins,
]);
if (options.dev && options.hot) {
// Note: this intentionally doesn't include the path separator because
// I'm not sure which one it should use on Windows, and false positives
// are unlikely anyway. If you later decide to include the separator,
// don't forget that the string usually *starts* with "node_modules" so
// the first one often won't be there.
const mayContainEditableReactComponents =
filename.indexOf('node_modules') === -1;
if (mayContainEditableReactComponents) {
const hmrConfig = makeHMRConfig();
hmrConfig.plugins = withExtraPlugins.concat(hmrConfig.plugins);
config = {...config, ...hmrConfig};
}
}
return {
...babelRC,
...config,
};
}
const transform /*: BabelTransformer['transform'] */ = ({
filename,
options,
src,
plugins,
}) => {
const OLD_BABEL_ENV = process.env.BABEL_ENV;
process.env.BABEL_ENV = options.dev
? 'development'
: process.env.BABEL_ENV || 'production';
try {
const babelConfig = {
// ES modules require sourceType='module' but OSS may not always want that
sourceType: 'unambiguous',
...buildBabelConfig(filename, options, plugins),
caller: {name: 'metro', bundler: 'metro', platform: options.platform},
ast: true,
// NOTE(EvanBacon): We split the parse/transform steps up to accommodate
// Hermes parsing, but this defaults to cloning the AST which increases
// the transformation time by a fair amount.
// You get this behavior by default when using Babel's `transform` method directly.
cloneInputAst: false,
};
const sourceAst /*: BabelNodeFile */ =
isTypeScriptSource(filename) ||
isTSXSource(filename) ||
!options.hermesParser
? parseSync(src, babelConfig)
: // $FlowFixMe[incompatible-exact]
require('hermes-parser').parse(src, {
babel: true,
sourceType: babelConfig.sourceType,
});
const result /*: TransformResult<MetroBabelFileMetadata> */ =
transformFromAstSync(sourceAst, src, babelConfig);
// The result from `transformFromAstSync` can be null (if the file is ignored)
if (!result) {
/* $FlowFixMe BabelTransformer specifies that the `ast` can never be null but
* the function returns here. Discovered when typing `BabelNode`. */
return {ast: null};
}
return {ast: nullthrows(result.ast), metadata: result.metadata};
} finally {
if (OLD_BABEL_ENV) {
process.env.BABEL_ENV = OLD_BABEL_ENV;
}
}
};
function getCacheKey() {
var key = crypto.createHash('md5');
cacheKeyParts.forEach(part => key.update(part));
return key.digest('hex');
}
const babelTransformer /*: BabelTransformer */ = {
transform,
getCacheKey,
};
module.exports = babelTransformer;