|
| 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 | +} |
0 commit comments