@jsenv/sourcemap 1.4.2 → 1.4.4

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": "@jsenv/sourcemap",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,8 +13,10 @@
13
13
  "./*": "./*"
14
14
  },
15
15
  "dependencies": {
16
+ "@jridgewell/gen-mapping": "0.3.13",
17
+ "@jridgewell/trace-mapping": "0.3.31",
16
18
  "@jsenv/urls": "2.9.10",
17
- "magic-string": "1.0.0",
19
+ "magic-string": "1.2.3",
18
20
  "source-map-js": "1.2.1"
19
21
  },
20
22
  "publishConfig": {
@@ -32,14 +32,28 @@ export const createMagicSource = (content) => {
32
32
  };
33
33
  }
34
34
  const code = magicString.toString();
35
- const map = magicString.generateMap({
36
- hires: true,
37
- includeContent: true,
38
- source,
39
- });
35
+ // The map is generated when first read, not here: a consumer that
36
+ // throws sourcemaps away (a build with sourcemaps "none") never reads
37
+ // it, and generating a high resolution map costs as much as the edit.
38
+ let map;
40
39
  return {
41
40
  content: code,
42
- sourcemap: map,
41
+ get sourcemap() {
42
+ if (map === undefined) {
43
+ // "boundary" = a mapping per word boundary. Per-character maps
44
+ // (hires: true) only add sub-word precision and cost ~25% of a
45
+ // package build in generation + composition + GC; line-level maps
46
+ // (hires: false) are cheaper still but collapse heavily rewritten
47
+ // lines (JSX) to their edit points. Word precision is what
48
+ // breakpoints, stack traces and devtools hovers actually consume.
49
+ map = magicString.generateMap({
50
+ hires: "boundary",
51
+ includeContent: true,
52
+ source,
53
+ });
54
+ }
55
+ return map;
56
+ },
43
57
  };
44
58
  },
45
59
  };
@@ -1,10 +1,25 @@
1
1
  /*
2
- * https://github.com/mozilla/source-map#sourcemapgenerator
2
+ * https://github.com/jridgewell/trace-mapping
3
+ * https://github.com/jridgewell/gen-mapping
4
+ *
5
+ * @jridgewell/* rather than source-map-js (still used by
6
+ * original_position.js): composing is the hot spot of a build with
7
+ * sourcemaps, and source-map-js sorts + binary searches its mappings on
8
+ * every lookup where trace-mapping walks them in order. On @jsenv/navi
9
+ * (3MB bundle, 18MB map) the build went from 11s to 4.6s for a map that is
10
+ * byte for byte the one source-map-js produced.
3
11
  */
4
12
 
5
- import { requireSourcemap } from "./require_sourcemap.js";
6
-
7
- const { SourceMapConsumer, SourceMapGenerator } = requireSourcemap();
13
+ import {
14
+ addMapping,
15
+ GenMapping,
16
+ toEncodedMap,
17
+ } from "@jridgewell/gen-mapping";
18
+ import {
19
+ eachMapping,
20
+ originalPositionFor,
21
+ TraceMap,
22
+ } from "@jridgewell/trace-mapping";
8
23
 
9
24
  // "first" maps an intermediate content back to the true original source(s);
10
25
  // "second" maps the final content back to that same intermediate content
@@ -24,10 +39,11 @@ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
24
39
  if (!secondSourcemap) {
25
40
  return firstSourcemap;
26
41
  }
27
- const sourcemapGenerator = new SourceMapGenerator();
28
- const firstSourcemapConsumer = new SourceMapConsumer(firstSourcemap);
29
- const secondSourcemapConsumer = new SourceMapConsumer(secondSourcemap);
30
- secondSourcemapConsumer.eachMapping(
42
+ const genMapping = new GenMapping();
43
+ const firstTraceMap = new TraceMap(firstSourcemap);
44
+ const secondTraceMap = new TraceMap(secondSourcemap);
45
+ eachMapping(
46
+ secondTraceMap,
31
47
  ({
32
48
  generatedLine,
33
49
  generatedColumn,
@@ -40,7 +56,7 @@ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
40
56
  // content) — nothing to chain through "first", leave unmapped.
41
57
  return;
42
58
  }
43
- const original = firstSourcemapConsumer.originalPositionFor({
59
+ const original = originalPositionFor(firstTraceMap, {
44
60
  line: originalLine,
45
61
  column: originalColumn,
46
62
  });
@@ -49,7 +65,10 @@ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
49
65
  // — leave unmapped rather than guessing.
50
66
  return;
51
67
  }
52
- sourcemapGenerator.addMapping({
68
+ // addMapping, not maybeAddMapping: the latter drops mappings it
69
+ // considers redundant with the previous one, which shrinks the map
70
+ // but loses positions (a real fidelity change, to decide on its own).
71
+ addMapping(genMapping, {
53
72
  generated: { line: generatedLine, column: generatedColumn },
54
73
  original: { line: original.line, column: original.column },
55
74
  source: original.source,
@@ -57,12 +76,16 @@ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
57
76
  });
58
77
  },
59
78
  );
60
- const sourcemap = sourcemapGenerator.toJSON();
61
- const sources = [];
79
+ const encodedMap = toEncodedMap(genMapping);
80
+ const sourcemap = {
81
+ version: 3,
82
+ sources: [...encodedMap.sources],
83
+ names: [...encodedMap.names],
84
+ mappings: encodedMap.mappings,
85
+ };
62
86
  const sourcesContent = [];
63
87
  const firstSourcesContent = firstSourcemap.sourcesContent;
64
88
  sourcemap.sources.forEach((source) => {
65
- sources.push(source);
66
89
  if (firstSourcesContent) {
67
90
  const firstSourceIndex = firstSourcemap.sources.indexOf(source);
68
91
  if (firstSourceIndex > -1) {
@@ -72,7 +95,6 @@ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
72
95
  }
73
96
  sourcesContent.push(null);
74
97
  });
75
- sourcemap.sources = sources;
76
98
  sourcemap.sourcesContent = sourcesContent;
77
99
  return sourcemap;
78
100
  };