@jsenv/sourcemap 1.3.19 → 1.4.1

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.3.19",
3
+ "version": "1.4.1",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@jsenv/urls": "2.9.10",
17
- "magic-string": "0.30.21",
17
+ "magic-string": "1.0.0",
18
18
  "source-map-js": "1.2.1"
19
19
  },
20
20
  "publishConfig": {
@@ -6,6 +6,14 @@ import { requireSourcemap } from "./require_sourcemap.js";
6
6
 
7
7
  const { SourceMapConsumer, SourceMapGenerator } = requireSourcemap();
8
8
 
9
+ // "first" maps an intermediate content back to the true original source(s);
10
+ // "second" maps the final content back to that same intermediate content
11
+ // (its "original" positions live in the coordinate space "first" was
12
+ // generated for). Composing them means chaining through "first" for every
13
+ // mapping in "second" — not merging both mapping sets side by side: they
14
+ // don't share a single coordinate space, so naively adding both to the same
15
+ // generator produces mappings that silently collide/override each other
16
+ // wherever "second" happens to cover a position "first" also maps.
9
17
  export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
10
18
  if (!firstSourcemap && !secondSourcemap) {
11
19
  return null;
@@ -19,28 +27,42 @@ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
19
27
  const sourcemapGenerator = new SourceMapGenerator();
20
28
  const firstSourcemapConsumer = new SourceMapConsumer(firstSourcemap);
21
29
  const secondSourcemapConsumer = new SourceMapConsumer(secondSourcemap);
22
- const firstMappings = readMappings(firstSourcemapConsumer);
23
- firstMappings.forEach((mapping) => {
24
- sourcemapGenerator.addMapping(mapping);
25
- });
26
- const secondMappings = readMappings(secondSourcemapConsumer);
27
- secondMappings.forEach((mapping) => {
28
- sourcemapGenerator.addMapping(mapping);
29
- });
30
+ secondSourcemapConsumer.eachMapping(
31
+ ({
32
+ generatedLine,
33
+ generatedColumn,
34
+ originalLine,
35
+ originalColumn,
36
+ name,
37
+ }) => {
38
+ if (typeof originalColumn !== "number") {
39
+ // "second" has no original position here (e.g. injected/synthetic
40
+ // content) — nothing to chain through "first", leave unmapped.
41
+ return;
42
+ }
43
+ const original = firstSourcemapConsumer.originalPositionFor({
44
+ line: originalLine,
45
+ column: originalColumn,
46
+ });
47
+ if (original.source === null) {
48
+ // "first" has no mapping covering that intermediate position either
49
+ // — leave unmapped rather than guessing.
50
+ return;
51
+ }
52
+ sourcemapGenerator.addMapping({
53
+ generated: { line: generatedLine, column: generatedColumn },
54
+ original: { line: original.line, column: original.column },
55
+ source: original.source,
56
+ name: original.name || name || undefined,
57
+ });
58
+ },
59
+ );
30
60
  const sourcemap = sourcemapGenerator.toJSON();
31
61
  const sources = [];
32
62
  const sourcesContent = [];
33
63
  const firstSourcesContent = firstSourcemap.sourcesContent;
34
- const secondSourcesContent = secondSourcemap.sourcesContent;
35
64
  sourcemap.sources.forEach((source) => {
36
65
  sources.push(source);
37
- if (secondSourcesContent) {
38
- const secondSourceIndex = secondSourcemap.sources.indexOf(source);
39
- if (secondSourceIndex > -1) {
40
- sourcesContent.push(secondSourcesContent[secondSourceIndex]);
41
- return;
42
- }
43
- }
44
66
  if (firstSourcesContent) {
45
67
  const firstSourceIndex = firstSourcemap.sources.indexOf(source);
46
68
  if (firstSourceIndex > -1) {
@@ -54,34 +76,3 @@ export const composeTwoSourcemaps = (firstSourcemap, secondSourcemap) => {
54
76
  sourcemap.sourcesContent = sourcesContent;
55
77
  return sourcemap;
56
78
  };
57
-
58
- const readMappings = (consumer) => {
59
- const mappings = [];
60
- consumer.eachMapping(
61
- ({
62
- originalColumn,
63
- originalLine,
64
- generatedColumn,
65
- generatedLine,
66
- source,
67
- name,
68
- }) => {
69
- mappings.push({
70
- original:
71
- typeof originalColumn === "number"
72
- ? {
73
- column: originalColumn,
74
- line: originalLine,
75
- }
76
- : undefined,
77
- generated: {
78
- column: generatedColumn,
79
- line: generatedLine,
80
- },
81
- source: typeof originalColumn === "number" ? source : undefined,
82
- name,
83
- });
84
- },
85
- );
86
- return mappings;
87
- };