@react-native/metro-babel-transformer 0.73.6 → 0.73.7
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/.babelrc +12 -0
- package/.prettierrc +8 -0
- package/BUCK +16 -0
- package/index.js.flow +12 -0
- package/package.json +28 -8
- package/scripts/build.js +114 -0
- package/src/index.js +346 -0
- package/dist/index.js +0 -245
- /package/{dist → src}/index.js.flow +0 -0
package/.babelrc
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"plugins": [
|
|
3
|
+
"@babel/plugin-proposal-object-rest-spread",
|
|
4
|
+
"@babel/plugin-transform-async-to-generator",
|
|
5
|
+
"@babel/plugin-transform-destructuring",
|
|
6
|
+
"@babel/plugin-transform-flow-strip-types",
|
|
7
|
+
"@babel/plugin-syntax-dynamic-import",
|
|
8
|
+
"@babel/plugin-proposal-class-properties",
|
|
9
|
+
"@babel/plugin-proposal-nullish-coalescing-operator",
|
|
10
|
+
"@babel/plugin-proposal-optional-chaining"
|
|
11
|
+
]
|
|
12
|
+
}
|
package/.prettierrc
ADDED
package/BUCK
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
)
|
package/index.js.flow
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
* @oncall react_native
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export * from './src';
|
package/package.json
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native/metro-babel-transformer",
|
|
3
|
-
"version": "0.73.
|
|
3
|
+
"version": "0.73.7",
|
|
4
4
|
"description": "Babel transformer for React Native applications.",
|
|
5
|
-
"
|
|
6
|
-
".": "./dist/index.js",
|
|
7
|
-
"./package.json": "./package.json"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"dist"
|
|
11
|
-
],
|
|
5
|
+
"main": "src/index.js",
|
|
12
6
|
"repository": {
|
|
13
7
|
"type": "git",
|
|
14
8
|
"url": "git@github.com:facebook/react-native.git",
|
|
15
9
|
"directory": "packages/react-native-babel-transformer"
|
|
16
10
|
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prepare-release": "test -d lib && mv src src.real && mv lib src",
|
|
13
|
+
"cleanup-release": "test ! -e lib && mv src lib && mv src.real src && rimraf lib",
|
|
14
|
+
"build-clean": "rimraf build && rimraf src.real",
|
|
15
|
+
"build": " node scripts/build.js --verbose",
|
|
16
|
+
"prepare": "yarn run build-clean && yarn run build && yarn run prepare-release",
|
|
17
|
+
"postpublish": "yarn run cleanup-release"
|
|
18
|
+
},
|
|
17
19
|
"keywords": [
|
|
18
20
|
"transformer",
|
|
19
21
|
"react-native",
|
|
@@ -27,6 +29,24 @@
|
|
|
27
29
|
"hermes-parser": "0.14.0",
|
|
28
30
|
"nullthrows": "^1.1.1"
|
|
29
31
|
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@babel/plugin-proposal-class-properties": "^7.18.0",
|
|
34
|
+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.0",
|
|
35
|
+
"@babel/plugin-proposal-object-rest-spread": "^7.20.0",
|
|
36
|
+
"@babel/plugin-proposal-optional-chaining": "^7.20.0",
|
|
37
|
+
"@babel/plugin-syntax-dynamic-import": "^7.8.0",
|
|
38
|
+
"@babel/plugin-transform-async-to-generator": "^7.20.0",
|
|
39
|
+
"@babel/plugin-transform-destructuring": "^7.20.0",
|
|
40
|
+
"@babel/plugin-transform-flow-strip-types": "^7.20.0",
|
|
41
|
+
"@babel/preset-env": "^7.20.0",
|
|
42
|
+
"chalk": "^4.0.0",
|
|
43
|
+
"glob": "^7.1.1",
|
|
44
|
+
"invariant": "^2.2.4",
|
|
45
|
+
"micromatch": "^4.0.4",
|
|
46
|
+
"mkdirp": "^0.5.1",
|
|
47
|
+
"prettier": "2.8.8",
|
|
48
|
+
"rimraf": "^3.0.2"
|
|
49
|
+
},
|
|
30
50
|
"peerDependencies": {
|
|
31
51
|
"@babel/core": "*"
|
|
32
52
|
},
|
package/scripts/build.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
* @format
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* script to build (transpile) files.
|
|
12
|
+
*
|
|
13
|
+
* Based off of the build script from Metro, and tweaked to run in just one
|
|
14
|
+
* package instead of in a monorepo. Just run `build.js` and the JS files in
|
|
15
|
+
* `src/` will be built in `lib/`, and the original source files will be copied
|
|
16
|
+
* over as `Example.js.flow`, so consumers of this module can still make use of
|
|
17
|
+
* type checking.
|
|
18
|
+
*
|
|
19
|
+
* Call this script with the `--verbose` flag to show the full output of this
|
|
20
|
+
* script.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
'use strict';
|
|
24
|
+
|
|
25
|
+
const babel = require('@babel/core');
|
|
26
|
+
const chalk = require('chalk');
|
|
27
|
+
const fs = require('fs');
|
|
28
|
+
const glob = require('glob');
|
|
29
|
+
const micromatch = require('micromatch');
|
|
30
|
+
const mkdirp = require('mkdirp');
|
|
31
|
+
const path = require('path');
|
|
32
|
+
const prettier = require('prettier');
|
|
33
|
+
const prettierConfig = JSON.parse(
|
|
34
|
+
fs.readFileSync(path.resolve(__dirname, '..', '.prettierrc'), 'utf8'),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const SRC_DIR = 'src';
|
|
38
|
+
const BUILD_DIR = 'lib';
|
|
39
|
+
const JS_FILES_PATTERN = '**/*.js';
|
|
40
|
+
const IGNORE_PATTERN = '**/__tests__/**';
|
|
41
|
+
const PACKAGE_DIR = path.resolve(__dirname, '../');
|
|
42
|
+
|
|
43
|
+
const fixedWidth = str => {
|
|
44
|
+
const WIDTH = 80;
|
|
45
|
+
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g')) || [str];
|
|
46
|
+
let lastString = strs[strs.length - 1];
|
|
47
|
+
if (lastString.length < WIDTH) {
|
|
48
|
+
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
|
|
49
|
+
}
|
|
50
|
+
return strs.slice(0, -1).concat(lastString).join('\n');
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function getBuildPath(file, buildFolder) {
|
|
54
|
+
const pkgSrcPath = path.resolve(PACKAGE_DIR, SRC_DIR);
|
|
55
|
+
const pkgBuildPath = path.resolve(PACKAGE_DIR, BUILD_DIR);
|
|
56
|
+
const relativeToSrcPath = path.relative(pkgSrcPath, file);
|
|
57
|
+
return path.resolve(pkgBuildPath, relativeToSrcPath);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function buildFile(file, silent) {
|
|
61
|
+
const destPath = getBuildPath(file, BUILD_DIR);
|
|
62
|
+
|
|
63
|
+
mkdirp.sync(path.dirname(destPath));
|
|
64
|
+
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
|
|
65
|
+
silent ||
|
|
66
|
+
process.stdout.write(
|
|
67
|
+
chalk.dim(' \u2022 ') +
|
|
68
|
+
path.relative(PACKAGE_DIR, file) +
|
|
69
|
+
' (ignore)\n',
|
|
70
|
+
);
|
|
71
|
+
} else if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
|
|
72
|
+
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
|
|
73
|
+
silent ||
|
|
74
|
+
process.stdout.write(
|
|
75
|
+
chalk.red(' \u2022 ') +
|
|
76
|
+
path.relative(PACKAGE_DIR, file) +
|
|
77
|
+
chalk.red(' \u21D2 ') +
|
|
78
|
+
path.relative(PACKAGE_DIR, destPath) +
|
|
79
|
+
' (copy)' +
|
|
80
|
+
'\n',
|
|
81
|
+
);
|
|
82
|
+
} else {
|
|
83
|
+
const transformed = prettier.format(
|
|
84
|
+
babel.transformFileSync(file, {}).code,
|
|
85
|
+
{
|
|
86
|
+
...prettierConfig,
|
|
87
|
+
parser: 'babel',
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
fs.writeFileSync(destPath, transformed);
|
|
91
|
+
const source = fs.readFileSync(file).toString('utf-8');
|
|
92
|
+
if (/@flow/.test(source)) {
|
|
93
|
+
fs.createReadStream(file).pipe(fs.createWriteStream(destPath + '.flow'));
|
|
94
|
+
}
|
|
95
|
+
silent ||
|
|
96
|
+
process.stdout.write(
|
|
97
|
+
chalk.green(' \u2022 ') +
|
|
98
|
+
path.relative(PACKAGE_DIR, file) +
|
|
99
|
+
chalk.green(' \u21D2 ') +
|
|
100
|
+
path.relative(PACKAGE_DIR, destPath) +
|
|
101
|
+
'\n',
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const srcDir = path.resolve(__dirname, '..', SRC_DIR);
|
|
107
|
+
const pattern = path.resolve(srcDir, '**/*');
|
|
108
|
+
const files = glob.sync(pattern, {nodir: true});
|
|
109
|
+
|
|
110
|
+
process.stdout.write(fixedWidth(`${path.basename(PACKAGE_DIR)}\n`));
|
|
111
|
+
|
|
112
|
+
files.forEach(file => buildFile(file, !process.argv.includes('--verbose')));
|
|
113
|
+
|
|
114
|
+
process.stdout.write(`[ ${chalk.green('OK')} ]\n`);
|
package/src/index.js
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
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 _excluded = ['projectRoot', 'extendsBabelConfigPath'],
|
|
26
|
+
_excluded2 = ['experimentalImportSupport'];
|
|
27
|
+
function ownKeys(object, enumerableOnly) {
|
|
28
|
+
var keys = Object.keys(object);
|
|
29
|
+
if (Object.getOwnPropertySymbols) {
|
|
30
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
31
|
+
enumerableOnly &&
|
|
32
|
+
(symbols = symbols.filter(function (sym) {
|
|
33
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
34
|
+
})),
|
|
35
|
+
keys.push.apply(keys, symbols);
|
|
36
|
+
}
|
|
37
|
+
return keys;
|
|
38
|
+
}
|
|
39
|
+
function _objectSpread(target) {
|
|
40
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
41
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
42
|
+
i % 2
|
|
43
|
+
? ownKeys(Object(source), !0).forEach(function (key) {
|
|
44
|
+
_defineProperty(target, key, source[key]);
|
|
45
|
+
})
|
|
46
|
+
: Object.getOwnPropertyDescriptors
|
|
47
|
+
? Object.defineProperties(
|
|
48
|
+
target,
|
|
49
|
+
Object.getOwnPropertyDescriptors(source),
|
|
50
|
+
)
|
|
51
|
+
: ownKeys(Object(source)).forEach(function (key) {
|
|
52
|
+
Object.defineProperty(
|
|
53
|
+
target,
|
|
54
|
+
key,
|
|
55
|
+
Object.getOwnPropertyDescriptor(source, key),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return target;
|
|
60
|
+
}
|
|
61
|
+
function _defineProperty(obj, key, value) {
|
|
62
|
+
key = _toPropertyKey(key);
|
|
63
|
+
if (key in obj) {
|
|
64
|
+
Object.defineProperty(obj, key, {
|
|
65
|
+
value: value,
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
});
|
|
70
|
+
} else {
|
|
71
|
+
obj[key] = value;
|
|
72
|
+
}
|
|
73
|
+
return obj;
|
|
74
|
+
}
|
|
75
|
+
function _toPropertyKey(arg) {
|
|
76
|
+
var key = _toPrimitive(arg, 'string');
|
|
77
|
+
return typeof key === 'symbol' ? key : String(key);
|
|
78
|
+
}
|
|
79
|
+
function _toPrimitive(input, hint) {
|
|
80
|
+
if (typeof input !== 'object' || input === null) return input;
|
|
81
|
+
var prim = input[Symbol.toPrimitive];
|
|
82
|
+
if (prim !== undefined) {
|
|
83
|
+
var res = prim.call(input, hint || 'default');
|
|
84
|
+
if (typeof res !== 'object') return res;
|
|
85
|
+
throw new TypeError('@@toPrimitive must return a primitive value.');
|
|
86
|
+
}
|
|
87
|
+
return (hint === 'string' ? String : Number)(input);
|
|
88
|
+
}
|
|
89
|
+
function _objectWithoutProperties(source, excluded) {
|
|
90
|
+
if (source == null) return {};
|
|
91
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
|
92
|
+
var key, i;
|
|
93
|
+
if (Object.getOwnPropertySymbols) {
|
|
94
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
95
|
+
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
|
96
|
+
key = sourceSymbolKeys[i];
|
|
97
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
98
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
99
|
+
target[key] = source[key];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return target;
|
|
103
|
+
}
|
|
104
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
105
|
+
if (source == null) return {};
|
|
106
|
+
var target = {};
|
|
107
|
+
var sourceKeys = Object.keys(source);
|
|
108
|
+
var key, i;
|
|
109
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
|
110
|
+
key = sourceKeys[i];
|
|
111
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
112
|
+
target[key] = source[key];
|
|
113
|
+
}
|
|
114
|
+
return target;
|
|
115
|
+
}
|
|
116
|
+
const _require = require('@babel/core'),
|
|
117
|
+
parseSync = _require.parseSync,
|
|
118
|
+
transformFromAstSync = _require.transformFromAstSync;
|
|
119
|
+
const inlineRequiresPlugin = require('babel-preset-fbjs/plugins/inline-requires');
|
|
120
|
+
const crypto = require('crypto');
|
|
121
|
+
const fs = require('fs');
|
|
122
|
+
const makeHMRConfig = require('@react-native/babel-preset/src/configs/hmr');
|
|
123
|
+
const nullthrows = require('nullthrows');
|
|
124
|
+
const path = require('path');
|
|
125
|
+
const cacheKeyParts = [
|
|
126
|
+
fs.readFileSync(__filename),
|
|
127
|
+
require('babel-preset-fbjs/package.json').version,
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
// TS detection conditions copied from @react-native/babel-preset
|
|
131
|
+
function isTypeScriptSource(fileName /*: string */) {
|
|
132
|
+
return !!fileName && fileName.endsWith('.ts');
|
|
133
|
+
}
|
|
134
|
+
function isTSXSource(fileName /*: string */) {
|
|
135
|
+
return !!fileName && fileName.endsWith('.tsx');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Return a memoized function that checks for the existence of a
|
|
140
|
+
* project level .babelrc file, and if it doesn't exist, reads the
|
|
141
|
+
* default RN babelrc file and uses that.
|
|
142
|
+
*/
|
|
143
|
+
const getBabelRC = (function () {
|
|
144
|
+
let babelRC /*: ?BabelCoreOptions */ = null;
|
|
145
|
+
|
|
146
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
147
|
+
* LTI update could not be added via codemod */
|
|
148
|
+
return function _getBabelRC(_ref) {
|
|
149
|
+
let projectRoot = _ref.projectRoot,
|
|
150
|
+
extendsBabelConfigPath = _ref.extendsBabelConfigPath,
|
|
151
|
+
options = _objectWithoutProperties(_ref, _excluded);
|
|
152
|
+
if (babelRC != null) {
|
|
153
|
+
return babelRC;
|
|
154
|
+
}
|
|
155
|
+
babelRC = {
|
|
156
|
+
plugins: [],
|
|
157
|
+
extends: extendsBabelConfigPath,
|
|
158
|
+
};
|
|
159
|
+
if (extendsBabelConfigPath) {
|
|
160
|
+
return babelRC;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Let's look for a babel config file in the project root.
|
|
164
|
+
let projectBabelRCPath;
|
|
165
|
+
|
|
166
|
+
// .babelrc
|
|
167
|
+
if (projectRoot) {
|
|
168
|
+
projectBabelRCPath = path.resolve(projectRoot, '.babelrc');
|
|
169
|
+
}
|
|
170
|
+
if (projectBabelRCPath) {
|
|
171
|
+
// .babelrc.js
|
|
172
|
+
if (!fs.existsSync(projectBabelRCPath)) {
|
|
173
|
+
projectBabelRCPath = path.resolve(projectRoot, '.babelrc.js');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// babel.config.js
|
|
177
|
+
if (!fs.existsSync(projectBabelRCPath)) {
|
|
178
|
+
projectBabelRCPath = path.resolve(projectRoot, 'babel.config.js');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// If we found a babel config file, extend our config off of it
|
|
182
|
+
// otherwise the default config will be used
|
|
183
|
+
if (fs.existsSync(projectBabelRCPath)) {
|
|
184
|
+
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
|
|
185
|
+
babelRC.extends = projectBabelRCPath;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// If a babel config file doesn't exist in the project then
|
|
190
|
+
// the default preset for react-native will be used instead.
|
|
191
|
+
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
|
|
192
|
+
// $FlowFixMe[incompatible-type] `extends` is missing in null or undefined.
|
|
193
|
+
if (!babelRC.extends) {
|
|
194
|
+
const experimentalImportSupport = options.experimentalImportSupport,
|
|
195
|
+
presetOptions = _objectWithoutProperties(options, _excluded2);
|
|
196
|
+
|
|
197
|
+
// $FlowFixMe[incompatible-use] `presets` is missing in null or undefined.
|
|
198
|
+
babelRC.presets = [
|
|
199
|
+
[
|
|
200
|
+
require('@react-native/babel-preset'),
|
|
201
|
+
_objectSpread(
|
|
202
|
+
_objectSpread(
|
|
203
|
+
{
|
|
204
|
+
projectRoot,
|
|
205
|
+
},
|
|
206
|
+
presetOptions,
|
|
207
|
+
),
|
|
208
|
+
{},
|
|
209
|
+
{
|
|
210
|
+
disableImportExportTransform: experimentalImportSupport,
|
|
211
|
+
enableBabelRuntime: options.enableBabelRuntime,
|
|
212
|
+
},
|
|
213
|
+
),
|
|
214
|
+
],
|
|
215
|
+
];
|
|
216
|
+
}
|
|
217
|
+
return babelRC;
|
|
218
|
+
};
|
|
219
|
+
})();
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Given a filename and options, build a Babel
|
|
223
|
+
* config object with the appropriate plugins.
|
|
224
|
+
*/
|
|
225
|
+
function buildBabelConfig(
|
|
226
|
+
filename /*: string */,
|
|
227
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
228
|
+
* LTI update could not be added via codemod */
|
|
229
|
+
options,
|
|
230
|
+
plugins /*:: ?: Plugins*/ = [],
|
|
231
|
+
) /*: BabelCoreOptions*/ {
|
|
232
|
+
const babelRC = getBabelRC(options);
|
|
233
|
+
const extraConfig /*: BabelCoreOptions */ = {
|
|
234
|
+
babelrc:
|
|
235
|
+
typeof options.enableBabelRCLookup === 'boolean'
|
|
236
|
+
? options.enableBabelRCLookup
|
|
237
|
+
: true,
|
|
238
|
+
code: false,
|
|
239
|
+
cwd: options.projectRoot,
|
|
240
|
+
filename,
|
|
241
|
+
highlightCode: true,
|
|
242
|
+
};
|
|
243
|
+
let config /*: BabelCoreOptions */ = _objectSpread(
|
|
244
|
+
_objectSpread({}, babelRC),
|
|
245
|
+
extraConfig,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
// Add extra plugins
|
|
249
|
+
const extraPlugins = [];
|
|
250
|
+
if (options.inlineRequires) {
|
|
251
|
+
extraPlugins.push(inlineRequiresPlugin);
|
|
252
|
+
}
|
|
253
|
+
const withExtrPlugins = (config.plugins = extraPlugins.concat(
|
|
254
|
+
config.plugins,
|
|
255
|
+
plugins,
|
|
256
|
+
));
|
|
257
|
+
if (options.dev && options.hot) {
|
|
258
|
+
// Note: this intentionally doesn't include the path separator because
|
|
259
|
+
// I'm not sure which one it should use on Windows, and false positives
|
|
260
|
+
// are unlikely anyway. If you later decide to include the separator,
|
|
261
|
+
// don't forget that the string usually *starts* with "node_modules" so
|
|
262
|
+
// the first one often won't be there.
|
|
263
|
+
const mayContainEditableReactComponents =
|
|
264
|
+
filename.indexOf('node_modules') === -1;
|
|
265
|
+
if (mayContainEditableReactComponents) {
|
|
266
|
+
const hmrConfig = makeHMRConfig();
|
|
267
|
+
hmrConfig.plugins = withExtrPlugins.concat(hmrConfig.plugins);
|
|
268
|
+
config = _objectSpread(_objectSpread({}, config), hmrConfig);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return _objectSpread(_objectSpread({}, babelRC), config);
|
|
272
|
+
}
|
|
273
|
+
const transform /*: BabelTransformer['transform'] */ = ({
|
|
274
|
+
filename,
|
|
275
|
+
options,
|
|
276
|
+
src,
|
|
277
|
+
plugins,
|
|
278
|
+
}) => {
|
|
279
|
+
const OLD_BABEL_ENV = process.env.BABEL_ENV;
|
|
280
|
+
process.env.BABEL_ENV = options.dev
|
|
281
|
+
? 'development'
|
|
282
|
+
: process.env.BABEL_ENV || 'production';
|
|
283
|
+
try {
|
|
284
|
+
const babelConfig = _objectSpread(
|
|
285
|
+
_objectSpread(
|
|
286
|
+
{
|
|
287
|
+
// ES modules require sourceType='module' but OSS may not always want that
|
|
288
|
+
sourceType: 'unambiguous',
|
|
289
|
+
},
|
|
290
|
+
buildBabelConfig(filename, options, plugins),
|
|
291
|
+
),
|
|
292
|
+
{},
|
|
293
|
+
{
|
|
294
|
+
caller: {
|
|
295
|
+
name: 'metro',
|
|
296
|
+
bundler: 'metro',
|
|
297
|
+
platform: options.platform,
|
|
298
|
+
},
|
|
299
|
+
ast: true,
|
|
300
|
+
// NOTE(EvanBacon): We split the parse/transform steps up to accommodate
|
|
301
|
+
// Hermes parsing, but this defaults to cloning the AST which increases
|
|
302
|
+
// the transformation time by a fair amount.
|
|
303
|
+
// You get this behavior by default when using Babel's `transform` method directly.
|
|
304
|
+
cloneInputAst: false,
|
|
305
|
+
},
|
|
306
|
+
);
|
|
307
|
+
const sourceAst =
|
|
308
|
+
isTypeScriptSource(filename) ||
|
|
309
|
+
isTSXSource(filename) ||
|
|
310
|
+
!options.hermesParser
|
|
311
|
+
? parseSync(src, babelConfig)
|
|
312
|
+
: require('hermes-parser').parse(src, {
|
|
313
|
+
babel: true,
|
|
314
|
+
sourceType: babelConfig.sourceType,
|
|
315
|
+
});
|
|
316
|
+
const result /*: TransformResult<MetroBabelFileMetadata> */ =
|
|
317
|
+
transformFromAstSync(sourceAst, src, babelConfig);
|
|
318
|
+
|
|
319
|
+
// The result from `transformFromAstSync` can be null (if the file is ignored)
|
|
320
|
+
if (!result) {
|
|
321
|
+
/* $FlowFixMe BabelTransformer specifies that the `ast` can never be null but
|
|
322
|
+
* the function returns here. Discovered when typing `BabelNode`. */
|
|
323
|
+
return {
|
|
324
|
+
ast: null,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
ast: nullthrows(result.ast),
|
|
329
|
+
metadata: result.metadata,
|
|
330
|
+
};
|
|
331
|
+
} finally {
|
|
332
|
+
if (OLD_BABEL_ENV) {
|
|
333
|
+
process.env.BABEL_ENV = OLD_BABEL_ENV;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
function getCacheKey() {
|
|
338
|
+
var key = crypto.createHash('md5');
|
|
339
|
+
cacheKeyParts.forEach(part => key.update(part));
|
|
340
|
+
return key.digest('hex');
|
|
341
|
+
}
|
|
342
|
+
const babelTransformer /*: BabelTransformer */ = {
|
|
343
|
+
transform,
|
|
344
|
+
getCacheKey,
|
|
345
|
+
};
|
|
346
|
+
module.exports = babelTransformer;
|
package/dist/index.js
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
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;
|
|
File without changes
|