Skip to content

Commit 9fed212

Browse files
fix: keep scopes correct for source-less segments and a warm cache
`collectSourceScopes` read the open run before checking there was one, so a `mappings` that begins with a source-less segment, or carries two in a row, threw a TypeError out of asset processing. Both shapes are what `ConcatSource` and `ReplaceSource` emit around unmapped content. Binding names are also recorded at parse time whatever the devtool is: the module the cache restores is never parsed again, and the option that reads them is chosen afterwards. Enabling scopes now moves the dependency-templates hash too, so code generation cached by a build without them is not reused.
1 parent 3658eca commit 9fed212

5 files changed

Lines changed: 55 additions & 19 deletions

File tree

‎lib/EvalSourceMapDevToolPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class EvalSourceMapDevToolPlugin {
8989
const options = this.options;
9090
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
9191
if (options.scopes) {
92-
enableImportBindingScopes(compilation.moduleGraph);
92+
enableImportBindingScopes(compilation);
9393
}
9494
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
9595
new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);

‎lib/SourceMapDevToolPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class SourceMapDevToolPlugin {
330330

331331
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
332332
if (options.scopes) {
333-
enableImportBindingScopes(compilation.moduleGraph);
333+
enableImportBindingScopes(compilation);
334334
}
335335
new SourceMapDevToolModuleOptionsPlugin(options).apply(compilation);
336336

‎lib/dependencies/HarmonyImportDependencyParserPlugin.js‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const {
1313
getImportAttributes
1414
} = require("../javascript/JavascriptParser");
1515
const { getInnerGraphUtils } = require("../optimize/InnerGraph");
16-
const { isImportBindingScopesEnabled } = require("../util/sourceMapScopes");
1716
const ConstDependency = require("./ConstDependency");
1817
const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
1918
const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
@@ -246,18 +245,15 @@ class HarmonyImportDependencyParserPlugin {
246245
lastSideEffectDependency = undefined;
247246
});
248247
parser.hooks.finish.tap(PLUGIN_NAME, () => {
249-
const recordDeclared = isImportBindingScopesEnabled(
250-
parser.state.compilation.moduleGraph
251-
);
252248
for (const settings of declaredSpecifiers) {
253249
const dependency = settings.dependency;
254250
if (dependency === undefined) continue;
255-
if (recordDeclared) {
256-
if (dependency.declaredSpecifiers === undefined) {
257-
dependency.declaredSpecifiers = [];
258-
}
259-
dependency.declaredSpecifiers.push([settings.ids, settings.name]);
251+
// Recorded whatever the devtool is: a module the cache restores is
252+
// never parsed again, and the option that reads this is chosen after.
253+
if (dependency.declaredSpecifiers === undefined) {
254+
dependency.declaredSpecifiers = [];
260255
}
256+
dependency.declaredSpecifiers.push([settings.ids, settings.name]);
261257
// A namespace import names no export, so there is nothing to resolve
262258
// for it and nothing to report.
263259
if (settings.used || settings.ids.length === 0) continue;

‎lib/util/sourceMapScopes.js‎

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

88
/** @import { RawSourceMap } from "webpack-sources" */
9+
/** @import Compilation from "../Compilation" */
910
/** @import ModuleGraph from "../ModuleGraph" */
1011

1112
/**
@@ -170,10 +171,11 @@ const collectSourceScopes = (mappings, sourceCount) => {
170171
const lastLine = forEachMapping(
171172
mappings,
172173
(generatedLine, generatedColumn, sourceIndex, originalLine) => {
173-
if (sourceIndex === openSource) {
174-
/** @type {SourceScope} */
175-
(openScope).originalEnd.line = Math.max(
176-
/** @type {SourceScope} */ (openScope).originalEnd.line,
174+
// A segment naming no source reports -1, which is also what no open
175+
// run reports, so the run has to be there before it is extended.
176+
if (openScope !== undefined && sourceIndex === openSource) {
177+
openScope.originalEnd.line = Math.max(
178+
openScope.originalEnd.line,
177179
originalLine + 1
178180
);
179181
return;
@@ -379,11 +381,14 @@ const collectingModuleGraphs = new WeakSet();
379381

380382
/**
381383
* Asks code generation to record the expression behind each imported binding.
382-
* @param {ModuleGraph} moduleGraph module graph
384+
* @param {Compilation} compilation the compilation
383385
* @returns {void}
384386
*/
385-
const enableImportBindingScopes = (moduleGraph) => {
386-
collectingModuleGraphs.add(moduleGraph);
387+
const enableImportBindingScopes = (compilation) => {
388+
collectingModuleGraphs.add(compilation.moduleGraph);
389+
// Code generation cached by a build without scopes carries no binding
390+
// expressions, and nothing else in its key says the devtool changed.
391+
compilation.dependencyTemplates.updateHash("source-map-scopes");
387392
};
388393

389394
/**

‎test/sourceMapScopes.unittest.js‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"use strict";
22

3+
const DependencyTemplates = require("../lib/DependencyTemplates");
34
const {
45
addScopesToSourceMap,
56
collectSourceScopes,
6-
encodeScopes
7+
enableImportBindingScopes,
8+
encodeScopes,
9+
isImportBindingScopesEnabled
710
} = require("../lib/util/sourceMapScopes");
811

912
describe("sourceMapScopes", () => {
@@ -44,6 +47,22 @@ describe("sourceMapScopes", () => {
4447
expect(scopes[0].rangeEnds[0]).toEqual({ line: 1, column: 0 });
4548
});
4649

50+
it("opens no run for a source-less segment before any mapped one", () => {
51+
/* cspell:disable-next-line */
52+
const scopes = collectSourceScopes("A;AAAA", 1);
53+
54+
expect(scopes).toHaveLength(1);
55+
expect(scopes[0].rangeStarts).toEqual([{ line: 1, column: 0 }]);
56+
});
57+
58+
it("survives source-less segments in a row", () => {
59+
/* cspell:disable-next-line */
60+
const scopes = collectSourceScopes("AAAA;A;A", 1);
61+
62+
expect(scopes).toHaveLength(1);
63+
expect(scopes[0].rangeEnds).toEqual([{ line: 1, column: 0 }]);
64+
});
65+
4766
it("stops at a digit outside the alphabet rather than misplacing the rest", () => {
4867
expect(collectSourceScopes("AAAA;!!!", 1)).toHaveLength(1);
4968
});
@@ -122,6 +141,22 @@ describe("sourceMapScopes", () => {
122141
});
123142
});
124143

144+
describe("enableImportBindingScopes", () => {
145+
it("moves the code generation cache key, so a build without scopes is not reused", () => {
146+
const dependencyTemplates = new DependencyTemplates();
147+
const before = dependencyTemplates.getHash();
148+
const compilation =
149+
/** @type {import("../lib/Compilation")} */
150+
(/** @type {unknown} */ ({ moduleGraph: {}, dependencyTemplates }));
151+
152+
expect(isImportBindingScopesEnabled(compilation.moduleGraph)).toBe(false);
153+
enableImportBindingScopes(compilation);
154+
155+
expect(isImportBindingScopesEnabled(compilation.moduleGraph)).toBe(true);
156+
expect(dependencyTemplates.getHash()).not.toBe(before);
157+
});
158+
});
159+
125160
describe("addScopesToSourceMap", () => {
126161
/**
127162
* @returns {import("../lib/util/sourceMapScopes").ScopedSourceMap} a two-source map

0 commit comments

Comments
 (0)