Skip to content

Commit 0d685d4

Browse files
authored
Merge pull request #15413 from cool-little-fish/fix-15274
fix: use relative path in source map for context module
2 parents 5a26b7c + a9cf0d0 commit 0d685d4

9 files changed

Lines changed: 53 additions & 8 deletions

File tree

lib/Compilation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3328,7 +3328,8 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
33283328
dependencyTemplates,
33293329
runtimeTemplate,
33303330
runtime,
3331-
codeGenerationResults: results
3331+
codeGenerationResults: results,
3332+
compilation: this
33323333
});
33333334
} catch (err) {
33343335
errors.push(new CodeGenerationError(module, err));

lib/ContextModule.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ const {
1919
keepOriginalOrder,
2020
compareModulesById
2121
} = require("./util/comparators");
22-
const { contextify, parseResource } = require("./util/identifier");
22+
const {
23+
contextify,
24+
parseResource,
25+
makePathsRelative
26+
} = require("./util/identifier");
2327
const makeSerializable = require("./util/makeSerializable");
2428

2529
/** @typedef {import("webpack-sources").Source} Source */
@@ -1066,9 +1070,21 @@ module.exports = webpackEmptyAsyncContext;`;
10661070
return this.getSourceForEmptyContext(id, runtimeTemplate);
10671071
}
10681072

1069-
getSource(sourceString) {
1073+
/**
1074+
* @param {string} sourceString source content
1075+
* @param {Compilation=} compilation the compilation
1076+
* @returns {Source} generated source
1077+
*/
1078+
getSource(sourceString, compilation) {
10701079
if (this.useSourceMap || this.useSimpleSourceMap) {
1071-
return new OriginalSource(sourceString, this.identifier());
1080+
return new OriginalSource(
1081+
sourceString,
1082+
`webpack://${makePathsRelative(
1083+
(compilation && compilation.compiler.context) || "",
1084+
this.identifier(),
1085+
compilation && compilation.compiler.root
1086+
)}`
1087+
);
10721088
}
10731089
return new RawSource(sourceString);
10741090
}
@@ -1078,11 +1094,14 @@ module.exports = webpackEmptyAsyncContext;`;
10781094
* @returns {CodeGenerationResult} result
10791095
*/
10801096
codeGeneration(context) {
1081-
const { chunkGraph } = context;
1097+
const { chunkGraph, compilation } = context;
10821098
const sources = new Map();
10831099
sources.set(
10841100
"javascript",
1085-
this.getSource(this.getSourceString(this.options.mode, context))
1101+
this.getSource(
1102+
this.getSourceString(this.options.mode, context),
1103+
compilation
1104+
)
10861105
);
10871106
const set = new Set();
10881107
const allDeps = /** @type {ContextElementDependency[]} */ (

lib/Module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const makeSerializable = require("./util/makeSerializable");
4949
* @property {string=} type the type of source that should be generated
5050
*/
5151

52+
// TODO webpack 6: compilation will be required in CodeGenerationContext
5253
/**
5354
* @typedef {Object} CodeGenerationContext
5455
* @property {DependencyTemplates} dependencyTemplates the dependency templates
@@ -58,6 +59,7 @@ const makeSerializable = require("./util/makeSerializable");
5859
* @property {RuntimeSpec} runtime the runtimes code should be generated for
5960
* @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
6061
* @property {CodeGenerationResults} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
62+
* @property {Compilation=} compilation the compilation
6163
*/
6264

6365
/**

test/__snapshots__/StatsTestCases.basictest.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ exports[`StatsTestCases should print correct stats for concat-and-sideeffects 1`
738738

739739
exports[`StatsTestCases should print correct stats for context-independence 1`] = `
740740
"asset main-1aad2f42f93e93c4e0b4.js 12.7 KiB [emitted] [immutable] (name: main)
741-
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11.1 KiB [emitted] [dev] (auxiliary name: main)
741+
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11 KiB [emitted] [dev] (auxiliary name: main)
742742
asset 695-4dd37417c69a0af66bac.js 455 bytes [emitted] [immutable]
743743
sourceMap 695-4dd37417c69a0af66bac.js.map 342 bytes [emitted] [dev]
744744
runtime modules 6.6 KiB 9 modules
@@ -754,7 +754,7 @@ built modules 500 bytes [built]
754754
webpack x.x.x compiled successfully in X ms
755755
756756
asset main-1aad2f42f93e93c4e0b4.js 12.7 KiB [emitted] [immutable] (name: main)
757-
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11.1 KiB [emitted] [dev] (auxiliary name: main)
757+
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11 KiB [emitted] [dev] (auxiliary name: main)
758758
asset 695-4dd37417c69a0af66bac.js 455 bytes [emitted] [immutable]
759759
sourceMap 695-4dd37417c69a0af66bac.js.map 342 bytes [emitted] [dev]
760760
runtime modules 6.6 KiB 9 modules
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "a";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "b";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const foo = Math.random() > 0.5 ? "a" : "b";
2+
require(`./foo/${foo}.js`);
3+
4+
it("context module should use relative path in source map file", () => {
5+
var fs = require("fs");
6+
var source = fs.readFileSync(__filename + ".map", "utf-8");
7+
var map = JSON.parse(source);
8+
expect(map.sources).toContain("webpack:///./foo/ sync ^\\.\\/.*\\.js$");
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
node: {
3+
__dirname: false,
4+
__filename: false
5+
},
6+
devtool: "source-map"
7+
};

types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@ declare interface CodeGenerationContext {
12391239
* code generation results of other modules (need to have a codeGenerationDependency to use that)
12401240
*/
12411241
codeGenerationResults: CodeGenerationResults;
1242+
1243+
/**
1244+
* the compilation
1245+
*/
1246+
compilation?: Compilation;
12421247
}
12431248
declare interface CodeGenerationResult {
12441249
/**

0 commit comments

Comments
 (0)