Skip to content

Commit 24773c8

Browse files
In aspnet-webpack, provide a SourceMapDevToolPlugin wrapper that strips out "charset=utf-8;" from inline source map URLs to enable VS debugger compatibility
1 parent 6c64587 commit 24773c8

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as webpack from 'webpack';
2+
3+
const searchValue = /^\n*\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,/;
4+
const replaceValue = '\n//# sourceMappingURL=data:application/json;base64,';
5+
6+
/**
7+
* Wraps Webpack's built-in SourceMapDevToolPlugin with a post-processing step that makes inline source maps
8+
* work with Visual Studio's native debugger.
9+
*
10+
* The issue this fixes is that VS doesn't like to see 'charset=utf-8;' in the 'sourceMappingURL'. If that string
11+
* is present, VS will ignore the source map entirely. Until that VS bug is fixed, we can just strip out the charset
12+
* specifier from the URL. It's not needed because tools assume it's utf-8 anyway.
13+
*/
14+
export class SourceMapDevToolPlugin {
15+
/**
16+
* Constructs an instance of SourceMapDevToolPlugin.
17+
*
18+
* @param options Options that will be passed through to Webpack's native SourceMapDevToolPlugin.
19+
*/
20+
constructor(private options?: any) {}
21+
22+
protected apply(compiler: any) {
23+
// First, attach Webpack's native SourceMapDevToolPlugin, passing through the options
24+
const underlyingPlugin: any = new webpack.SourceMapDevToolPlugin(this.options);
25+
underlyingPlugin.apply(compiler);
26+
27+
// Hook into the compilation right after the native SourceMapDevToolPlugin does
28+
compiler.plugin('compilation', compilation => {
29+
compilation.plugin('after-optimize-chunk-assets', chunks => {
30+
// Look for any compiled assets that might be an inline 'sourceMappingURL' source segment
31+
if (compilation.assets) {
32+
Object.getOwnPropertyNames(compilation.assets).forEach(assetName => {
33+
const asset = compilation.assets[assetName];
34+
if (asset && asset.children instanceof Array) {
35+
for (let index = 0; index < asset.children.length; index++) {
36+
const assetChild = asset.children[index];
37+
if (typeof assetChild === 'string') {
38+
// This asset is a source segment, so if it matches our regex, update it
39+
asset.children[index] = assetChild.replace(searchValue, replaceValue);
40+
}
41+
}
42+
}
43+
});
44+
}
45+
});
46+
});
47+
}
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { createWebpackDevServer } from './WebpackDevMiddleware';
22
export { loadViaWebpack } from './LoadViaWebpack';
3+
export { SourceMapDevToolPlugin } from './SourceMapDevToolPlugin';

0 commit comments

Comments
 (0)