Skip to content

Commit b0db193

Browse files
alexander-akaitclaude
authored andcommitted
feat: emit the source map scopes field
A bundler rewrites an imported binding into a namespace read, and a source map only maps positions, so a debugger cannot resolve the name the source used. The Scopes proposal answers that by naming, per scope, the expression each variable evaluates to. Add map({ scopes: true }). A source declares what its names read through a new OriginalSource argument, the chunk stream carries it beside the source content, and the field is built while the mappings are written rather than decoded back out of them.
1 parent 82a4bf0 commit b0db193

19 files changed

Lines changed: 880 additions & 65 deletions

‎.changeset/source-map-scopes.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-sources": minor
3+
---
4+
5+
Add `map({ scopes: true })`, emitting the source map `scopes` field from the bindings a source declares.

‎lib/CachedSource.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ const getCacheKey = (options) => {
118118
if (
119119
/** @type {Options} */ (options).source === undefined &&
120120
/** @type {Options} */ (options).finalSource === undefined &&
121-
/** @type {MapOptions} */ (options).module === undefined
121+
/** @type {MapOptions} */ (options).module === undefined &&
122+
/** @type {MapOptions} */ (options).scopes === undefined
122123
) {
123124
if (columns === undefined) return CACHE_KEY_EMPTY;
124125
return columns ? CACHE_KEY_COLUMNS_TRUE : CACHE_KEY_COLUMNS_FALSE;

‎lib/ConcatSource.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ class ConcatSource extends Source {
277277
);
278278
}
279279
},
280-
(i, source, sourceContent) => {
280+
(i, source, sourceContent, scopeBindings) => {
281281
let globalIndex = sourceMapping.get(source);
282282
if (globalIndex === undefined) {
283283
sourceMapping.set(source, (globalIndex = sourceMapping.size));
284-
onSource(globalIndex, source, sourceContent);
284+
onSource(globalIndex, source, sourceContent, scopeBindings);
285285
}
286286
sourceIndexMapping[i] = globalIndex;
287287
},

‎lib/OriginalSource.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ const {
2424
/** @typedef {import("./helpers/streamChunks").OnName} OnName */
2525
/** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
2626
/** @typedef {import("./helpers/streamChunks").Options} Options */
27+
/** @typedef {import("./helpers/streamChunks").ScopeBindings} ScopeBindings */
2728

2829
class OriginalSource extends Source {
2930
/**
3031
* @param {string | Buffer} value value
3132
* @param {string} name name
33+
* @param {ScopeBindings=} scopeBindings what each name this source declares
34+
* evaluates to in the generated code, for the map's `scopes` field
3235
*/
33-
constructor(value, name) {
36+
constructor(value, name, scopeBindings) {
3437
super();
3538

3639
const isBuffer = Buffer.isBuffer(value);
@@ -47,6 +50,11 @@ class OriginalSource extends Source {
4750
* @type {string}
4851
*/
4952
this._name = name;
53+
/**
54+
* @private
55+
* @type {undefined | ScopeBindings}
56+
*/
57+
this._scopeBindings = scopeBindings;
5058
}
5159

5260
getName() {
@@ -126,7 +134,7 @@ class OriginalSource extends Source {
126134
/** @type {Buffer} */
127135
(this._valueAsBuffer).toString("utf8");
128136
}
129-
onSource(0, this._name, this._value);
137+
onSource(0, this._name, this._value, this._scopeBindings);
130138
const finalSource = Boolean(options && options.finalSource);
131139
if (!options || options.columns !== false) {
132140
// With column info we need to walk every potential token. The

‎lib/ReplaceSource.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,13 +607,13 @@ class ReplaceSource extends Source {
607607
}
608608
pos = endPos;
609609
},
610-
(sourceIndex, source, sourceContent) => {
610+
(sourceIndex, source, sourceContent, scopeBindings) => {
611611
/* istanbul ignore next: non-sequential sourceIndex emission is not produced by any in-tree Source */
612612
while (sourceContents.length < sourceIndex) {
613613
sourceContents.push(undefined);
614614
}
615615
sourceContents[sourceIndex] = sourceContent;
616-
onSource(sourceIndex, source, sourceContent);
616+
onSource(sourceIndex, source, sourceContent, scopeBindings);
617617
},
618618
(nameIndex, name) => {
619619
let globalIndex = nameMapping.get(name);

‎lib/Source.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @typedef {object} MapOptions
1010
* @property {boolean=} columns need columns?
1111
* @property {boolean=} module is module
12+
* @property {boolean=} scopes emit the `scopes` field from the bindings the sources declare
1213
*/
1314

1415
/**
@@ -22,6 +23,7 @@
2223
* @property {string} file file
2324
* @property {string=} debugId debug id
2425
* @property {number[]=} ignoreList ignore list
26+
* @property {string=} scopes encoded `scopes` field of the "Scopes" proposal
2527
*/
2628

2729
/** @typedef {string | Buffer} SourceValue */

‎lib/helpers/getFromStreamChunks.js‎

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

88
const { createMappingsWriter } = require("./createMappingsSerializer");
9+
const { createScopesWriter } = require("./scopes");
910

1011
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
1112
/** @typedef {import("../Source").SourceAndMap} SourceAndMap */
@@ -50,7 +51,9 @@ module.exports.getMap = (source, options) => {
5051
const potentialNames = [];
5152
const mappingsWriter = createMappingsWriter(options);
5253
const addMapping = mappingsWriter.add;
53-
source.streamChunks(
54+
const scopesWriter =
55+
options && options.scopes ? createScopesWriter() : undefined;
56+
const generated = source.streamChunks(
5457
{ ...options, source: false, finalSource: true },
5558
(
5659
chunk,
@@ -69,32 +72,47 @@ module.exports.getMap = (source, options) => {
6972
originalColumn,
7073
nameIndex,
7174
);
75+
if (scopesWriter !== undefined) {
76+
scopesWriter.add(
77+
generatedLine,
78+
generatedColumn,
79+
sourceIndex,
80+
originalLine,
81+
);
82+
}
7283
},
73-
(sourceIndex, source, sourceContent) => {
84+
(sourceIndex, source, sourceContent, scopeBindings) => {
7485
setAtIndex(potentialSources, sourceIndex, source);
7586
if (sourceContent !== undefined) {
7687
setAtIndex(potentialSourcesContent, sourceIndex, sourceContent);
7788
}
89+
if (scopesWriter !== undefined) {
90+
scopesWriter.addSource(sourceIndex, scopeBindings);
91+
}
7892
},
7993
(nameIndex, name) => {
8094
setAtIndex(potentialNames, nameIndex, name);
8195
},
8296
);
8397
const mappings = mappingsWriter.finish();
84-
return mappings.length > 0
85-
? {
86-
version: 3,
87-
file: "x",
88-
mappings,
89-
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
90-
sources: /** @type {string[]} */ (potentialSources),
91-
sourcesContent:
92-
potentialSourcesContent.length > 0
93-
? /** @type {string[]} */ (potentialSourcesContent)
94-
: undefined,
95-
names: /** @type {string[]} */ (potentialNames),
96-
}
97-
: null;
98+
if (mappings.length === 0) return null;
99+
/** @type {RawSourceMap} */
100+
const map = {
101+
version: 3,
102+
file: "x",
103+
mappings,
104+
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
105+
sources: /** @type {string[]} */ (potentialSources),
106+
sourcesContent:
107+
potentialSourcesContent.length > 0
108+
? /** @type {string[]} */ (potentialSourcesContent)
109+
: undefined,
110+
names: /** @type {string[]} */ (potentialNames),
111+
};
112+
if (scopesWriter !== undefined) {
113+
scopesWriter.finish(map, generated.generatedLine);
114+
}
115+
return map;
98116
};
99117

100118
/**
@@ -112,7 +130,9 @@ module.exports.getSourceAndMap = (inputSource, options) => {
112130
const potentialNames = [];
113131
const mappingsWriter = createMappingsWriter(options);
114132
const addMapping = mappingsWriter.add;
115-
const { source } = inputSource.streamChunks(
133+
const scopesWriter =
134+
options && options.scopes ? createScopesWriter() : undefined;
135+
const generated = inputSource.streamChunks(
116136
// `source: true` overrides any `source: false` a caller forwarded
117137
// (e.g. the streamChunks fallback passing getMap options through
118138
// sourceAndMap) — this helper always consumes the source.
@@ -135,8 +155,19 @@ module.exports.getSourceAndMap = (inputSource, options) => {
135155
originalColumn,
136156
nameIndex,
137157
);
158+
if (scopesWriter !== undefined) {
159+
scopesWriter.add(
160+
generatedLine,
161+
generatedColumn,
162+
sourceIndex,
163+
originalLine,
164+
);
165+
}
138166
},
139-
(sourceIndex, source, sourceContent) => {
167+
(sourceIndex, source, sourceContent, scopeBindings) => {
168+
if (scopesWriter !== undefined) {
169+
scopesWriter.addSource(sourceIndex, scopeBindings);
170+
}
140171
while (potentialSources.length < sourceIndex) {
141172
potentialSources.push(null);
142173
}
@@ -156,22 +187,25 @@ module.exports.getSourceAndMap = (inputSource, options) => {
156187
},
157188
);
158189
const mappings = mappingsWriter.finish();
159-
return {
160-
source: source !== undefined ? source : code,
161-
map:
162-
mappings.length > 0
163-
? {
164-
version: 3,
165-
file: "x",
166-
mappings,
167-
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
168-
sources: /** @type {string[]} */ (potentialSources),
169-
sourcesContent:
170-
potentialSourcesContent.length > 0
171-
? /** @type {string[]} */ (potentialSourcesContent)
172-
: undefined,
173-
names: /** @type {string[]} */ (potentialNames),
174-
}
175-
: null,
176-
};
190+
const { source } = generated;
191+
/** @type {RawSourceMap | null} */
192+
let map = null;
193+
if (mappings.length > 0) {
194+
map = {
195+
version: 3,
196+
file: "x",
197+
mappings,
198+
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
199+
sources: /** @type {string[]} */ (potentialSources),
200+
sourcesContent:
201+
potentialSourcesContent.length > 0
202+
? /** @type {string[]} */ (potentialSourcesContent)
203+
: undefined,
204+
names: /** @type {string[]} */ (potentialNames),
205+
};
206+
if (scopesWriter !== undefined) {
207+
scopesWriter.finish(map, generated.generatedLine);
208+
}
209+
}
210+
return { source: source !== undefined ? source : code, map };
177211
};

0 commit comments

Comments
 (0)