-
Notifications
You must be signed in to change notification settings - Fork 599
/
index.js
132 lines (116 loc) 路 3.46 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
/**
* 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
*/
'use strict';
import type {BabelCoreOptions, BabelFileMetadata} from '@babel/core';
const {parseSync, transformFromAstSync} = require('@babel/core');
const nullthrows = require('nullthrows');
export type CustomTransformOptions = {
[string]: mixed,
__proto__: null,
...
};
export type TransformProfile = 'default' | 'hermes-stable' | 'hermes-canary';
type BabelTransformerOptions = $ReadOnly<{
customTransformOptions?: CustomTransformOptions,
dev: boolean,
enableBabelRCLookup?: boolean,
enableBabelRuntime: boolean | string,
extendsBabelConfigPath?: string,
experimentalImportSupport?: boolean,
hermesParser?: boolean,
hot: boolean,
inlineRequires: boolean,
nonInlinedRequires?: $ReadOnlyArray<string>,
minify: boolean,
unstable_disableES6Transforms?: boolean,
platform: ?string,
projectRoot: string,
publicPath: string,
unstable_transformProfile?: TransformProfile,
globalPrefix: string,
...
}>;
export type BabelTransformerArgs = $ReadOnly<{
filename: string,
options: BabelTransformerOptions,
plugins?: $PropertyType<BabelCoreOptions, 'plugins'>,
src: string,
}>;
export type BabelFileFunctionMapMetadata = $ReadOnly<{
names: $ReadOnlyArray<string>,
mappings: string,
}>;
export type MetroBabelFileMetadata = {
...BabelFileMetadata,
metro?: ?{
functionMap?: ?BabelFileFunctionMapMetadata,
...
},
...
};
export type BabelTransformer = {
transform: BabelTransformerArgs => {
ast: BabelNodeFile,
// Deprecated, will be removed in a future breaking release. Function maps
// will be generated by an input Babel plugin instead and written into
// `metadata` - transformers don't need to return them explicitly.
functionMap?: BabelFileFunctionMapMetadata,
metadata?: MetroBabelFileMetadata,
...
},
getCacheKey?: () => string,
};
function transform({filename, options, plugins, src}: BabelTransformerArgs) {
const OLD_BABEL_ENV = process.env.BABEL_ENV;
process.env.BABEL_ENV = options.dev
? 'development'
: process.env.BABEL_ENV || 'production';
try {
const babelConfig = {
caller: {name: 'metro', bundler: 'metro', platform: options.platform},
ast: true,
babelrc: options.enableBabelRCLookup,
code: false,
cwd: options.projectRoot,
highlightCode: true,
filename,
plugins,
sourceType: 'module',
// 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 = options.hermesParser
? require('hermes-parser').parse(src, {
babel: true,
sourceType: babelConfig.sourceType,
})
: parseSync(src, babelConfig);
const transformResult = transformFromAstSync<MetroBabelFileMetadata>(
sourceAst,
src,
babelConfig,
);
return {
ast: nullthrows(transformResult.ast),
metadata: transformResult.metadata,
};
} finally {
if (OLD_BABEL_ENV) {
process.env.BABEL_ENV = OLD_BABEL_ENV;
}
}
}
module.exports = ({
transform,
}: BabelTransformer);