@react-native/metro-babel-transformer 0.73.4 → 0.73.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +245 -0
- package/package.json +8 -6
- package/BUCK +0 -16
- /package/{src/index.js → dist/index.js.flow} +0 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @format
|
|
9
|
+
* @oncall react_native
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// This file uses Flow comment syntax so that it may be used from source as a
|
|
13
|
+
// transformer without itself requiring transformation, such as in
|
|
14
|
+
// facebook/react-native's own tests.
|
|
15
|
+
|
|
16
|
+
"use strict";
|
|
17
|
+
|
|
18
|
+
/*::
|
|
19
|
+
import type {BabelCoreOptions, Plugins, TransformResult} from '@babel/core';
|
|
20
|
+
import type {
|
|
21
|
+
BabelTransformer,
|
|
22
|
+
MetroBabelFileMetadata,
|
|
23
|
+
} from 'metro-babel-transformer';
|
|
24
|
+
*/
|
|
25
|
+
const { parseSync, transformFromAstSync } = require("@babel/core");
|
|
26
|
+
const inlineRequiresPlugin = require("babel-preset-fbjs/plugins/inline-requires");
|
|
27
|
+
const crypto = require("crypto");
|
|
28
|
+
const fs = require("fs");
|
|
29
|
+
const makeHMRConfig = require("@react-native/babel-preset/src/configs/hmr");
|
|
30
|
+
const nullthrows = require("nullthrows");
|
|
31
|
+
const path = require("path");
|
|
32
|
+
const cacheKeyParts = [
|
|
33
|
+
fs.readFileSync(__filename),
|
|
34
|
+
require("babel-preset-fbjs/package.json").version,
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
// TS detection conditions copied from @react-native/babel-preset
|
|
38
|
+
function isTypeScriptSource(fileName /*: string */) {
|
|
39
|
+
return !!fileName && fileName.endsWith(".ts");
|
|
40
|
+
}
|
|
41
|
+
function isTSXSource(fileName /*: string */) {
|
|
42
|
+
return !!fileName && fileName.endsWith(".tsx");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Return a memoized function that checks for the existence of a
|
|
47
|
+
* project level .babelrc file, and if it doesn't exist, reads the
|
|
48
|
+
* default RN babelrc file and uses that.
|
|
49
|
+
*/
|
|
50
|
+
const getBabelRC = (function () {
|
|
51
|
+
let babelRC /*: ?BabelCoreOptions */ = null;
|
|
52
|
+
|
|
53
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
54
|
+
* LTI update could not be added via codemod */
|
|
55
|
+
return function _getBabelRC({
|
|
56
|
+
projectRoot,
|
|
57
|
+
extendsBabelConfigPath,
|
|
58
|
+
...options
|
|
59
|
+
}) {
|
|
60
|
+
if (babelRC != null) {
|
|
61
|
+
return babelRC;
|
|
62
|
+
}
|
|
63
|
+
babelRC = {
|
|
64
|
+
plugins: [],
|
|
65
|
+
extends: extendsBabelConfigPath,
|
|
66
|
+
};
|
|
67
|
+
if (extendsBabelConfigPath) {
|
|
68
|
+
return babelRC;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Let's look for a babel config file in the project root.
|
|
72
|
+
let projectBabelRCPath;
|
|
73
|
+
|
|
74
|
+
// .babelrc
|
|
75
|
+
if (projectRoot) {
|
|
76
|
+
projectBabelRCPath = path.resolve(projectRoot, ".babelrc");
|
|
77
|
+
}
|
|
78
|
+
if (projectBabelRCPath) {
|
|
79
|
+
// .babelrc.js
|
|
80
|
+
if (!fs.existsSync(projectBabelRCPath)) {
|
|
81
|
+
projectBabelRCPath = path.resolve(projectRoot, ".babelrc.js");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// babel.config.js
|
|
85
|
+
if (!fs.existsSync(projectBabelRCPath)) {
|
|
86
|
+
projectBabelRCPath = path.resolve(projectRoot, "babel.config.js");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// If we found a babel config file, extend our config off of it
|
|
90
|
+
// otherwise the default config will be used
|
|
91
|
+
if (fs.existsSync(projectBabelRCPath)) {
|
|
92
|
+
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
|
|
93
|
+
babelRC.extends = projectBabelRCPath;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// If a babel config file doesn't exist in the project then
|
|
98
|
+
// the default preset for react-native will be used instead.
|
|
99
|
+
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
|
|
100
|
+
// $FlowFixMe[incompatible-type] `extends` is missing in null or undefined.
|
|
101
|
+
if (!babelRC.extends) {
|
|
102
|
+
const { experimentalImportSupport, ...presetOptions } = options;
|
|
103
|
+
|
|
104
|
+
// $FlowFixMe[incompatible-use] `presets` is missing in null or undefined.
|
|
105
|
+
babelRC.presets = [
|
|
106
|
+
[
|
|
107
|
+
require("@react-native/babel-preset"),
|
|
108
|
+
{
|
|
109
|
+
projectRoot,
|
|
110
|
+
...presetOptions,
|
|
111
|
+
disableImportExportTransform: experimentalImportSupport,
|
|
112
|
+
enableBabelRuntime: options.enableBabelRuntime,
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
return babelRC;
|
|
118
|
+
};
|
|
119
|
+
})();
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Given a filename and options, build a Babel
|
|
123
|
+
* config object with the appropriate plugins.
|
|
124
|
+
*/
|
|
125
|
+
function buildBabelConfig(
|
|
126
|
+
filename /*: string */,
|
|
127
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
128
|
+
* LTI update could not be added via codemod */
|
|
129
|
+
options,
|
|
130
|
+
plugins /*:: ?: Plugins*/ = []
|
|
131
|
+
) /*: BabelCoreOptions*/ {
|
|
132
|
+
const babelRC = getBabelRC(options);
|
|
133
|
+
const extraConfig /*: BabelCoreOptions */ = {
|
|
134
|
+
babelrc:
|
|
135
|
+
typeof options.enableBabelRCLookup === "boolean"
|
|
136
|
+
? options.enableBabelRCLookup
|
|
137
|
+
: true,
|
|
138
|
+
code: false,
|
|
139
|
+
cwd: options.projectRoot,
|
|
140
|
+
filename,
|
|
141
|
+
highlightCode: true,
|
|
142
|
+
};
|
|
143
|
+
let config /*: BabelCoreOptions */ = {
|
|
144
|
+
...babelRC,
|
|
145
|
+
...extraConfig,
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// Add extra plugins
|
|
149
|
+
const extraPlugins = [];
|
|
150
|
+
if (options.inlineRequires) {
|
|
151
|
+
extraPlugins.push(inlineRequiresPlugin);
|
|
152
|
+
}
|
|
153
|
+
const withExtrPlugins = (config.plugins = extraPlugins.concat(
|
|
154
|
+
config.plugins,
|
|
155
|
+
plugins
|
|
156
|
+
));
|
|
157
|
+
if (options.dev && options.hot) {
|
|
158
|
+
// Note: this intentionally doesn't include the path separator because
|
|
159
|
+
// I'm not sure which one it should use on Windows, and false positives
|
|
160
|
+
// are unlikely anyway. If you later decide to include the separator,
|
|
161
|
+
// don't forget that the string usually *starts* with "node_modules" so
|
|
162
|
+
// the first one often won't be there.
|
|
163
|
+
const mayContainEditableReactComponents =
|
|
164
|
+
filename.indexOf("node_modules") === -1;
|
|
165
|
+
if (mayContainEditableReactComponents) {
|
|
166
|
+
const hmrConfig = makeHMRConfig();
|
|
167
|
+
hmrConfig.plugins = withExtrPlugins.concat(hmrConfig.plugins);
|
|
168
|
+
config = {
|
|
169
|
+
...config,
|
|
170
|
+
...hmrConfig,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
...babelRC,
|
|
176
|
+
...config,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const transform /*: BabelTransformer['transform'] */ = ({
|
|
180
|
+
filename,
|
|
181
|
+
options,
|
|
182
|
+
src,
|
|
183
|
+
plugins,
|
|
184
|
+
}) => {
|
|
185
|
+
const OLD_BABEL_ENV = process.env.BABEL_ENV;
|
|
186
|
+
process.env.BABEL_ENV = options.dev
|
|
187
|
+
? "development"
|
|
188
|
+
: process.env.BABEL_ENV || "production";
|
|
189
|
+
try {
|
|
190
|
+
const babelConfig = {
|
|
191
|
+
// ES modules require sourceType='module' but OSS may not always want that
|
|
192
|
+
sourceType: "unambiguous",
|
|
193
|
+
...buildBabelConfig(filename, options, plugins),
|
|
194
|
+
caller: {
|
|
195
|
+
name: "metro",
|
|
196
|
+
bundler: "metro",
|
|
197
|
+
platform: options.platform,
|
|
198
|
+
},
|
|
199
|
+
ast: true,
|
|
200
|
+
// NOTE(EvanBacon): We split the parse/transform steps up to accommodate
|
|
201
|
+
// Hermes parsing, but this defaults to cloning the AST which increases
|
|
202
|
+
// the transformation time by a fair amount.
|
|
203
|
+
// You get this behavior by default when using Babel's `transform` method directly.
|
|
204
|
+
cloneInputAst: false,
|
|
205
|
+
};
|
|
206
|
+
const sourceAst =
|
|
207
|
+
isTypeScriptSource(filename) ||
|
|
208
|
+
isTSXSource(filename) ||
|
|
209
|
+
!options.hermesParser
|
|
210
|
+
? parseSync(src, babelConfig)
|
|
211
|
+
: require("hermes-parser").parse(src, {
|
|
212
|
+
babel: true,
|
|
213
|
+
sourceType: babelConfig.sourceType,
|
|
214
|
+
});
|
|
215
|
+
const result /*: TransformResult<MetroBabelFileMetadata> */ =
|
|
216
|
+
transformFromAstSync(sourceAst, src, babelConfig);
|
|
217
|
+
|
|
218
|
+
// The result from `transformFromAstSync` can be null (if the file is ignored)
|
|
219
|
+
if (!result) {
|
|
220
|
+
/* $FlowFixMe BabelTransformer specifies that the `ast` can never be null but
|
|
221
|
+
* the function returns here. Discovered when typing `BabelNode`. */
|
|
222
|
+
return {
|
|
223
|
+
ast: null,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
ast: nullthrows(result.ast),
|
|
228
|
+
metadata: result.metadata,
|
|
229
|
+
};
|
|
230
|
+
} finally {
|
|
231
|
+
if (OLD_BABEL_ENV) {
|
|
232
|
+
process.env.BABEL_ENV = OLD_BABEL_ENV;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
function getCacheKey() {
|
|
237
|
+
var key = crypto.createHash("md5");
|
|
238
|
+
cacheKeyParts.forEach((part) => key.update(part));
|
|
239
|
+
return key.digest("hex");
|
|
240
|
+
}
|
|
241
|
+
const babelTransformer /*: BabelTransformer */ = {
|
|
242
|
+
transform,
|
|
243
|
+
getCacheKey,
|
|
244
|
+
};
|
|
245
|
+
module.exports = babelTransformer;
|
package/package.json
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native/metro-babel-transformer",
|
|
3
|
-
"version": "0.73.
|
|
3
|
+
"version": "0.73.6",
|
|
4
4
|
"description": "Babel transformer for React Native applications.",
|
|
5
|
-
"
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./dist/index.js",
|
|
7
|
+
"./package.json": "./package.json"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
6
12
|
"repository": {
|
|
7
13
|
"type": "git",
|
|
8
14
|
"url": "git@github.com:facebook/react-native.git",
|
|
9
15
|
"directory": "packages/react-native-babel-transformer"
|
|
10
16
|
},
|
|
11
|
-
"scripts": {
|
|
12
|
-
"prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src",
|
|
13
|
-
"cleanup-release": "test ! -e build && mv src build && mv src.real src"
|
|
14
|
-
},
|
|
15
17
|
"keywords": [
|
|
16
18
|
"transformer",
|
|
17
19
|
"react-native",
|
package/BUCK
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
load("@fbsource//tools/build_defs/third_party:yarn_defs.bzl", "yarn_workspace")
|
|
2
|
-
|
|
3
|
-
oncall("react_native")
|
|
4
|
-
|
|
5
|
-
yarn_workspace(
|
|
6
|
-
name = "yarn-workspace",
|
|
7
|
-
srcs = glob(
|
|
8
|
-
["src/**/*.js"],
|
|
9
|
-
exclude = [
|
|
10
|
-
"**/__fixtures__/**",
|
|
11
|
-
"**/__mocks__/**",
|
|
12
|
-
"**/__tests__/**",
|
|
13
|
-
],
|
|
14
|
-
),
|
|
15
|
-
visibility = ["PUBLIC"],
|
|
16
|
-
)
|
|
File without changes
|