metro-transform-worker 0.80.7 → 0.80.9

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.80.7",
3
+ "version": "0.80.9",
4
4
  "description": "🚇 Transform worker for Metro.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -17,17 +17,17 @@
17
17
  "@babel/generator": "^7.20.0",
18
18
  "@babel/parser": "^7.20.0",
19
19
  "@babel/types": "^7.20.0",
20
- "metro": "0.80.7",
21
- "metro-babel-transformer": "0.80.7",
22
- "metro-cache": "0.80.7",
23
- "metro-cache-key": "0.80.7",
24
- "metro-minify-terser": "0.80.7",
25
- "metro-source-map": "0.80.7",
26
- "metro-transform-plugins": "0.80.7",
20
+ "metro": "0.80.9",
21
+ "metro-babel-transformer": "0.80.9",
22
+ "metro-cache": "0.80.9",
23
+ "metro-cache-key": "0.80.9",
24
+ "metro-minify-terser": "0.80.9",
25
+ "metro-source-map": "0.80.9",
26
+ "metro-transform-plugins": "0.80.9",
27
27
  "nullthrows": "^1.1.1"
28
28
  },
29
29
  "devDependencies": {
30
- "metro-memory-fs": "0.80.7",
30
+ "metro-memory-fs": "0.80.9",
31
31
  "@react-native/metro-babel-transformer": "0.73.11"
32
32
  },
33
33
  "engines": {
package/src/index.d.ts CHANGED
@@ -64,6 +64,8 @@ export type JsTransformerConfig = Readonly<{
64
64
  unstable_compactOutput: boolean;
65
65
  /** Enable `require.context` statements which can be used to import multiple files in a directory. */
66
66
  unstable_allowRequireContext: boolean;
67
+ /** Whether to rename scoped `require` functions to `_$$_REQUIRE`, usually an extraneous operation when serializing to iife (default). */
68
+ unstable_renameRequire?: boolean;
67
69
  }>;
68
70
 
69
71
  export {CustomTransformOptions} from 'metro-babel-transformer';
package/src/index.js CHANGED
@@ -14,7 +14,6 @@ const {
14
14
  toSegmentTuple,
15
15
  } = require("metro-source-map");
16
16
  const metroTransformPlugins = require("metro-transform-plugins");
17
- const countLines = require("metro/src/lib/countLines");
18
17
  const collectDependencies = require("metro/src/ModuleGraph/worker/collectDependencies");
19
18
  const {
20
19
  InvalidRequireCallError: InternalInvalidRequireCallError,
@@ -199,7 +198,8 @@ async function transformJS(file, { config, options, projectRoot }) {
199
198
  importDefault,
200
199
  importAll,
201
200
  dependencyMapName,
202
- config.globalPrefix
201
+ config.globalPrefix,
202
+ config.unstable_renameRequire === false
203
203
  ));
204
204
  }
205
205
  }
@@ -247,11 +247,13 @@ async function transformJS(file, { config, options, projectRoot }) {
247
247
  reserved
248
248
  ));
249
249
  }
250
+ let lineCount;
251
+ ({ lineCount, map } = countLinesAndTerminateMap(code, map));
250
252
  const output = [
251
253
  {
252
254
  data: {
253
255
  code,
254
- lineCount: countLines(code),
256
+ lineCount,
255
257
  map,
256
258
  functionMap: file.functionMap,
257
259
  },
@@ -323,11 +325,13 @@ async function transformJSON(file, { options, config, projectRoot }) {
323
325
  } else {
324
326
  jsType = "js/module";
325
327
  }
328
+ let lineCount;
329
+ ({ lineCount, map } = countLinesAndTerminateMap(code, map));
326
330
  const output = [
327
331
  {
328
332
  data: {
329
333
  code,
330
- lineCount: countLines(code),
334
+ lineCount,
331
335
  map,
332
336
  functionMap: null,
333
337
  },
@@ -410,6 +414,7 @@ module.exports = {
410
414
  getCacheKey: (config) => {
411
415
  const { babelTransformerPath, minifierPath, ...remainingConfig } = config;
412
416
  const filesKey = getCacheKey([
417
+ __filename,
413
418
  require.resolve(babelTransformerPath),
414
419
  require.resolve(minifierPath),
415
420
  require.resolve("./utils/getMinifier"),
@@ -426,3 +431,31 @@ module.exports = {
426
431
  ].join("$");
427
432
  },
428
433
  };
434
+ function countLinesAndTerminateMap(code, map) {
435
+ const NEWLINE = /\r\n?|\n|\u2028|\u2029/g;
436
+ let lineCount = 1;
437
+ let lastLineStart = 0;
438
+ for (const match of code.matchAll(NEWLINE)) {
439
+ lineCount++;
440
+ lastLineStart = match.index + match[0].length;
441
+ }
442
+ const lastLineLength = code.length - lastLineStart;
443
+ const lastLineIndex1Based = lineCount;
444
+ const lastLineNextColumn0Based = lastLineLength;
445
+ const lastMapping = map[map.length - 1];
446
+ const terminatingMapping = [lastLineIndex1Based, lastLineNextColumn0Based];
447
+ if (
448
+ !lastMapping ||
449
+ lastMapping[0] !== terminatingMapping[0] ||
450
+ lastMapping[1] !== terminatingMapping[1]
451
+ ) {
452
+ return {
453
+ lineCount,
454
+ map: map.concat([terminatingMapping]),
455
+ };
456
+ }
457
+ return {
458
+ lineCount,
459
+ map: [...map],
460
+ };
461
+ }
package/src/index.js.flow CHANGED
@@ -44,7 +44,6 @@ const {
44
44
  toSegmentTuple,
45
45
  } = require('metro-source-map');
46
46
  const metroTransformPlugins = require('metro-transform-plugins');
47
- const countLines = require('metro/src/lib/countLines');
48
47
  const collectDependencies = require('metro/src/ModuleGraph/worker/collectDependencies');
49
48
  const {
50
49
  InvalidRequireCallError: InternalInvalidRequireCallError,
@@ -97,6 +96,8 @@ export type JsTransformerConfig = $ReadOnly<{
97
96
  unstable_compactOutput: boolean,
98
97
  /** Enable `require.context` statements which can be used to import multiple files in a directory. */
99
98
  unstable_allowRequireContext: boolean,
99
+ /** Whether to rename scoped `require` functions to `_$$_REQUIRE`, usually an extraneous operation when serializing to iife (default). */
100
+ unstable_renameRequire?: boolean,
100
101
  }>;
101
102
 
102
103
  export type {CustomTransformOptions} from 'metro-babel-transformer';
@@ -388,6 +389,10 @@ async function transformJS(
388
389
  importAll,
389
390
  dependencyMapName,
390
391
  config.globalPrefix,
392
+ // TODO: This config is optional to allow its introduction in a minor
393
+ // release. It should be made non-optional in ConfigT or removed in
394
+ // future.
395
+ config.unstable_renameRequire === false,
391
396
  ));
392
397
  }
393
398
  }
@@ -441,11 +446,14 @@ async function transformJS(
441
446
  ));
442
447
  }
443
448
 
449
+ let lineCount;
450
+ ({lineCount, map} = countLinesAndTerminateMap(code, map));
451
+
444
452
  const output: Array<JsOutput> = [
445
453
  {
446
454
  data: {
447
455
  code,
448
- lineCount: countLines(code),
456
+ lineCount,
449
457
  map,
450
458
  functionMap: file.functionMap,
451
459
  },
@@ -552,9 +560,11 @@ async function transformJSON(
552
560
  jsType = 'js/module';
553
561
  }
554
562
 
563
+ let lineCount;
564
+ ({lineCount, map} = countLinesAndTerminateMap(code, map));
555
565
  const output: Array<JsOutput> = [
556
566
  {
557
- data: {code, lineCount: countLines(code), map, functionMap: null},
567
+ data: {code, lineCount, map, functionMap: null},
558
568
  type: jsType,
559
569
  },
560
570
  ];
@@ -652,6 +662,7 @@ module.exports = {
652
662
  const {babelTransformerPath, minifierPath, ...remainingConfig} = config;
653
663
 
654
664
  const filesKey = getCacheKey([
665
+ __filename,
655
666
  require.resolve(babelTransformerPath),
656
667
  require.resolve(minifierPath),
657
668
  require.resolve('./utils/getMinifier'),
@@ -670,3 +681,42 @@ module.exports = {
670
681
  ].join('$');
671
682
  },
672
683
  };
684
+
685
+ function countLinesAndTerminateMap(
686
+ code: string,
687
+ map: $ReadOnlyArray<MetroSourceMapSegmentTuple>,
688
+ ): {
689
+ lineCount: number,
690
+ map: Array<MetroSourceMapSegmentTuple>,
691
+ } {
692
+ const NEWLINE = /\r\n?|\n|\u2028|\u2029/g;
693
+ let lineCount = 1;
694
+ let lastLineStart = 0;
695
+
696
+ // Count lines and keep track of where the last line starts
697
+ for (const match of code.matchAll(NEWLINE)) {
698
+ lineCount++;
699
+ lastLineStart = match.index + match[0].length;
700
+ }
701
+ const lastLineLength = code.length - lastLineStart;
702
+ const lastLineIndex1Based = lineCount;
703
+ const lastLineNextColumn0Based = lastLineLength;
704
+
705
+ // If there isn't a mapping at one-past-the-last column of the last line,
706
+ // add one that maps to nothing. This ensures out-of-bounds lookups hit the
707
+ // null mapping rather than aliasing to whichever mapping happens to be last.
708
+ // ASSUMPTION: Mappings are generated in order of increasing line and column.
709
+ const lastMapping = map[map.length - 1];
710
+ const terminatingMapping = [lastLineIndex1Based, lastLineNextColumn0Based];
711
+ if (
712
+ !lastMapping ||
713
+ lastMapping[0] !== terminatingMapping[0] ||
714
+ lastMapping[1] !== terminatingMapping[1]
715
+ ) {
716
+ return {
717
+ lineCount,
718
+ map: map.concat([terminatingMapping]),
719
+ };
720
+ }
721
+ return {lineCount, map: [...map]};
722
+ }