Skip to content

Instantly share code, notes, and snippets.

@ZKHelloworld
Created June 10, 2021 03:40
Show Gist options
  • Save ZKHelloworld/e7ff7bf46b6cfa22816f650417de103d to your computer and use it in GitHub Desktop.
Save ZKHelloworld/e7ff7bf46b6cfa22816f650417de103d to your computer and use it in GitHub Desktop.
dev-sourcemap-webpack-plugin
const path = require('path');
const { RawSource } = require('webpack').sources;
const PLUGIN_NAME = 'DevSourcemapWebpackPlugin';
class DevSourcemapWebpackPlugin {
constructor(options) {
this.test = options.test || new RegExp(`${PLUGIN_NAME}__no_source_map_reg__`);
}
apply(compiler) {
compiler.hooks.emit.tapAsync(PLUGIN_NAME, (compilation, callback) => {
const { assets } = compilation;
// traverse all the sourcemap file
for (var p in assets) {
// config in designer.prod.conf.js output.sourceMapFilename
if (!this.test.test(p)) {
continue;
}
// dev sourcemap
assets[`..${path.sep}${p}`] = assets[p];
// prod sourcemap
const sourcemap = JSON.parse(assets[p]._value);
delete sourcemap.sourcesContent;
assets[p] = new RawSource(JSON.stringify(sourcemap));
}
callback();
});
}
}
module.exports = DevSourcemapWebpackPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment