metro-transform-worker 0.75.1 → 0.76.1
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/package.json +11 -12
- package/src/index.d.ts +120 -0
- package/src/index.js +7 -83
- package/src/index.js.flow +6 -104
- package/types/index.d.ts +120 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metro-transform-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.76.1",
|
|
4
4
|
"description": "🚇 Transform worker for Metro.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -18,21 +18,20 @@
|
|
|
18
18
|
"@babel/parser": "^7.20.0",
|
|
19
19
|
"@babel/types": "^7.20.0",
|
|
20
20
|
"babel-preset-fbjs": "^3.4.0",
|
|
21
|
-
"metro": "0.
|
|
22
|
-
"metro-babel-transformer": "0.
|
|
23
|
-
"metro-cache": "0.
|
|
24
|
-
"metro-cache-key": "0.
|
|
25
|
-
"metro-
|
|
26
|
-
"metro-
|
|
27
|
-
"metro-transform-plugins": "0.75.1",
|
|
21
|
+
"metro": "0.76.1",
|
|
22
|
+
"metro-babel-transformer": "0.76.1",
|
|
23
|
+
"metro-cache": "0.76.1",
|
|
24
|
+
"metro-cache-key": "0.76.1",
|
|
25
|
+
"metro-source-map": "0.76.1",
|
|
26
|
+
"metro-transform-plugins": "0.76.1",
|
|
28
27
|
"nullthrows": "^1.1.1"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
|
-
"metro-memory-fs": "0.
|
|
32
|
-
"metro-minify-terser": "0.
|
|
33
|
-
"metro-react-native-babel-transformer": "0.
|
|
30
|
+
"metro-memory-fs": "0.76.1",
|
|
31
|
+
"metro-minify-terser": "0.76.1",
|
|
32
|
+
"metro-react-native-babel-transformer": "0.76.1"
|
|
34
33
|
},
|
|
35
34
|
"engines": {
|
|
36
|
-
"node": ">=
|
|
35
|
+
"node": ">=16"
|
|
37
36
|
}
|
|
38
37
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
* @oncall react_native
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {DynamicRequiresBehavior} from 'metro';
|
|
12
|
+
import type {TransformResultDependency} from 'metro/DeltaBundler';
|
|
13
|
+
import type {AllowOptionalDependencies} from 'metro/DeltaBundler/types';
|
|
14
|
+
import type {
|
|
15
|
+
CustomTransformOptions,
|
|
16
|
+
TransformProfile,
|
|
17
|
+
} from 'metro-babel-transformer';
|
|
18
|
+
import type {
|
|
19
|
+
BasicSourceMap,
|
|
20
|
+
FBSourceFunctionMap,
|
|
21
|
+
MetroSourceMapSegmentTuple,
|
|
22
|
+
} from 'metro-source-map';
|
|
23
|
+
|
|
24
|
+
export type MinifierConfig = Readonly<Record<string, unknown>>;
|
|
25
|
+
|
|
26
|
+
export interface MinifierOptions {
|
|
27
|
+
code: string;
|
|
28
|
+
map?: BasicSourceMap;
|
|
29
|
+
filename: string;
|
|
30
|
+
reserved: ReadonlyArray<string>;
|
|
31
|
+
config: MinifierConfig;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MinifierResult {
|
|
35
|
+
code: string;
|
|
36
|
+
map?: BasicSourceMap;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type Minifier = (
|
|
40
|
+
options: MinifierOptions,
|
|
41
|
+
) => MinifierResult | Promise<MinifierResult>;
|
|
42
|
+
|
|
43
|
+
export type Type = 'script' | 'module' | 'asset';
|
|
44
|
+
|
|
45
|
+
export type JsTransformerConfig = Readonly<{
|
|
46
|
+
assetPlugins: ReadonlyArray<string>;
|
|
47
|
+
assetRegistryPath: string;
|
|
48
|
+
asyncRequireModulePath: string;
|
|
49
|
+
babelTransformerPath: string;
|
|
50
|
+
dynamicDepsInPackages: DynamicRequiresBehavior;
|
|
51
|
+
enableBabelRCLookup: boolean;
|
|
52
|
+
enableBabelRuntime: boolean;
|
|
53
|
+
globalPrefix: string;
|
|
54
|
+
hermesParser: boolean;
|
|
55
|
+
minifierConfig: MinifierConfig;
|
|
56
|
+
minifierPath: string;
|
|
57
|
+
optimizationSizeLimit: number;
|
|
58
|
+
publicPath: string;
|
|
59
|
+
allowOptionalDependencies: AllowOptionalDependencies;
|
|
60
|
+
unstable_collectDependenciesPath: string;
|
|
61
|
+
unstable_dependencyMapReservedName?: string;
|
|
62
|
+
unstable_disableModuleWrapping: boolean;
|
|
63
|
+
unstable_disableNormalizePseudoGlobals: boolean;
|
|
64
|
+
unstable_compactOutput: boolean;
|
|
65
|
+
/** Enable `require.context` statements which can be used to import multiple files in a directory. */
|
|
66
|
+
unstable_allowRequireContext: boolean;
|
|
67
|
+
}>;
|
|
68
|
+
|
|
69
|
+
export {CustomTransformOptions} from 'metro-babel-transformer';
|
|
70
|
+
|
|
71
|
+
export type JsTransformOptions = Readonly<{
|
|
72
|
+
customTransformOptions?: CustomTransformOptions;
|
|
73
|
+
dev: boolean;
|
|
74
|
+
experimentalImportSupport?: boolean;
|
|
75
|
+
hot: boolean;
|
|
76
|
+
inlinePlatform: boolean;
|
|
77
|
+
inlineRequires: boolean;
|
|
78
|
+
minify: boolean;
|
|
79
|
+
nonInlinedRequires?: ReadonlyArray<string>;
|
|
80
|
+
platform?: string;
|
|
81
|
+
runtimeBytecodeVersion?: number;
|
|
82
|
+
type: Type;
|
|
83
|
+
unstable_disableES6Transforms?: boolean;
|
|
84
|
+
unstable_transformProfile: TransformProfile;
|
|
85
|
+
}>;
|
|
86
|
+
|
|
87
|
+
export type BytecodeFileType =
|
|
88
|
+
| 'bytecode/module'
|
|
89
|
+
| 'bytecode/module/asset'
|
|
90
|
+
| 'bytecode/script';
|
|
91
|
+
|
|
92
|
+
export type JSFileType = 'js/script' | 'js/module' | 'js/module/asset';
|
|
93
|
+
|
|
94
|
+
export type JsOutput = Readonly<{
|
|
95
|
+
data: Readonly<{
|
|
96
|
+
code: string;
|
|
97
|
+
lineCount: number;
|
|
98
|
+
map: MetroSourceMapSegmentTuple[];
|
|
99
|
+
functionMap: FBSourceFunctionMap | null;
|
|
100
|
+
}>;
|
|
101
|
+
type: JSFileType;
|
|
102
|
+
}>;
|
|
103
|
+
|
|
104
|
+
// Hermes byte-code output type
|
|
105
|
+
export type BytecodeOutput = unknown;
|
|
106
|
+
|
|
107
|
+
export type TransformResponse = Readonly<{
|
|
108
|
+
dependencies: ReadonlyArray<TransformResultDependency>;
|
|
109
|
+
output: ReadonlyArray<JsOutput | BytecodeOutput>;
|
|
110
|
+
}>;
|
|
111
|
+
|
|
112
|
+
export function transform(
|
|
113
|
+
config: JsTransformerConfig,
|
|
114
|
+
projectRoot: string,
|
|
115
|
+
filename: string,
|
|
116
|
+
data: Buffer,
|
|
117
|
+
options: JsTransformOptions,
|
|
118
|
+
): Promise<TransformResponse>;
|
|
119
|
+
|
|
120
|
+
export function getCacheKey(config: JsTransformerConfig): string;
|
package/src/index.js
CHANGED
|
@@ -28,6 +28,7 @@ const countLines = require("metro/src/lib/countLines");
|
|
|
28
28
|
const {
|
|
29
29
|
InvalidRequireCallError: InternalInvalidRequireCallError,
|
|
30
30
|
} = require("metro/src/ModuleGraph/worker/collectDependencies");
|
|
31
|
+
const collectDependencies = require("metro/src/ModuleGraph/worker/collectDependencies");
|
|
31
32
|
const generateImportNames = require("metro/src/ModuleGraph/worker/generateImportNames");
|
|
32
33
|
const JsFileWrapping = require("metro/src/ModuleGraph/worker/JsFileWrapping");
|
|
33
34
|
const nullthrows = require("nullthrows");
|
|
@@ -87,22 +88,9 @@ const minifyCode = async (
|
|
|
87
88
|
throw error;
|
|
88
89
|
}
|
|
89
90
|
};
|
|
90
|
-
const compileToBytecode = (rawCode, type, options) => {
|
|
91
|
-
let code = rawCode;
|
|
92
|
-
if (type.startsWith("js/module")) {
|
|
93
|
-
const index = code.lastIndexOf(")");
|
|
94
|
-
code =
|
|
95
|
-
code.slice(0, index) +
|
|
96
|
-
",$$METRO_D[0],$$METRO_D[1],$$METRO_D[2]" +
|
|
97
|
-
code.slice(index);
|
|
98
|
-
}
|
|
99
|
-
const HermesCompiler = require("metro-hermes-compiler");
|
|
100
|
-
return HermesCompiler.compile(code, options);
|
|
101
|
-
};
|
|
102
91
|
const disabledDependencyTransformer = {
|
|
103
92
|
transformSyncRequire: () => void 0,
|
|
104
93
|
transformImportCall: () => void 0,
|
|
105
|
-
transformJSResource: () => void 0,
|
|
106
94
|
transformPrefetch: () => void 0,
|
|
107
95
|
transformIllegalDynamicRequire: () => void 0,
|
|
108
96
|
};
|
|
@@ -114,15 +102,13 @@ class InvalidRequireCallError extends Error {
|
|
|
114
102
|
}
|
|
115
103
|
}
|
|
116
104
|
async function transformJS(file, { config, options, projectRoot }) {
|
|
117
|
-
var _file$ast;
|
|
118
105
|
// Transformers can output null ASTs (if they ignore the file). In that case
|
|
119
106
|
// we need to parse the module source code to get their AST.
|
|
120
107
|
let ast =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
:
|
|
124
|
-
|
|
125
|
-
});
|
|
108
|
+
file.ast ??
|
|
109
|
+
babylon.parse(file.code, {
|
|
110
|
+
sourceType: "unambiguous",
|
|
111
|
+
});
|
|
126
112
|
const { importDefault, importAll } = generateImportNames(ast);
|
|
127
113
|
|
|
128
114
|
// Add "use strict" if the file was parsed as a module, and the directive did
|
|
@@ -226,8 +212,6 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
226
212
|
dependencyMapName: config.unstable_dependencyMapReservedName,
|
|
227
213
|
unstable_allowRequireContext: config.unstable_allowRequireContext,
|
|
228
214
|
};
|
|
229
|
-
// $FlowFixMe[unsupported-syntax] dynamic require
|
|
230
|
-
const collectDependencies = require(config.unstable_collectDependenciesPath);
|
|
231
215
|
({ ast, dependencies, dependencyMapName } = collectDependencies(
|
|
232
216
|
ast,
|
|
233
217
|
opts
|
|
@@ -305,23 +289,6 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
305
289
|
type: file.type,
|
|
306
290
|
},
|
|
307
291
|
];
|
|
308
|
-
if (options.runtimeBytecodeVersion != null) {
|
|
309
|
-
output.push({
|
|
310
|
-
data: compileToBytecode(code, file.type, {
|
|
311
|
-
sourceURL: file.filename,
|
|
312
|
-
sourceMap: fromRawMappings([
|
|
313
|
-
{
|
|
314
|
-
code,
|
|
315
|
-
source: file.code,
|
|
316
|
-
map,
|
|
317
|
-
functionMap: null,
|
|
318
|
-
path: file.filename,
|
|
319
|
-
},
|
|
320
|
-
]).toString(),
|
|
321
|
-
}),
|
|
322
|
-
type: getBytecodeFileType(file.type),
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
292
|
return {
|
|
326
293
|
dependencies,
|
|
327
294
|
output,
|
|
@@ -353,7 +320,6 @@ async function transformAsset(file, context) {
|
|
|
353
320
|
* the generic JavaScript transformation.
|
|
354
321
|
*/
|
|
355
322
|
async function transformJSWithBabel(file, context) {
|
|
356
|
-
var _transformResult$func;
|
|
357
323
|
const { babelTransformerPath } = context.config;
|
|
358
324
|
// $FlowFixMe[unsupported-syntax] dynamic require
|
|
359
325
|
const transformer = require(babelTransformerPath);
|
|
@@ -363,11 +329,7 @@ async function transformJSWithBabel(file, context) {
|
|
|
363
329
|
const jsFile = {
|
|
364
330
|
...file,
|
|
365
331
|
ast: transformResult.ast,
|
|
366
|
-
functionMap:
|
|
367
|
-
(_transformResult$func = transformResult.functionMap) !== null &&
|
|
368
|
-
_transformResult$func !== void 0
|
|
369
|
-
? _transformResult$func
|
|
370
|
-
: null,
|
|
332
|
+
functionMap: transformResult.functionMap ?? null,
|
|
371
333
|
};
|
|
372
334
|
return await transformJS(jsFile, context);
|
|
373
335
|
}
|
|
@@ -412,43 +374,11 @@ async function transformJSON(file, { options, config, projectRoot }) {
|
|
|
412
374
|
type: jsType,
|
|
413
375
|
},
|
|
414
376
|
];
|
|
415
|
-
if (options.runtimeBytecodeVersion != null) {
|
|
416
|
-
output.push({
|
|
417
|
-
data: compileToBytecode(code, jsType, {
|
|
418
|
-
sourceURL: file.filename,
|
|
419
|
-
sourceMap: fromRawMappings([
|
|
420
|
-
{
|
|
421
|
-
code,
|
|
422
|
-
source: file.code,
|
|
423
|
-
map,
|
|
424
|
-
functionMap: null,
|
|
425
|
-
path: file.filename,
|
|
426
|
-
},
|
|
427
|
-
]).toString(),
|
|
428
|
-
}),
|
|
429
|
-
type: getBytecodeFileType(jsType),
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
377
|
return {
|
|
433
378
|
dependencies: [],
|
|
434
379
|
output,
|
|
435
380
|
};
|
|
436
381
|
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* Returns the bytecode type for a file type
|
|
440
|
-
*/
|
|
441
|
-
function getBytecodeFileType(type) {
|
|
442
|
-
switch (type) {
|
|
443
|
-
case "js/module/asset":
|
|
444
|
-
return "bytecode/module/asset";
|
|
445
|
-
case "js/script":
|
|
446
|
-
return "bytecode/script";
|
|
447
|
-
default:
|
|
448
|
-
type;
|
|
449
|
-
return "bytecode/module";
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
382
|
function getBabelTransformArgs(file, { options, config, projectRoot }) {
|
|
453
383
|
return {
|
|
454
384
|
filename: file.filename,
|
|
@@ -518,18 +448,12 @@ module.exports = {
|
|
|
518
448
|
return await transformJSWithBabel(file, context);
|
|
519
449
|
},
|
|
520
450
|
getCacheKey: (config) => {
|
|
521
|
-
const {
|
|
522
|
-
babelTransformerPath,
|
|
523
|
-
minifierPath,
|
|
524
|
-
unstable_collectDependenciesPath,
|
|
525
|
-
...remainingConfig
|
|
526
|
-
} = config;
|
|
451
|
+
const { babelTransformerPath, minifierPath, ...remainingConfig } = config;
|
|
527
452
|
const filesKey = getCacheKey([
|
|
528
453
|
require.resolve(babelTransformerPath),
|
|
529
454
|
require.resolve(minifierPath),
|
|
530
455
|
require.resolve("./utils/getMinifier"),
|
|
531
456
|
require.resolve("./utils/assetTransformer"),
|
|
532
|
-
require.resolve(unstable_collectDependenciesPath),
|
|
533
457
|
require.resolve("metro/src/ModuleGraph/worker/generateImportNames"),
|
|
534
458
|
require.resolve("metro/src/ModuleGraph/worker/JsFileWrapping"),
|
|
535
459
|
...metroTransformPlugins.getTransformPluginCacheKeyFiles(),
|
package/src/index.js.flow
CHANGED
|
@@ -18,10 +18,6 @@ import type {
|
|
|
18
18
|
CustomTransformOptions,
|
|
19
19
|
TransformProfile,
|
|
20
20
|
} from 'metro-babel-transformer';
|
|
21
|
-
import type {
|
|
22
|
-
HermesCompilerResult,
|
|
23
|
-
Options as HermesCompilerOptions,
|
|
24
|
-
} from 'metro-hermes-compiler';
|
|
25
21
|
import type {
|
|
26
22
|
BasicSourceMap,
|
|
27
23
|
FBSourceFunctionMap,
|
|
@@ -33,7 +29,6 @@ import type {
|
|
|
33
29
|
DependencyTransformer,
|
|
34
30
|
DynamicRequiresBehavior,
|
|
35
31
|
} from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
36
|
-
import typeof CollectDependenciesFn from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
37
32
|
|
|
38
33
|
const getMinifier = require('./utils/getMinifier');
|
|
39
34
|
const {transformFromAstSync} = require('@babel/core');
|
|
@@ -52,6 +47,7 @@ const countLines = require('metro/src/lib/countLines');
|
|
|
52
47
|
const {
|
|
53
48
|
InvalidRequireCallError: InternalInvalidRequireCallError,
|
|
54
49
|
} = require('metro/src/ModuleGraph/worker/collectDependencies');
|
|
50
|
+
const collectDependencies = require('metro/src/ModuleGraph/worker/collectDependencies');
|
|
55
51
|
const generateImportNames = require('metro/src/ModuleGraph/worker/generateImportNames');
|
|
56
52
|
const JsFileWrapping = require('metro/src/ModuleGraph/worker/JsFileWrapping');
|
|
57
53
|
const nullthrows = require('nullthrows');
|
|
@@ -94,7 +90,6 @@ export type JsTransformerConfig = $ReadOnly<{
|
|
|
94
90
|
optimizationSizeLimit: number,
|
|
95
91
|
publicPath: string,
|
|
96
92
|
allowOptionalDependencies: AllowOptionalDependencies,
|
|
97
|
-
unstable_collectDependenciesPath: string,
|
|
98
93
|
unstable_dependencyMapReservedName: ?string,
|
|
99
94
|
unstable_disableModuleWrapping: boolean,
|
|
100
95
|
unstable_disableNormalizePseudoGlobals: boolean,
|
|
@@ -115,17 +110,11 @@ export type JsTransformOptions = $ReadOnly<{
|
|
|
115
110
|
minify: boolean,
|
|
116
111
|
nonInlinedRequires?: $ReadOnlyArray<string>,
|
|
117
112
|
platform: ?string,
|
|
118
|
-
runtimeBytecodeVersion: ?number,
|
|
119
113
|
type: Type,
|
|
120
114
|
unstable_disableES6Transforms?: boolean,
|
|
121
115
|
unstable_transformProfile: TransformProfile,
|
|
122
116
|
}>;
|
|
123
117
|
|
|
124
|
-
export type BytecodeFileType =
|
|
125
|
-
| 'bytecode/module'
|
|
126
|
-
| 'bytecode/module/asset'
|
|
127
|
-
| 'bytecode/script';
|
|
128
|
-
|
|
129
118
|
opaque type Path = string;
|
|
130
119
|
|
|
131
120
|
type BaseFile = $ReadOnly<{
|
|
@@ -169,19 +158,9 @@ export type JsOutput = $ReadOnly<{
|
|
|
169
158
|
type: JSFileType,
|
|
170
159
|
}>;
|
|
171
160
|
|
|
172
|
-
export type BytecodeOutput = $ReadOnly<{
|
|
173
|
-
data: HermesCompilerResult,
|
|
174
|
-
type: BytecodeFileType,
|
|
175
|
-
}>;
|
|
176
|
-
|
|
177
|
-
type DependencySplitCondition = $PropertyType<
|
|
178
|
-
$PropertyType<TransformResultDependency, 'data'>,
|
|
179
|
-
'splitCondition',
|
|
180
|
-
>;
|
|
181
|
-
|
|
182
161
|
type TransformResponse = $ReadOnly<{
|
|
183
162
|
dependencies: $ReadOnlyArray<TransformResultDependency>,
|
|
184
|
-
output: $ReadOnlyArray<JsOutput
|
|
163
|
+
output: $ReadOnlyArray<JsOutput>,
|
|
185
164
|
}>;
|
|
186
165
|
|
|
187
166
|
function getDynamicDepsBehavior(
|
|
@@ -247,27 +226,9 @@ const minifyCode = async (
|
|
|
247
226
|
}
|
|
248
227
|
};
|
|
249
228
|
|
|
250
|
-
const
|
|
251
|
-
rawCode: string,
|
|
252
|
-
type: string,
|
|
253
|
-
options: HermesCompilerOptions,
|
|
254
|
-
): HermesCompilerResult => {
|
|
255
|
-
let code = rawCode;
|
|
256
|
-
if (type.startsWith('js/module')) {
|
|
257
|
-
const index = code.lastIndexOf(')');
|
|
258
|
-
code =
|
|
259
|
-
code.slice(0, index) +
|
|
260
|
-
',$$METRO_D[0],$$METRO_D[1],$$METRO_D[2]' +
|
|
261
|
-
code.slice(index);
|
|
262
|
-
}
|
|
263
|
-
const HermesCompiler = require('metro-hermes-compiler');
|
|
264
|
-
return HermesCompiler.compile(code, options);
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
const disabledDependencyTransformer: DependencyTransformer<mixed> = {
|
|
229
|
+
const disabledDependencyTransformer: DependencyTransformer = {
|
|
268
230
|
transformSyncRequire: () => void 0,
|
|
269
231
|
transformImportCall: () => void 0,
|
|
270
|
-
transformJSResource: () => void 0,
|
|
271
232
|
transformPrefetch: () => void 0,
|
|
272
233
|
transformIllegalDynamicRequire: () => void 0,
|
|
273
234
|
};
|
|
@@ -401,8 +362,6 @@ async function transformJS(
|
|
|
401
362
|
dependencyMapName: config.unstable_dependencyMapReservedName,
|
|
402
363
|
unstable_allowRequireContext: config.unstable_allowRequireContext,
|
|
403
364
|
};
|
|
404
|
-
// $FlowFixMe[unsupported-syntax] dynamic require
|
|
405
|
-
const collectDependencies: CollectDependenciesFn<DependencySplitCondition> = require(config.unstable_collectDependenciesPath);
|
|
406
365
|
({ast, dependencies, dependencyMapName} = collectDependencies(ast, opts));
|
|
407
366
|
} catch (error) {
|
|
408
367
|
if (error instanceof InternalInvalidRequireCallError) {
|
|
@@ -473,7 +432,7 @@ async function transformJS(
|
|
|
473
432
|
));
|
|
474
433
|
}
|
|
475
434
|
|
|
476
|
-
const output: Array<JsOutput
|
|
435
|
+
const output: Array<JsOutput> = [
|
|
477
436
|
{
|
|
478
437
|
data: {
|
|
479
438
|
code,
|
|
@@ -485,24 +444,6 @@ async function transformJS(
|
|
|
485
444
|
},
|
|
486
445
|
];
|
|
487
446
|
|
|
488
|
-
if (options.runtimeBytecodeVersion != null) {
|
|
489
|
-
output.push({
|
|
490
|
-
data: (compileToBytecode(code, file.type, {
|
|
491
|
-
sourceURL: file.filename,
|
|
492
|
-
sourceMap: fromRawMappings([
|
|
493
|
-
{
|
|
494
|
-
code,
|
|
495
|
-
source: file.code,
|
|
496
|
-
map,
|
|
497
|
-
functionMap: null,
|
|
498
|
-
path: file.filename,
|
|
499
|
-
},
|
|
500
|
-
]).toString(),
|
|
501
|
-
}): HermesCompilerResult),
|
|
502
|
-
type: getBytecodeFileType(file.type),
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
|
|
506
447
|
return {
|
|
507
448
|
dependencies,
|
|
508
449
|
output,
|
|
@@ -597,52 +538,19 @@ async function transformJSON(
|
|
|
597
538
|
jsType = 'js/module';
|
|
598
539
|
}
|
|
599
540
|
|
|
600
|
-
const output: Array<JsOutput
|
|
541
|
+
const output: Array<JsOutput> = [
|
|
601
542
|
{
|
|
602
543
|
data: {code, lineCount: countLines(code), map, functionMap: null},
|
|
603
544
|
type: jsType,
|
|
604
545
|
},
|
|
605
546
|
];
|
|
606
547
|
|
|
607
|
-
if (options.runtimeBytecodeVersion != null) {
|
|
608
|
-
output.push({
|
|
609
|
-
data: (compileToBytecode(code, jsType, {
|
|
610
|
-
sourceURL: file.filename,
|
|
611
|
-
sourceMap: fromRawMappings([
|
|
612
|
-
{
|
|
613
|
-
code,
|
|
614
|
-
source: file.code,
|
|
615
|
-
map,
|
|
616
|
-
functionMap: null,
|
|
617
|
-
path: file.filename,
|
|
618
|
-
},
|
|
619
|
-
]).toString(),
|
|
620
|
-
}): HermesCompilerResult),
|
|
621
|
-
type: getBytecodeFileType(jsType),
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
|
-
|
|
625
548
|
return {
|
|
626
549
|
dependencies: [],
|
|
627
550
|
output,
|
|
628
551
|
};
|
|
629
552
|
}
|
|
630
553
|
|
|
631
|
-
/**
|
|
632
|
-
* Returns the bytecode type for a file type
|
|
633
|
-
*/
|
|
634
|
-
function getBytecodeFileType(type: JSFileType): BytecodeFileType {
|
|
635
|
-
switch (type) {
|
|
636
|
-
case 'js/module/asset':
|
|
637
|
-
return 'bytecode/module/asset';
|
|
638
|
-
case 'js/script':
|
|
639
|
-
return 'bytecode/script';
|
|
640
|
-
default:
|
|
641
|
-
(type: 'js/module');
|
|
642
|
-
return 'bytecode/module';
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
|
|
646
554
|
function getBabelTransformArgs(
|
|
647
555
|
file: $ReadOnly<{filename: Path, code: string, ...}>,
|
|
648
556
|
{options, config, projectRoot}: TransformationContext,
|
|
@@ -730,19 +638,13 @@ module.exports = {
|
|
|
730
638
|
},
|
|
731
639
|
|
|
732
640
|
getCacheKey: (config: JsTransformerConfig): string => {
|
|
733
|
-
const {
|
|
734
|
-
babelTransformerPath,
|
|
735
|
-
minifierPath,
|
|
736
|
-
unstable_collectDependenciesPath,
|
|
737
|
-
...remainingConfig
|
|
738
|
-
} = config;
|
|
641
|
+
const {babelTransformerPath, minifierPath, ...remainingConfig} = config;
|
|
739
642
|
|
|
740
643
|
const filesKey = getCacheKey([
|
|
741
644
|
require.resolve(babelTransformerPath),
|
|
742
645
|
require.resolve(minifierPath),
|
|
743
646
|
require.resolve('./utils/getMinifier'),
|
|
744
647
|
require.resolve('./utils/assetTransformer'),
|
|
745
|
-
require.resolve(unstable_collectDependenciesPath),
|
|
746
648
|
require.resolve('metro/src/ModuleGraph/worker/generateImportNames'),
|
|
747
649
|
require.resolve('metro/src/ModuleGraph/worker/JsFileWrapping'),
|
|
748
650
|
...metroTransformPlugins.getTransformPluginCacheKeyFiles(),
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
* @oncall react_native
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {DynamicRequiresBehavior} from 'metro';
|
|
12
|
+
import type {TransformResultDependency} from 'metro/DeltaBundler';
|
|
13
|
+
import type {AllowOptionalDependencies} from 'metro/DeltaBundler/types';
|
|
14
|
+
import type {
|
|
15
|
+
CustomTransformOptions,
|
|
16
|
+
TransformProfile,
|
|
17
|
+
} from 'metro-babel-transformer';
|
|
18
|
+
import type {
|
|
19
|
+
BasicSourceMap,
|
|
20
|
+
FBSourceFunctionMap,
|
|
21
|
+
MetroSourceMapSegmentTuple,
|
|
22
|
+
} from 'metro-source-map';
|
|
23
|
+
|
|
24
|
+
export type MinifierConfig = Readonly<Record<string, unknown>>;
|
|
25
|
+
|
|
26
|
+
export interface MinifierOptions {
|
|
27
|
+
code: string;
|
|
28
|
+
map?: BasicSourceMap;
|
|
29
|
+
filename: string;
|
|
30
|
+
reserved: ReadonlyArray<string>;
|
|
31
|
+
config: MinifierConfig;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MinifierResult {
|
|
35
|
+
code: string;
|
|
36
|
+
map?: BasicSourceMap;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type Minifier = (
|
|
40
|
+
options: MinifierOptions,
|
|
41
|
+
) => MinifierResult | Promise<MinifierResult>;
|
|
42
|
+
|
|
43
|
+
export type Type = 'script' | 'module' | 'asset';
|
|
44
|
+
|
|
45
|
+
export type JsTransformerConfig = Readonly<{
|
|
46
|
+
assetPlugins: ReadonlyArray<string>;
|
|
47
|
+
assetRegistryPath: string;
|
|
48
|
+
asyncRequireModulePath: string;
|
|
49
|
+
babelTransformerPath: string;
|
|
50
|
+
dynamicDepsInPackages: DynamicRequiresBehavior;
|
|
51
|
+
enableBabelRCLookup: boolean;
|
|
52
|
+
enableBabelRuntime: boolean;
|
|
53
|
+
globalPrefix: string;
|
|
54
|
+
hermesParser: boolean;
|
|
55
|
+
minifierConfig: MinifierConfig;
|
|
56
|
+
minifierPath: string;
|
|
57
|
+
optimizationSizeLimit: number;
|
|
58
|
+
publicPath: string;
|
|
59
|
+
allowOptionalDependencies: AllowOptionalDependencies;
|
|
60
|
+
unstable_collectDependenciesPath: string;
|
|
61
|
+
unstable_dependencyMapReservedName?: string;
|
|
62
|
+
unstable_disableModuleWrapping: boolean;
|
|
63
|
+
unstable_disableNormalizePseudoGlobals: boolean;
|
|
64
|
+
unstable_compactOutput: boolean;
|
|
65
|
+
/** Enable `require.context` statements which can be used to import multiple files in a directory. */
|
|
66
|
+
unstable_allowRequireContext: boolean;
|
|
67
|
+
}>;
|
|
68
|
+
|
|
69
|
+
export {CustomTransformOptions} from 'metro-babel-transformer';
|
|
70
|
+
|
|
71
|
+
export type JsTransformOptions = Readonly<{
|
|
72
|
+
customTransformOptions?: CustomTransformOptions;
|
|
73
|
+
dev: boolean;
|
|
74
|
+
experimentalImportSupport?: boolean;
|
|
75
|
+
hot: boolean;
|
|
76
|
+
inlinePlatform: boolean;
|
|
77
|
+
inlineRequires: boolean;
|
|
78
|
+
minify: boolean;
|
|
79
|
+
nonInlinedRequires?: ReadonlyArray<string>;
|
|
80
|
+
platform?: string;
|
|
81
|
+
runtimeBytecodeVersion?: number;
|
|
82
|
+
type: Type;
|
|
83
|
+
unstable_disableES6Transforms?: boolean;
|
|
84
|
+
unstable_transformProfile: TransformProfile;
|
|
85
|
+
}>;
|
|
86
|
+
|
|
87
|
+
export type BytecodeFileType =
|
|
88
|
+
| 'bytecode/module'
|
|
89
|
+
| 'bytecode/module/asset'
|
|
90
|
+
| 'bytecode/script';
|
|
91
|
+
|
|
92
|
+
export type JSFileType = 'js/script' | 'js/module' | 'js/module/asset';
|
|
93
|
+
|
|
94
|
+
export type JsOutput = Readonly<{
|
|
95
|
+
data: Readonly<{
|
|
96
|
+
code: string;
|
|
97
|
+
lineCount: number;
|
|
98
|
+
map: MetroSourceMapSegmentTuple[];
|
|
99
|
+
functionMap: FBSourceFunctionMap | null;
|
|
100
|
+
}>;
|
|
101
|
+
type: JSFileType;
|
|
102
|
+
}>;
|
|
103
|
+
|
|
104
|
+
// Hermes byte-code output type
|
|
105
|
+
export type BytecodeOutput = unknown;
|
|
106
|
+
|
|
107
|
+
export type TransformResponse = Readonly<{
|
|
108
|
+
dependencies: ReadonlyArray<TransformResultDependency>;
|
|
109
|
+
output: ReadonlyArray<JsOutput | BytecodeOutput>;
|
|
110
|
+
}>;
|
|
111
|
+
|
|
112
|
+
export function transform(
|
|
113
|
+
config: JsTransformerConfig,
|
|
114
|
+
projectRoot: string,
|
|
115
|
+
filename: string,
|
|
116
|
+
data: Buffer,
|
|
117
|
+
options: JsTransformOptions,
|
|
118
|
+
): Promise<TransformResponse>;
|
|
119
|
+
|
|
120
|
+
export function getCacheKey(config: JsTransformerConfig): string;
|