Created
June 10, 2021 03:40
-
-
Save ZKHelloworld/e7ff7bf46b6cfa22816f650417de103d to your computer and use it in GitHub Desktop.
dev-sourcemap-webpack-plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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