metro-transform-worker 0.80.7 → 0.80.8

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.8",
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.8",
21
+ "metro-babel-transformer": "0.80.8",
22
+ "metro-cache": "0.80.8",
23
+ "metro-cache-key": "0.80.8",
24
+ "metro-minify-terser": "0.80.8",
25
+ "metro-source-map": "0.80.8",
26
+ "metro-transform-plugins": "0.80.8",
27
27
  "nullthrows": "^1.1.1"
28
28
  },
29
29
  "devDependencies": {
30
- "metro-memory-fs": "0.80.7",
30
+ "metro-memory-fs": "0.80.8",
31
31
  "@react-native/metro-babel-transformer": "0.73.11"
32
32
  },
33
33
  "engines": {
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,
@@ -247,11 +246,13 @@ async function transformJS(file, { config, options, projectRoot }) {
247
246
  reserved
248
247
  ));
249
248
  }
249
+ let lineCount;
250
+ ({ lineCount, map } = countLinesAndTerminateMap(code, map));
250
251
  const output = [
251
252
  {
252
253
  data: {
253
254
  code,
254
- lineCount: countLines(code),
255
+ lineCount,
255
256
  map,
256
257
  functionMap: file.functionMap,
257
258
  },
@@ -323,11 +324,13 @@ async function transformJSON(file, { options, config, projectRoot }) {
323
324
  } else {
324
325
  jsType = "js/module";
325
326
  }
327
+ let lineCount;
328
+ ({ lineCount, map } = countLinesAndTerminateMap(code, map));
326
329
  const output = [
327
330
  {
328
331
  data: {
329
332
  code,
330
- lineCount: countLines(code),
333
+ lineCount,
331
334
  map,
332
335
  functionMap: null,
333
336
  },
@@ -410,6 +413,7 @@ module.exports = {
410
413
  getCacheKey: (config) => {
411
414
  const { babelTransformerPath, minifierPath, ...remainingConfig } = config;
412
415
  const filesKey = getCacheKey([
416
+ __filename,
413
417
  require.resolve(babelTransformerPath),
414
418
  require.resolve(minifierPath),
415
419
  require.resolve("./utils/getMinifier"),
@@ -426,3 +430,31 @@ module.exports = {
426
430
  ].join("$");
427
431
  },
428
432
  };
433
+ function countLinesAndTerminateMap(code, map) {
434
+ const NEWLINE = /\r\n?|\n|\u2028|\u2029/g;
435
+ let lineCount = 1;
436
+ let lastLineStart = 0;
437
+ for (const match of code.matchAll(NEWLINE)) {
438
+ lineCount++;
439
+ lastLineStart = match.index + match[0].length;
440
+ }
441
+ const lastLineLength = code.length - lastLineStart;
442
+ const lastLineIndex1Based = lineCount;
443
+ const lastLineNextColumn0Based = lastLineLength;
444
+ const lastMapping = map[map.length - 1];
445
+ const terminatingMapping = [lastLineIndex1Based, lastLineNextColumn0Based];
446
+ if (
447
+ !lastMapping ||
448
+ lastMapping[0] !== terminatingMapping[0] ||
449
+ lastMapping[1] !== terminatingMapping[1]
450
+ ) {
451
+ return {
452
+ lineCount,
453
+ map: map.concat([terminatingMapping]),
454
+ };
455
+ }
456
+ return {
457
+ lineCount,
458
+ map: [...map],
459
+ };
460
+ }
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,
@@ -441,11 +440,14 @@ async function transformJS(
441
440
  ));
442
441
  }
443
442
 
443
+ let lineCount;
444
+ ({lineCount, map} = countLinesAndTerminateMap(code, map));
445
+
444
446
  const output: Array<JsOutput> = [
445
447
  {
446
448
  data: {
447
449
  code,
448
- lineCount: countLines(code),
450
+ lineCount,
449
451
  map,
450
452
  functionMap: file.functionMap,
451
453
  },
@@ -552,9 +554,11 @@ async function transformJSON(
552
554
  jsType = 'js/module';
553
555
  }
554
556
 
557
+ let lineCount;
558
+ ({lineCount, map} = countLinesAndTerminateMap(code, map));
555
559
  const output: Array<JsOutput> = [
556
560
  {
557
- data: {code, lineCount: countLines(code), map, functionMap: null},
561
+ data: {code, lineCount, map, functionMap: null},
558
562
  type: jsType,
559
563
  },
560
564
  ];
@@ -652,6 +656,7 @@ module.exports = {
652
656
  const {babelTransformerPath, minifierPath, ...remainingConfig} = config;
653
657
 
654
658
  const filesKey = getCacheKey([
659
+ __filename,
655
660
  require.resolve(babelTransformerPath),
656
661
  require.resolve(minifierPath),
657
662
  require.resolve('./utils/getMinifier'),
@@ -670,3 +675,42 @@ module.exports = {
670
675
  ].join('$');
671
676
  },
672
677
  };
678
+
679
+ function countLinesAndTerminateMap(
680
+ code: string,
681
+ map: $ReadOnlyArray<MetroSourceMapSegmentTuple>,
682
+ ): {
683
+ lineCount: number,
684
+ map: Array<MetroSourceMapSegmentTuple>,
685
+ } {
686
+ const NEWLINE = /\r\n?|\n|\u2028|\u2029/g;
687
+ let lineCount = 1;
688
+ let lastLineStart = 0;
689
+
690
+ // Count lines and keep track of where the last line starts
691
+ for (const match of code.matchAll(NEWLINE)) {
692
+ lineCount++;
693
+ lastLineStart = match.index + match[0].length;
694
+ }
695
+ const lastLineLength = code.length - lastLineStart;
696
+ const lastLineIndex1Based = lineCount;
697
+ const lastLineNextColumn0Based = lastLineLength;
698
+
699
+ // If there isn't a mapping at one-past-the-last column of the last line,
700
+ // add one that maps to nothing. This ensures out-of-bounds lookups hit the
701
+ // null mapping rather than aliasing to whichever mapping happens to be last.
702
+ // ASSUMPTION: Mappings are generated in order of increasing line and column.
703
+ const lastMapping = map[map.length - 1];
704
+ const terminatingMapping = [lastLineIndex1Based, lastLineNextColumn0Based];
705
+ if (
706
+ !lastMapping ||
707
+ lastMapping[0] !== terminatingMapping[0] ||
708
+ lastMapping[1] !== terminatingMapping[1]
709
+ ) {
710
+ return {
711
+ lineCount,
712
+ map: map.concat([terminatingMapping]),
713
+ };
714
+ }
715
+ return {lineCount, map: [...map]};
716
+ }