-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
metro-transform-worker.ts
812 lines (720 loc) · 25.8 KB
/
metro-transform-worker.ts
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/**
* Copyright 2023-present 650 Industries (Expo). All rights reserved.
* 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.
*
* Fork of the Metro transformer worker, but with additional transforms moved to `babel-preset-expo` and modifications made for web support.
* https://github.com/facebook/metro/blob/412771475c540b6f85d75d9dcd5a39a6e0753582/packages/metro-transform-worker/src/index.js#L1
*/
import { transformFromAstSync } from '@babel/core';
import type { ParseResult, PluginItem } from '@babel/core';
import generate from '@babel/generator';
import * as babylon from '@babel/parser';
import template from '@babel/template';
import type { NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import JsFileWrapping from 'metro/src/ModuleGraph/worker/JsFileWrapping';
import generateImportNames from 'metro/src/ModuleGraph/worker/generateImportNames';
import type { BabelTransformer, BabelTransformerArgs } from 'metro-babel-transformer';
import { stableHash } from 'metro-cache';
import getMetroCacheKey from 'metro-cache-key';
import {
fromRawMappings,
functionMapBabelPlugin,
toBabelSegments,
toSegmentTuple,
} from 'metro-source-map';
import type { FBSourceFunctionMap, MetroSourceMapSegmentTuple } from 'metro-source-map';
import metroTransformPlugins from 'metro-transform-plugins';
import { JsTransformerConfig, JsTransformOptions, Type } from 'metro-transform-worker';
import getMinifier from 'metro-transform-worker/src/utils/getMinifier';
import assert from 'node:assert';
import * as assetTransformer from './asset-transformer';
import collectDependencies, {
InvalidRequireCallError as InternalInvalidRequireCallError,
Dependency,
DependencyTransformer,
DynamicRequiresBehavior,
CollectedDependencies,
Options as CollectDependenciesOptions,
State,
} from './collect-dependencies';
import { countLinesAndTerminateMap } from './count-lines';
import { shouldMinify } from './resolveOptions';
import { ExpoJsOutput, ReconcileTransformSettings } from '../serializer/jsOutput';
export { JsTransformOptions };
interface BaseFile {
readonly code: string;
readonly filename: string;
readonly inputFileSize: number;
}
interface AssetFile extends BaseFile {
readonly type: 'asset';
}
type JSFileType = 'js/script' | 'js/module' | 'js/module/asset';
interface JSFile extends BaseFile {
readonly ast?: ParseResult | null;
readonly type: JSFileType;
readonly functionMap: FBSourceFunctionMap | null;
readonly reactClientReference?: string;
readonly hasCjsExports?: boolean;
}
interface JSONFile extends BaseFile {
readonly type: Type;
}
interface TransformationContext {
readonly config: JsTransformerConfig;
readonly projectRoot: string;
readonly options: JsTransformOptions;
}
interface TransformResponse {
readonly dependencies: CollectedDependencies['dependencies'];
readonly output: readonly ExpoJsOutput[];
}
export class InvalidRequireCallError extends Error {
innerError: InternalInvalidRequireCallError;
filename: string;
constructor(innerError: InternalInvalidRequireCallError, filename: string) {
super(`${filename}:${innerError.message}`);
this.innerError = innerError;
this.filename = filename;
}
}
// asserts non-null
function nullthrows<T extends object>(x: T | null, message?: string): NonNullable<T> {
assert(x != null, message);
return x;
}
function getDynamicDepsBehavior(
inPackages: DynamicRequiresBehavior,
filename: string
): DynamicRequiresBehavior {
switch (inPackages) {
case 'reject':
return 'reject';
case 'throwAtRuntime':
return /(?:^|[/\\])node_modules[/\\]/.test(filename) ? inPackages : 'reject';
default:
throw new Error(`invalid value for dynamic deps behavior: \`${inPackages}\``);
}
}
export const minifyCode = async (
config: Pick<JsTransformerConfig, 'minifierPath' | 'minifierConfig'>,
filename: string,
code: string,
source: string,
map: MetroSourceMapSegmentTuple[],
reserved: string[] = []
): Promise<{
code: string;
map: MetroSourceMapSegmentTuple[];
}> => {
const sourceMap = fromRawMappings([
{
code,
source,
map,
// functionMap is overridden by the serializer
functionMap: null,
path: filename,
// isIgnored is overriden by the serializer
isIgnored: false,
},
]).toMap(undefined, {});
const minify = getMinifier(config.minifierPath);
try {
const minified = await minify({
code,
map: sourceMap,
filename,
reserved,
config: config.minifierConfig,
});
return {
code: minified.code,
map: minified.map ? toBabelSegments(minified.map).map(toSegmentTuple) : [],
};
} catch (error: any) {
if (error.constructor.name === 'JS_Parse_Error') {
throw new Error(`${error.message} in file ${filename} at ${error.line}:${error.col}`);
}
throw error;
}
};
function renameTopLevelModuleVariables() {
// A babel plugin which renames variables in the top-level scope that are named "module".
return {
visitor: {
Program(path: any) {
['global', 'require', 'module', 'exports'].forEach((name) => {
path.scope.rename(name, path.scope.generateUidIdentifier(name).name);
});
},
},
};
}
function applyUseStrictDirective(ast: babylon.ParseResult<t.File>) {
// Add "use strict" if the file was parsed as a module, and the directive did
// not exist yet.
const { directives } = ast.program;
if (
ast.program.sourceType === 'module' &&
directives != null &&
directives.findIndex((d) => d.value.value === 'use strict') === -1
) {
directives.push(t.directive(t.directiveLiteral('use strict')));
}
}
export function applyImportSupport<TFile extends t.File>(
ast: TFile,
{
filename,
options,
importDefault,
importAll,
}: {
filename: string;
options: Pick<
JsTransformOptions,
'experimentalImportSupport' | 'inlineRequires' | 'nonInlinedRequires'
>;
importDefault: string;
importAll: string;
}
): TFile {
// Perform the import-export transform (in case it's still needed), then
// fold requires and perform constant folding (if in dev).
const plugins: PluginItem[] = [];
const babelPluginOpts = {
...options,
inlineableCalls: [importDefault, importAll],
importDefault,
importAll,
};
// NOTE(EvanBacon): This is effectively a replacement for the `@babel/plugin-transform-modules-commonjs`
// plugin that's running in `@react-native/babel-preset`, but with shared names for inlining requires.
if (options.experimentalImportSupport === true) {
plugins.push(
// Ensure the iife "globals" don't have conflicting variables in the module.
renameTopLevelModuleVariables,
//
[metroTransformPlugins.importExportPlugin, babelPluginOpts]
);
}
// NOTE(EvanBacon): This can basically never be safely enabled because it doesn't respect side-effects and
// has no ability to respect side-effects because the transformer hasn't collected all dependencies yet.
if (options.inlineRequires) {
plugins.push([
metroTransformPlugins.inlineRequiresPlugin,
{
...babelPluginOpts,
ignoredRequires: options.nonInlinedRequires,
},
]);
}
// NOTE(EvanBacon): We apply this conditionally in `babel-preset-expo` with other AST transforms.
// plugins.push([metroTransformPlugins.inlinePlugin, babelPluginOpts]);
// TODO: This MUST be run even though no plugins are added, otherwise the babel runtime generators are broken.
if (plugins.length) {
ast = nullthrows<TFile>(
// @ts-expect-error
transformFromAstSync(ast, '', {
ast: true,
babelrc: false,
code: false,
configFile: false,
comments: true,
filename,
plugins,
sourceMaps: false,
// NOTE(kitten): This was done to wipe the paths/scope caches, which the `constantFoldingPlugin` needs to work,
// but has been replaced with `programPath.scope.crawl()`.
// Old Note from Metro:
// > Not-Cloning the input AST here should be safe because other code paths above this call
// > are mutating the AST as well and no code is depending on the original AST.
// > However, switching the flag to false caused issues with ES Modules if `experimentalImportSupport` isn't used https://github.com/facebook/metro/issues/641
// > either because one of the plugins is doing something funky or Babel messes up some caches.
// > Make sure to test the above mentioned case before flipping the flag back to false.
cloneInputAst: false,
})?.ast
);
}
return ast;
}
function performConstantFolding(
ast: babylon.ParseResult<t.File>,
{ filename }: { filename: string }
) {
// NOTE(kitten): Any Babel helpers that have been added (`path.hub.addHelper(...)`) will usually not have any
// references, and hence the `constantFoldingPlugin` below will remove them.
// To fix the references we add an explicit `programPath.scope.crawl()`. Alternatively, we could also wipe the
// Babel traversal cache (`traverse.cache.clear()`)
const clearProgramScopePlugin: PluginItem = {
visitor: {
Program: {
enter(path) {
path.scope.crawl();
},
},
},
};
// Run the constant folding plugin in its own pass, avoiding race conditions
// with other plugins that have exit() visitors on Program (e.g. the ESM
// transform).
ast = nullthrows<babylon.ParseResult<t.File>>(
// @ts-expect-error
transformFromAstSync(ast, '', {
ast: true,
babelrc: false,
code: false,
configFile: false,
comments: true,
filename,
plugins: [clearProgramScopePlugin, metroTransformPlugins.constantFoldingPlugin],
sourceMaps: false,
// NOTE(kitten): In Metro, this is also false, but only works because the prior run of `transformFromAstSync` was always
// running with `cloneInputAst: true`.
// This isn't needed anymore since `clearProgramScopePlugin` re-crawls the AST’s scope instead.
cloneInputAst: false,
}).ast
);
return ast;
}
async function transformJS(
file: JSFile,
{ config, options }: TransformationContext
): Promise<TransformResponse> {
const targetEnv = options.customTransformOptions?.environment;
const isServerEnv = targetEnv === 'node' || targetEnv === 'react-server';
const optimize =
// Ensure we don't enable tree shaking for scripts or assets.
file.type === 'js/module' &&
String(options.customTransformOptions?.optimize) === 'true' &&
// Disable tree shaking on JSON files.
!file.filename.endsWith('.json');
const unstable_disableModuleWrapping = optimize || config.unstable_disableModuleWrapping;
if (optimize && !options.experimentalImportSupport) {
// Add a warning so devs can incrementally migrate since experimentalImportSupport may cause other issues in their app.
throw new Error(
'Experimental graph optimizations only work with experimentalImportSupport enabled.'
);
}
// Transformers can output null ASTs (if they ignore the file). In that case
// we need to parse the module source code to get their AST.
let ast: babylon.ParseResult<t.File> =
file.ast ?? babylon.parse(file.code, { sourceType: 'unambiguous' });
// NOTE(EvanBacon): This can be really expensive on larger files. We should replace it with a cheaper alternative that just iterates and matches.
const { importDefault, importAll } = generateImportNames(ast);
// Add "use strict" if the file was parsed as a module, and the directive did
// not exist yet.
applyUseStrictDirective(ast);
// @ts-expect-error: Not on types yet (Metro 0.80).
const unstable_renameRequire = config.unstable_renameRequire;
// Disable all Metro single-file optimizations when full-graph optimization will be used.
if (!optimize) {
ast = applyImportSupport(ast, { filename: file.filename, options, importDefault, importAll });
}
if (!options.dev) {
ast = performConstantFolding(ast, { filename: file.filename });
}
let dependencyMapName: string = '';
let dependencies: readonly Dependency[];
let wrappedAst: t.File | undefined;
// If the module to transform is a script (meaning that is not part of the
// dependency graph and it code will just be prepended to the bundle modules),
// we need to wrap it differently than a commonJS module (also, scripts do
// not have dependencies).
let collectDependenciesOptions: CollectDependenciesOptions | undefined;
if (file.type === 'js/script') {
dependencies = [];
wrappedAst = JsFileWrapping.wrapPolyfill(ast);
} else {
try {
collectDependenciesOptions = {
asyncRequireModulePath: config.asyncRequireModulePath,
dependencyTransformer:
config.unstable_disableModuleWrapping === true
? disabledDependencyTransformer
: undefined,
dynamicRequires: isServerEnv
? // NOTE(EvanBacon): Allow arbitrary imports in server environments.
// This requires a patch to Metro collectDeps.
'warn'
: getDynamicDepsBehavior(config.dynamicDepsInPackages, file.filename),
inlineableCalls: [importDefault, importAll],
keepRequireNames: options.dev,
allowOptionalDependencies: config.allowOptionalDependencies,
dependencyMapName: config.unstable_dependencyMapReservedName,
unstable_allowRequireContext: config.unstable_allowRequireContext,
// If tree shaking is enabled, then preserve the original require calls.
// This ensures require.context calls are not broken.
collectOnly: optimize === true,
};
({ ast, dependencies, dependencyMapName } = collectDependencies(ast, {
...collectDependenciesOptions,
// This setting shouldn't be shared with the tree shaking transformer.
dependencyTransformer:
unstable_disableModuleWrapping === true ? disabledDependencyTransformer : undefined,
}));
// Ensure we use the same name for the second pass of the dependency collection in the serializer.
collectDependenciesOptions = {
...collectDependenciesOptions,
dependencyMapName,
};
} catch (error) {
if (error instanceof InternalInvalidRequireCallError) {
throw new InvalidRequireCallError(error, file.filename);
}
throw error;
}
if (unstable_disableModuleWrapping === true) {
wrappedAst = ast;
} else {
// TODO: Replace this with a cheaper transform that doesn't require AST.
({ ast: wrappedAst } = JsFileWrapping.wrapModule(
ast,
importDefault,
importAll,
dependencyMapName,
config.globalPrefix,
// TODO: This config is optional to allow its introduction in a minor
// release. It should be made non-optional in ConfigT or removed in
// future.
// @ts-expect-error: Not on types yet (Metro 0.80.9).
unstable_renameRequire === false
));
}
}
const minify = shouldMinify(options);
const shouldNormalizePseudoGlobals =
minify &&
file.inputFileSize <= config.optimizationSizeLimit &&
!config.unstable_disableNormalizePseudoGlobals;
const reserved: string[] = [];
if (config.unstable_dependencyMapReservedName != null) {
reserved.push(config.unstable_dependencyMapReservedName);
}
if (
shouldNormalizePseudoGlobals &&
// TODO: If the module wrapping is disabled then the normalize function needs to change to account for not being in a body.
!unstable_disableModuleWrapping
) {
// NOTE(EvanBacon): Simply pushing this function will mutate the AST, so it must run before the `generate` step!!
reserved.push(
...metroTransformPlugins.normalizePseudoGlobals(wrappedAst, {
reservedNames: reserved,
})
);
}
const result = generate(
wrappedAst,
{
comments: true,
compact: config.unstable_compactOutput,
filename: file.filename,
retainLines: false,
sourceFileName: file.filename,
sourceMaps: true,
},
file.code
);
// @ts-expect-error: incorrectly typed upstream
let map = result.rawMappings ? result.rawMappings.map(toSegmentTuple) : [];
let code = result.code;
// NOTE: We might want to enable this on native + hermes when tree shaking is enabled.
if (minify) {
({ map, code } = await minifyCode(
config,
file.filename,
result.code,
file.code,
map,
reserved
));
}
const possibleReconcile: ReconcileTransformSettings | undefined =
optimize && collectDependenciesOptions
? {
inlineRequires: options.inlineRequires,
importDefault,
importAll,
normalizePseudoGlobals: shouldNormalizePseudoGlobals,
globalPrefix: config.globalPrefix,
unstable_compactOutput: config.unstable_compactOutput,
collectDependenciesOptions,
minify: minify
? {
minifierPath: config.minifierPath,
minifierConfig: config.minifierConfig,
}
: undefined,
unstable_dependencyMapReservedName: config.unstable_dependencyMapReservedName,
optimizationSizeLimit: config.optimizationSizeLimit,
unstable_disableNormalizePseudoGlobals: config.unstable_disableNormalizePseudoGlobals,
unstable_renameRequire,
}
: undefined;
let lineCount;
({ lineCount, map } = countLinesAndTerminateMap(code, map));
const output: ExpoJsOutput[] = [
{
data: {
code,
lineCount,
map,
functionMap: file.functionMap,
hasCjsExports: file.hasCjsExports,
reactClientReference: file.reactClientReference,
...(possibleReconcile
? {
ast: wrappedAst,
// Store settings for the module that will be used to finish transformation after graph-based optimizations
// have finished.
reconcile: possibleReconcile,
}
: {}),
},
type: file.type,
},
];
return {
dependencies,
output,
};
}
/** Transforms an asset file. */
async function transformAsset(
file: AssetFile,
context: TransformationContext
): Promise<TransformResponse> {
const { assetRegistryPath, assetPlugins } = context.config;
// TODO: Add web asset hashing in production.
const result = await assetTransformer.transform(
getBabelTransformArgs(file, context),
assetRegistryPath,
assetPlugins
);
const jsFile: JSFile = {
...file,
type: 'js/module/asset',
ast: result.ast,
functionMap: null,
hasCjsExports: true,
reactClientReference: result.reactClientReference,
};
return transformJS(jsFile, context);
}
/**
* Transforms a JavaScript file with Babel before processing the file with
* the generic JavaScript transformation.
*/
async function transformJSWithBabel(
file: JSFile,
context: TransformationContext
): Promise<TransformResponse> {
const { babelTransformerPath } = context.config;
const transformer: BabelTransformer = require(babelTransformerPath);
// HACK: React Compiler injects import statements and exits the Babel process which leaves the code in
// a malformed state. For now, we'll enable the experimental import support which compiles import statements
// outside of the standard Babel process.
if (!context.options.experimentalImportSupport) {
const reactCompilerFlag = context.options.customTransformOptions?.reactCompiler;
if (reactCompilerFlag === true || reactCompilerFlag === 'true') {
// @ts-expect-error: readonly.
context.options.experimentalImportSupport = true;
}
}
// TODO: Add a babel plugin which returns if the module has commonjs, and if so, disable all tree shaking optimizations early.
const transformResult = await transformer.transform(
// functionMapBabelPlugin populates metadata.metro.functionMap
getBabelTransformArgs(file, context, [functionMapBabelPlugin])
);
const jsFile: JSFile = {
...file,
ast: transformResult.ast,
functionMap:
transformResult.metadata?.metro?.functionMap ??
// Fallback to deprecated explicitly-generated `functionMap`
transformResult.functionMap ??
null,
hasCjsExports: transformResult.metadata?.hasCjsExports,
reactClientReference: transformResult.metadata?.reactClientReference,
};
return await transformJS(jsFile, context);
}
async function transformJSON(
file: JSONFile,
{ options, config }: TransformationContext
): Promise<TransformResponse> {
let code =
config.unstable_disableModuleWrapping === true
? JsFileWrapping.jsonToCommonJS(file.code)
: JsFileWrapping.wrapJson(file.code, config.globalPrefix);
let map: MetroSourceMapSegmentTuple[] = [];
const minify = shouldMinify(options);
if (minify) {
({ map, code } = await minifyCode(config, file.filename, code, file.code, map));
}
let jsType: JSFileType;
if (file.type === 'asset') {
jsType = 'js/module/asset';
} else if (file.type === 'script') {
jsType = 'js/script';
} else {
jsType = 'js/module';
}
let lineCount;
({ lineCount, map } = countLinesAndTerminateMap(code, map));
const output: ExpoJsOutput[] = [
{
data: { code, lineCount, map, functionMap: null },
type: jsType,
},
];
return {
dependencies: [],
output,
};
}
function getBabelTransformArgs(
file: { filename: string; code: string },
{ options, config, projectRoot }: TransformationContext,
plugins: PluginItem[] = []
): BabelTransformerArgs {
const { inlineRequires: _, ...babelTransformerOptions } = options;
return {
filename: file.filename,
options: {
...babelTransformerOptions,
enableBabelRCLookup: config.enableBabelRCLookup,
enableBabelRuntime: config.enableBabelRuntime,
hermesParser: config.hermesParser,
projectRoot,
publicPath: config.publicPath,
globalPrefix: config.globalPrefix,
platform: babelTransformerOptions.platform ?? null,
},
plugins,
src: file.code,
};
}
export async function transform(
config: JsTransformerConfig,
projectRoot: string,
filename: string,
data: Buffer,
options: JsTransformOptions
): Promise<TransformResponse> {
const context: TransformationContext = {
config,
projectRoot,
options,
};
const sourceCode = data.toString('utf8');
const { unstable_dependencyMapReservedName } = config;
if (unstable_dependencyMapReservedName != null) {
const position = sourceCode.indexOf(unstable_dependencyMapReservedName);
if (position > -1) {
throw new SyntaxError(
'Source code contains the reserved string `' +
unstable_dependencyMapReservedName +
'` at character offset ' +
position
);
}
}
if (filename.endsWith('.json')) {
const jsonFile: JSONFile = {
filename,
inputFileSize: data.length,
code: sourceCode,
type: options.type,
};
return transformJSON(jsonFile, context);
}
if (options.type === 'asset') {
const file: AssetFile = {
filename,
inputFileSize: data.length,
code: sourceCode,
type: options.type,
};
return transformAsset(file, context);
}
const file: JSFile = {
filename,
inputFileSize: data.length,
code: sourceCode,
type: options.type === 'script' ? 'js/script' : 'js/module',
functionMap: null,
};
return transformJSWithBabel(file, context);
}
export function getCacheKey(config: JsTransformerConfig): string {
const { babelTransformerPath, minifierPath, ...remainingConfig } = config;
const filesKey = getMetroCacheKey([
require.resolve(babelTransformerPath),
require.resolve(minifierPath),
require.resolve('metro-transform-worker/src/utils/getMinifier'),
require.resolve('./collect-dependencies'),
require.resolve('./asset-transformer'),
require.resolve('./resolveOptions'),
require.resolve('metro/src/ModuleGraph/worker/generateImportNames'),
require.resolve('metro/src/ModuleGraph/worker/JsFileWrapping'),
...metroTransformPlugins.getTransformPluginCacheKeyFiles(),
]);
const babelTransformer = require(babelTransformerPath);
return [
filesKey,
stableHash(remainingConfig).toString('hex'),
babelTransformer.getCacheKey ? babelTransformer.getCacheKey() : '',
].join('$');
}
/**
* Produces a Babel template that transforms an "import(...)" call into a
* "require(...)" call to the asyncRequire specified.
*/
const makeShimAsyncRequireTemplate = template.expression(`require(ASYNC_REQUIRE_MODULE_PATH)`);
type InternalDependency = any;
const disabledDependencyTransformer: DependencyTransformer = {
transformSyncRequire: (path) => {},
transformImportMaybeSyncCall: () => {},
transformImportCall: (path: NodePath, dependency: InternalDependency, state: State) => {
// HACK: Ensure the async import code is included in the bundle when an import() call is found.
let topParent = path;
while (topParent.parentPath) {
topParent = topParent.parentPath;
}
// @ts-expect-error
if (topParent._handled) {
return;
}
path.insertAfter(
makeShimAsyncRequireTemplate({
ASYNC_REQUIRE_MODULE_PATH: nullthrows(state.asyncRequireModulePathStringLiteral),
})
);
// @ts-expect-error: Prevent recursive loop
topParent._handled = true;
},
transformPrefetch: () => {},
transformIllegalDynamicRequire: () => {},
};
export function collectDependenciesForShaking(
ast: babylon.ParseResult<t.File>,
options: CollectDependenciesOptions
) {
const collectDependenciesOptions = {
...options,
// If tree shaking is enabled, then preserve the original require calls.
// This ensures require.context calls are not broken.
collectOnly: true,
};
return collectDependencies(ast, {
...collectDependenciesOptions,
// This setting shouldn't be shared with the tree shaking transformer.
dependencyTransformer: disabledDependencyTransformer,
});
}