Skip to content

Commit 840b43f

Browse files
committed
SourceMapDevToolPlugin: add fileContext and publicPath options. Add a configCase test
1 parent 2525466 commit 840b43f

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

lib/SourceMapDevToolPlugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,17 @@ class SourceMapDevToolPlugin {
181181
}
182182
let sourceMapFile = compilation.getPath(sourceMapFilename, {
183183
chunk,
184-
filename,
184+
filename: options.fileContext ? path.relative(options.fileContext, filename) : filename,
185185
query,
186186
basename: basename(filename)
187187
});
188188
if(sourceMapFile.indexOf("[contenthash]") !== -1) {
189189
sourceMapFile = sourceMapFile.replace(/\[contenthash\]/g, crypto.createHash("md5").update(sourceMapString).digest("hex"));
190190
}
191-
const sourceMapUrl = path.relative(path.dirname(file), sourceMapFile).replace(/\\/g, "/");
191+
let sourceMapUrl = (options.fileContext ? sourceMapFile : path.relative(path.dirname(file), sourceMapFile)).replace(/\\/g, "/");
192+
if(options.publicPath) {
193+
sourceMapUrl = options.publicPath + sourceMapUrl;
194+
}
192195
if(currentSourceMappingURLComment !== false) {
193196
asset.__SourceMapDevToolData[file] = compilation.assets[file] = new ConcatSource(new RawSource(source), currentSourceMappingURLComment.replace(/\[url\]/g, sourceMapUrl));
194197
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
it("should contain publicPath prefix in [url] and resolve relatively to fileContext", function() {
2+
var fs = require("fs"),
3+
path = require("path");
4+
var source = fs.readFileSync(path.join(__dirname, "dist/public/test.js"), "utf-8");
5+
source.should.containEql("//# sourceMappingURL=https://10.10.10.10/project/sourcemaps/test.js.map");
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var testObject = {
2+
a: 1
3+
};
4+
5+
module.exports = testObject;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var webpack = require("../../../../");
2+
module.exports = {
3+
node: {
4+
__dirname: false,
5+
__filename: false
6+
},
7+
entry: {
8+
"bundle0": ["./index.js"],
9+
"dist/public/test": ["./test.js"],
10+
},
11+
output: {
12+
filename: "[name].js"
13+
},
14+
plugins: [
15+
new webpack.SourceMapDevToolPlugin({
16+
filename: "sourcemaps/[file].map",
17+
publicPath: "https://10.10.10.10/project/",
18+
fileContext: "dist/public"
19+
})
20+
]
21+
};

0 commit comments

Comments
 (0)