metro-transform-worker 0.84.4 → 0.86.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metro-transform-worker",
3
- "version": "0.84.4",
3
+ "version": "0.86.0",
4
4
  "description": "🚇 Transform worker for Metro.",
5
5
  "main": "src/index.js",
6
6
  "exports": {
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "git+https://github.com/facebook/metro.git",
13
+ "url": "git+https://github.com/react/metro.git",
14
14
  "directory": "packages/metro-transform-worker"
15
15
  },
16
16
  "scripts": {
@@ -24,13 +24,13 @@
24
24
  "@babel/parser": "^7.29.0",
25
25
  "@babel/types": "^7.29.0",
26
26
  "flow-enums-runtime": "^0.0.6",
27
- "metro": "0.84.4",
28
- "metro-babel-transformer": "0.84.4",
29
- "metro-cache": "0.84.4",
30
- "metro-cache-key": "0.84.4",
31
- "metro-minify-terser": "0.84.4",
32
- "metro-source-map": "0.84.4",
33
- "metro-transform-plugins": "0.84.4",
27
+ "metro": "0.86.0",
28
+ "metro-babel-transformer": "0.86.0",
29
+ "metro-cache": "0.86.0",
30
+ "metro-cache-key": "0.86.0",
31
+ "metro-minify-terser": "0.86.0",
32
+ "metro-source-map": "0.86.0",
33
+ "metro-transform-plugins": "0.86.0",
34
34
  "nullthrows": "^1.1.1"
35
35
  },
36
36
  "devDependencies": {
@@ -38,6 +38,6 @@
38
38
  "metro-memory-fs": "*"
39
39
  },
40
40
  "engines": {
41
- "node": "^20.19.4 || ^22.13.0 || ^24.3.0 || >= 25.0.0"
41
+ "node": "^22.13.0 || ^24.3.0 || >= 26.0.0"
42
42
  }
43
43
  }
package/src/index.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @noformat
8
8
  * @oncall react_native
9
- * @generated SignedSource<<165882da0b131608da36b1cbd00ecf28>>
9
+ * @generated SignedSource<<28342f12ccdb6543eff2ef94ebd19bbc>>
10
10
  *
11
11
  * This file was translated from Flow by scripts/generateTypeScriptDefinitions.js
12
12
  * Original file: packages/metro-transform-worker/src/index.js
@@ -22,7 +22,7 @@ import type {
22
22
  import type {
23
23
  BasicSourceMap,
24
24
  FBSourceFunctionMap,
25
- MetroSourceMapSegmentTuple,
25
+ VlqMap,
26
26
  } from 'metro-source-map';
27
27
  import type {TransformResultDependency} from 'metro/private/DeltaBundler';
28
28
  import type {AllowOptionalDependencies} from 'metro/private/DeltaBundler/types';
@@ -90,7 +90,7 @@ export type JsOutput = Readonly<{
90
90
  data: Readonly<{
91
91
  code: string;
92
92
  lineCount: number;
93
- map: Array<MetroSourceMapSegmentTuple>;
93
+ map: VlqMap;
94
94
  functionMap: null | undefined | FBSourceFunctionMap;
95
95
  }>;
96
96
  type: JSFileType;
@@ -112,16 +112,3 @@ export declare const getCacheKey: (
112
112
  opts?: Readonly<{projectRoot: string}>,
113
113
  ) => string;
114
114
  export declare type getCacheKey = typeof getCacheKey;
115
- /**
116
- * Backwards-compatibility with CommonJS consumers using interopRequireDefault.
117
- * Do not add to this list.
118
- *
119
- * @deprecated Default import from 'metro-transform-worker' is deprecated, use named exports.
120
- */
121
- declare const $$EXPORT_DEFAULT_DECLARATION$$: {
122
- getCacheKey: typeof getCacheKey;
123
- transform: typeof transform;
124
- };
125
- declare type $$EXPORT_DEFAULT_DECLARATION$$ =
126
- typeof $$EXPORT_DEFAULT_DECLARATION$$;
127
- export default $$EXPORT_DEFAULT_DECLARATION$$;
package/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true,
5
5
  });
6
- exports.transform = exports.getCacheKey = exports.default = void 0;
6
+ exports.transform = exports.getCacheKey = void 0;
7
7
  var assetTransformer = _interopRequireWildcard(
8
8
  require("./utils/assetTransformer"),
9
9
  );
@@ -300,23 +300,35 @@ async function transformJS(file, { config, options, projectRoot }) {
300
300
  },
301
301
  file.code,
302
302
  );
303
- let map = result.rawMappings
304
- ? result.rawMappings.map(_metroSourceMap.toSegmentTuple)
305
- : [];
306
303
  let code = result.code;
304
+ let map;
305
+ let lineCount;
307
306
  if (minify) {
308
- ({ map, code } = await minifyCode(
307
+ let tuples = result.decodedMap
308
+ ? (0, _metroSourceMap.tuplesFromBabelDecodedMap)(result.decodedMap)
309
+ : [];
310
+ ({ map: tuples, code } = await minifyCode(
309
311
  config,
310
312
  projectRoot,
311
313
  file.filename,
312
314
  result.code,
313
315
  file.code,
314
- map,
316
+ tuples,
315
317
  reserved,
316
318
  ));
319
+ ({ lineCount, map: tuples } = countLinesAndTerminateMap(code, tuples));
320
+ map = (0, _metroSourceMap.vlqMapFromTuples)(tuples);
321
+ } else {
322
+ const { lineCount: lines, lastLineColumn } = countLines(code);
323
+ lineCount = lines;
324
+ map = (0, _metroSourceMap.vlqMapFromBabelDecodedMap)(
325
+ result.decodedMap ?? {
326
+ mappings: [],
327
+ names: [],
328
+ },
329
+ [lines, lastLineColumn],
330
+ );
317
331
  }
318
- let lineCount;
319
- ({ lineCount, map } = countLinesAndTerminateMap(code, map));
320
332
  const output = [
321
333
  {
322
334
  data: {
@@ -350,7 +362,9 @@ async function transformAsset(file, context) {
350
362
  }
351
363
  async function transformJSWithBabel(file, context) {
352
364
  const { babelTransformerPath } = context.config;
353
- const transformer = require(babelTransformerPath);
365
+ const mod = require(babelTransformerPath);
366
+ const transformer =
367
+ mod.__esModule === true && "default" in mod ? mod.default : mod;
354
368
  const transformResult = await transformer.transform(
355
369
  getBabelTransformArgs(file, context, [
356
370
  _metroSourceMap.functionMapBabelPlugin,
@@ -406,13 +420,14 @@ async function transformJSON(file, { options, config, projectRoot }) {
406
420
  }
407
421
  let lineCount;
408
422
  ({ lineCount, map } = countLinesAndTerminateMap(code, map));
423
+ const outputMap = (0, _metroSourceMap.vlqMapFromTuples)(map);
409
424
  const output = [
410
425
  {
411
426
  data: {
412
427
  code,
413
428
  functionMap: null,
414
429
  lineCount,
415
- map,
430
+ map: outputMap,
416
431
  },
417
432
  type: jsType,
418
433
  },
@@ -525,7 +540,7 @@ const getCacheKey = (config, opts) => {
525
540
  ].join("$");
526
541
  };
527
542
  exports.getCacheKey = getCacheKey;
528
- function countLinesAndTerminateMap(code, map) {
543
+ function countLines(code) {
529
544
  const NEWLINE = /\r\n?|\n|\u2028|\u2029/g;
530
545
  let lineCount = 1;
531
546
  let lastLineStart = 0;
@@ -533,9 +548,15 @@ function countLinesAndTerminateMap(code, map) {
533
548
  lineCount++;
534
549
  lastLineStart = match.index + match[0].length;
535
550
  }
536
- const lastLineLength = code.length - lastLineStart;
551
+ return {
552
+ lineCount,
553
+ lastLineColumn: code.length - lastLineStart,
554
+ };
555
+ }
556
+ function countLinesAndTerminateMap(code, map) {
557
+ const { lineCount, lastLineColumn } = countLines(code);
537
558
  const lastLineIndex1Based = lineCount;
538
- const lastLineNextColumn0Based = lastLineLength;
559
+ const lastLineNextColumn0Based = lastLineColumn;
539
560
  const lastMapping = map[map.length - 1];
540
561
  const terminatingMapping = [lastLineIndex1Based, lastLineNextColumn0Based];
541
562
  if (
@@ -553,7 +574,3 @@ function countLinesAndTerminateMap(code, map) {
553
574
  map: [...map],
554
575
  };
555
576
  }
556
- var _default = (exports.default = {
557
- getCacheKey,
558
- transform,
559
- });
package/src/index.js.flow CHANGED
@@ -20,6 +20,7 @@ import type {
20
20
  BasicSourceMap,
21
21
  FBSourceFunctionMap,
22
22
  MetroSourceMapSegmentTuple,
23
+ VlqMap,
23
24
  } from 'metro-source-map';
24
25
  import type {
25
26
  ImportExportPluginOptions,
@@ -46,6 +47,9 @@ import {
46
47
  functionMapBabelPlugin,
47
48
  toBabelSegments,
48
49
  toSegmentTuple,
50
+ tuplesFromBabelDecodedMap,
51
+ vlqMapFromBabelDecodedMap,
52
+ vlqMapFromTuples,
49
53
  } from 'metro-source-map';
50
54
  import metroTransformPlugins from 'metro-transform-plugins';
51
55
  import collectDependencies from 'metro/private/ModuleGraph/worker/collectDependencies';
@@ -168,7 +172,7 @@ export type JsOutput = Readonly<{
168
172
  data: Readonly<{
169
173
  code: string,
170
174
  lineCount: number,
171
- map: Array<MetroSourceMapSegmentTuple>,
175
+ map: VlqMap,
172
176
  functionMap: ?FBSourceFunctionMap,
173
177
  }>,
174
178
  type: JSFileType,
@@ -471,23 +475,41 @@ async function transformJS(
471
475
  file.code,
472
476
  );
473
477
 
474
- let map = result.rawMappings ? result.rawMappings.map(toSegmentTuple) : [];
475
478
  let code = result.code;
479
+ let map: VlqMap;
480
+ let lineCount: number;
476
481
 
477
482
  if (minify) {
478
- ({map, code} = await minifyCode(
483
+ // The minifier returns its own map (not Babel's `decodedMap`), so we derive
484
+ // tuples from Babel's eagerly-computed decoded map, hand them to the
485
+ // minifier, then re-encode the resulting tuples to a compact VLQ map.
486
+ let tuples = result.decodedMap
487
+ ? tuplesFromBabelDecodedMap(result.decodedMap)
488
+ : [];
489
+
490
+ ({map: tuples, code} = await minifyCode(
479
491
  config,
480
492
  projectRoot,
481
493
  file.filename,
482
494
  result.code,
483
495
  file.code,
484
- map,
496
+ tuples,
485
497
  reserved,
486
498
  ));
487
- }
488
499
 
489
- let lineCount;
490
- ({lineCount, map} = countLinesAndTerminateMap(code, map));
500
+ ({lineCount, map: tuples} = countLinesAndTerminateMap(code, tuples));
501
+ map = vlqMapFromTuples(tuples);
502
+ } else {
503
+ // Dominant path (e.g. Hermes, which doesn't minify): encode the compact VLQ
504
+ // map straight from Babel's eagerly-computed decoded map, never
505
+ // materialising tuples.
506
+ const {lineCount: lines, lastLineColumn} = countLines(code);
507
+ lineCount = lines;
508
+ map = vlqMapFromBabelDecodedMap(
509
+ result.decodedMap ?? {mappings: [], names: []},
510
+ [lines, lastLineColumn],
511
+ );
512
+ }
491
513
 
492
514
  const output: Array<JsOutput> = [
493
515
  {
@@ -542,7 +564,9 @@ async function transformJSWithBabel(
542
564
  ): Promise<TransformResponse> {
543
565
  const {babelTransformerPath} = context.config;
544
566
  // $FlowFixMe[unsupported-syntax] dynamic require
545
- const transformer: BabelTransformer = require(babelTransformerPath);
567
+ const mod = require(babelTransformerPath);
568
+ const transformer: BabelTransformer =
569
+ mod.__esModule === true && 'default' in mod ? mod.default : mod;
546
570
 
547
571
  const transformResult = await transformer.transform(
548
572
  getBabelTransformArgs(file, context, [
@@ -614,9 +638,12 @@ async function transformJSON(
614
638
 
615
639
  let lineCount;
616
640
  ({lineCount, map} = countLinesAndTerminateMap(code, map));
641
+ // The JSON path builds tuples directly (no Babel `decodedMap`), so re-encode
642
+ // the finished tuples to a compact VLQ map.
643
+ const outputMap = vlqMapFromTuples(map);
617
644
  const output: Array<JsOutput> = [
618
645
  {
619
- data: {code, functionMap: null, lineCount, map},
646
+ data: {code, functionMap: null, lineCount, map: outputMap},
620
647
  type: jsType,
621
648
  },
622
649
  ];
@@ -753,12 +780,9 @@ export const getCacheKey = (
753
780
  ].join('$');
754
781
  };
755
782
 
756
- function countLinesAndTerminateMap(
757
- code: string,
758
- map: ReadonlyArray<MetroSourceMapSegmentTuple>,
759
- ): {
783
+ function countLines(code: string): {
760
784
  lineCount: number,
761
- map: Array<MetroSourceMapSegmentTuple>,
785
+ lastLineColumn: number,
762
786
  } {
763
787
  const NEWLINE = /\r\n?|\n|\u2028|\u2029/g;
764
788
  let lineCount = 1;
@@ -769,9 +793,19 @@ function countLinesAndTerminateMap(
769
793
  lineCount++;
770
794
  lastLineStart = match.index + match[0].length;
771
795
  }
772
- const lastLineLength = code.length - lastLineStart;
796
+ return {lineCount, lastLineColumn: code.length - lastLineStart};
797
+ }
798
+
799
+ function countLinesAndTerminateMap(
800
+ code: string,
801
+ map: ReadonlyArray<MetroSourceMapSegmentTuple>,
802
+ ): {
803
+ lineCount: number,
804
+ map: Array<MetroSourceMapSegmentTuple>,
805
+ } {
806
+ const {lineCount, lastLineColumn} = countLines(code);
773
807
  const lastLineIndex1Based = lineCount;
774
- const lastLineNextColumn0Based = lastLineLength;
808
+ const lastLineNextColumn0Based = lastLineColumn;
775
809
 
776
810
  // If there isn't a mapping at one-past-the-last column of the last line,
777
811
  // add one that maps to nothing. This ensures out-of-bounds lookups hit the
@@ -791,14 +825,3 @@ function countLinesAndTerminateMap(
791
825
  }
792
826
  return {lineCount, map: [...map]};
793
827
  }
794
-
795
- /**
796
- * Backwards-compatibility with CommonJS consumers using interopRequireDefault.
797
- * Do not add to this list.
798
- *
799
- * @deprecated Default import from 'metro-transform-worker' is deprecated, use named exports.
800
- */
801
- export default {
802
- getCacheKey,
803
- transform,
804
- };
@@ -6,7 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = getMinifier;
7
7
  function getMinifier(minifierPath) {
8
8
  try {
9
- return require(minifierPath);
9
+ const mod = require(minifierPath);
10
+ return mod.__esModule === true && "default" in mod ? mod.default : mod;
10
11
  } catch (e) {
11
12
  throw new Error(
12
13
  'A problem occurred while trying to fetch the minifier. Path: "' +
@@ -17,7 +17,8 @@ export default function getMinifier(minifierPath: string): Minifier {
17
17
  // any entry point that accepts them...
18
18
  try {
19
19
  // $FlowFixMe[unsupported-syntax] TODO t0 cannot do require with literal
20
- return require(minifierPath);
20
+ const mod = require(minifierPath);
21
+ return mod.__esModule === true && 'default' in mod ? mod.default : mod;
21
22
  } catch (e) {
22
23
  throw new Error(
23
24
  'A problem occurred while trying to fetch the minifier. Path: "' +