Skip to content

Commit 6b9884e

Browse files
committed
handle webpack:// prefix in SourceMap sources
1 parent dcc45dd commit 6b9884e

23 files changed

Lines changed: 411 additions & 18 deletions

lib/EvalSourceMapDevToolModuleTemplatePlugin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const { RawSource } = require("webpack-sources");
88
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
9+
const { absolutify } = require("./util/identifier");
910

1011
const cache = new WeakMap();
1112

@@ -60,7 +61,11 @@ class EvalSourceMapDevToolModuleTemplatePlugin {
6061
obj[key] = sourceMap[key];
6162
return obj;
6263
}, {});
64+
const context = this.compilation.compiler.options.context;
6365
const modules = sourceMap.sources.map(source => {
66+
if (source.startsWith("webpack://")) {
67+
source = absolutify(context, source.slice(10));
68+
}
6469
const module = self.compilation.findModule(source);
6570
return module || source;
6671
});

lib/SourceMapDevToolPlugin.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { ConcatSource, RawSource } = require("webpack-sources");
99
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
1010
const SourceMapDevToolModuleOptionsPlugin = require("./SourceMapDevToolModuleOptionsPlugin");
1111
const createHash = require("./util/createHash");
12+
const { absolutify } = require("./util/identifier");
1213

1314
const validateOptions = require("schema-utils");
1415
const schema = require("../schemas/plugins/SourceMapDevToolPlugin.json");
@@ -68,16 +69,24 @@ const getTaskForFile = (file, asset, chunk, options, compilation) => {
6869
sourceMap = asset.map(options);
6970
source = asset.source();
7071
}
71-
if (sourceMap) {
72-
return {
73-
chunk,
74-
file,
75-
asset,
76-
source,
77-
sourceMap,
78-
modules: undefined
79-
};
80-
}
72+
if (!sourceMap || typeof source !== "string") return;
73+
const context = compilation.options.context;
74+
const modules = sourceMap.sources.map(source => {
75+
if (source.startsWith("webpack://")) {
76+
source = absolutify(context, source.slice(10));
77+
}
78+
const module = compilation.findModule(source);
79+
return module || source;
80+
});
81+
82+
return {
83+
chunk,
84+
file,
85+
asset,
86+
source,
87+
sourceMap,
88+
modules
89+
};
8190
};
8291

8392
class SourceMapDevToolPlugin {
@@ -212,10 +221,7 @@ class SourceMapDevToolPlugin {
212221
);
213222

214223
if (task) {
215-
const modules = task.sourceMap.sources.map(source => {
216-
const module = compilation.findModule(source);
217-
return module || source;
218-
});
224+
const modules = task.modules;
219225

220226
for (let idx = 0; idx < modules.length; idx++) {
221227
const module = modules[idx];
@@ -234,8 +240,6 @@ class SourceMapDevToolPlugin {
234240
}
235241
}
236242

237-
task.modules = modules;
238-
239243
tasks.push(task);
240244
}
241245
});

lib/util/identifier.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
"use strict";
22
const path = require("path");
33

4+
/**
5+
* @param {string} context context for relative path
6+
* @param {string} relativePath path
7+
* @returns {string} absolute path
8+
*/
9+
const requestToAbsolute = (context, relativePath) => {
10+
if (relativePath.startsWith("./") || relativePath.startsWith("../"))
11+
return path.join(context, relativePath);
12+
return relativePath;
13+
};
14+
415
/**
516
* @typedef {Object} MakeRelativePathsCache
617
* @property {Map<string, Map<string, string>>=} relativePaths
@@ -100,3 +111,17 @@ exports.contextify = (context, request) => {
100111
})
101112
.join("!");
102113
};
114+
115+
/**
116+
* @param {string} context absolute context path
117+
* @param {string} request any request string
118+
* @returns {string} a new request string using absolute paths when possible
119+
*/
120+
const _absolutify = (context, request) => {
121+
return request
122+
.split("!")
123+
.map(r => requestToAbsolute(context, r))
124+
.join("!");
125+
};
126+
127+
exports.absolutify = _absolutify;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
"webpack-sources": "^1.4.1"
3131
},
3232
"devDependencies": {
33+
"@babel/core": "^7.7.2",
3334
"@types/node": "^10.12.21",
3435
"@types/tapable": "^1.0.1",
3536
"@types/webpack-sources": "^0.1.4",
3637
"@yarnpkg/lockfile": "^1.1.0",
38+
"babel-loader": "^8.0.6",
3739
"benchmark": "^2.1.1",
3840
"bundle-loader": "~0.5.0",
3941
"coffee-loader": "^0.9.0",

test/StatsTestCases.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ describe("StatsTestCases", () => {
5959
if (fs.existsSync(path.join(base, testName, "webpack.config.js"))) {
6060
options = require(path.join(base, testName, "webpack.config.js"));
6161
}
62+
let testConfig = {};
63+
try {
64+
// try to load a test file
65+
testConfig = Object.assign(
66+
testConfig,
67+
require(path.join(base, testName, "test.config.js"))
68+
);
69+
} catch (e) {
70+
// ignored
71+
}
72+
6273
(Array.isArray(options) ? options : [options]).forEach(options => {
6374
if (!options.context) options.context = path.join(base, testName);
6475
if (!options.output) options.output = options.output || {};
@@ -152,6 +163,7 @@ describe("StatsTestCases", () => {
152163
.replace(/(\w)\\(\w)/g, "$1/$2")
153164
.replace(/ dependencies:Xms/g, "");
154165
expect(actual).toMatchSnapshot();
166+
if (testConfig.validate) testConfig.validate(stats, stderr.toString());
155167
done();
156168
});
157169
});

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,62 @@ exports[`StatsTestCases should print correct stats for concat-and-sideeffects 1`
692692
ModuleConcatenation bailout: Module is not in any chunk"
693693
`;
694694

695+
exports[`StatsTestCases should print correct stats for context-independence 1`] = `
696+
"Hash: 05b3bf99e7372023e97905b3bf99e7372023e979db9f3b8e4ef7c772a36ddb9f3b8e4ef7c772a36d
697+
Child
698+
Hash: 05b3bf99e7372023e979
699+
Time: Xms
700+
Built at: Thu Jan 01 1970 00:00:00 GMT
701+
Asset Size Chunks Chunk Names
702+
1-4dcf1f7bb7b9da3c54c7.js 424 bytes 1 [emitted] [immutable]
703+
1-4dcf1f7bb7b9da3c54c7.js.map 336 bytes 1 [emitted] [dev]
704+
main-95e076639e5472415472.js 8.32 KiB 0 [emitted] [immutable] main
705+
main-95e076639e5472415472.js.map 8.32 KiB 0 [emitted] [dev] main
706+
Entrypoint main = main-95e076639e5472415472.js main-95e076639e5472415472.js.map
707+
[0] ./a/index.js 40 bytes {0} [built]
708+
[1] ./a/chunk.js + 1 modules 66 bytes {1} [built]
709+
| ./a/chunk.js 47 bytes [built]
710+
| ./a/module.js 19 bytes [built]
711+
Child
712+
Hash: 05b3bf99e7372023e979
713+
Time: Xms
714+
Built at: Thu Jan 01 1970 00:00:00 GMT
715+
Asset Size Chunks Chunk Names
716+
1-4dcf1f7bb7b9da3c54c7.js 424 bytes 1 [emitted] [immutable]
717+
1-4dcf1f7bb7b9da3c54c7.js.map 336 bytes 1 [emitted] [dev]
718+
main-95e076639e5472415472.js 8.32 KiB 0 [emitted] [immutable] main
719+
main-95e076639e5472415472.js.map 8.32 KiB 0 [emitted] [dev] main
720+
Entrypoint main = main-95e076639e5472415472.js main-95e076639e5472415472.js.map
721+
[0] ./b/index.js 40 bytes {0} [built]
722+
[1] ./b/chunk.js + 1 modules 66 bytes {1} [built]
723+
| ./b/chunk.js 47 bytes [built]
724+
| ./b/module.js 19 bytes [built]
725+
Child
726+
Hash: db9f3b8e4ef7c772a36d
727+
Time: Xms
728+
Built at: Thu Jan 01 1970 00:00:00 GMT
729+
Asset Size Chunks Chunk Names
730+
1-4dcf1f7bb7b9da3c54c7.js 940 bytes 1 [emitted] [immutable]
731+
main-95e076639e5472415472.js 8.66 KiB 0 [emitted] [immutable] main
732+
Entrypoint main = main-95e076639e5472415472.js
733+
[0] ./a/index.js 40 bytes {0} [built]
734+
[1] ./a/chunk.js + 1 modules 66 bytes {1} [built]
735+
| ./a/chunk.js 47 bytes [built]
736+
| ./a/module.js 19 bytes [built]
737+
Child
738+
Hash: db9f3b8e4ef7c772a36d
739+
Time: Xms
740+
Built at: Thu Jan 01 1970 00:00:00 GMT
741+
Asset Size Chunks Chunk Names
742+
1-4dcf1f7bb7b9da3c54c7.js 940 bytes 1 [emitted] [immutable]
743+
main-95e076639e5472415472.js 8.66 KiB 0 [emitted] [immutable] main
744+
Entrypoint main = main-95e076639e5472415472.js
745+
[0] ./b/index.js 40 bytes {0} [built]
746+
[1] ./b/chunk.js + 1 modules 66 bytes {1} [built]
747+
| ./b/chunk.js 47 bytes [built]
748+
| ./b/module.js 19 bytes [built]"
749+
`;
750+
695751
exports[`StatsTestCases should print correct stats for define-plugin 1`] = `
696752
"Hash: 97d5f15cb3086ba8eb8878ce8186fd9442bfeb83c3284590614d84a86804
697753
Child
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
it("should run", () => {
2+
require("./loader-source-root!");
3+
require("./loader-source-root-slash!");
4+
require("./loader-source-root-source-slash!");
5+
require("./loader-source-root-2-slash!");
6+
require("./loader-no-source-root!");
7+
require("./loader-pre-relative!");
8+
});
9+
10+
it("should generate the correct SourceMap", function() {
11+
var fs = require("fs");
12+
var source = JSON.parse(fs.readFileSync(__filename + ".map", "utf-8"));
13+
expect(source.sources).toContain("webpack:///./folder/test1.txt");
14+
expect(source.sources).toContain("webpack:///./folder/test2.txt");
15+
expect(source.sources).toContain("webpack:///./folder/test3.txt");
16+
expect(source.sources).toContain("webpack:///./folder/test4.txt");
17+
expect(source.sources).toContain("webpack:///./folder/test5.txt");
18+
expect(source.sources).toContain("webpack:///./folder/test6.txt");
19+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const path = require("path");
2+
module.exports = function() {
3+
this.callback(null, "module.exports = 'ok';", {
4+
version: 3,
5+
file: "/should/be/removed",
6+
sources: [path.join(__dirname, "folder", "test5.txt")],
7+
sourcesContent: ["Test"],
8+
mappings: "AAAA"
9+
});
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = function() {
2+
this.callback(null, "module.exports = 'ok';", {
3+
version: 3,
4+
file: "/should/be/removed",
5+
sources: ["webpack://./folder/test6.txt"],
6+
sourcesContent: ["Test"],
7+
mappings: "AAAA"
8+
});
9+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const path = require("path");
2+
module.exports = function() {
3+
this.callback(null, "module.exports = 'ok';", {
4+
version: 3,
5+
file: "/should/be/removed",
6+
sourceRoot: path.join(__dirname, "folder") + "/",
7+
sources: ["/test4.txt"],
8+
sourcesContent: ["Test"],
9+
mappings: "AAAA"
10+
});
11+
};

0 commit comments

Comments
 (0)