const {Compilation} = require('webpack');
const IGNORE_LIST = 'ignoreList';
const PLUGIN_NAME = 'source-map-ignore-list-plugin';
class SourceMapIgnoreListPlugin {
constructor({shouldIgnoreSource}) {
this.shouldIgnoreSource = shouldIgnoreSource;
}
apply(compiler) {
const {RawSource} = compiler.webpack.sources;
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
additionalAssets: true,
},
assets => {
for (const [name, asset] of Object.entries(assets)) {
if (!name.endsWith('.map')) {
continue;
}
const mapContent = asset.source().toString();
if (!mapContent) {
continue;
}
const map = JSON.parse(mapContent);
const ignoreList = [];
const sourcesCount = map.sources.length;
for (
let potentialSourceIndex = 0;
potentialSourceIndex < sourcesCount;
++potentialSourceIndex
) {
const source = map.sources[potentialSourceIndex];
if (this.shouldIgnoreSource(name, source)) {
ignoreList.push(potentialSourceIndex);
}
}
map[IGNORE_LIST] = ignoreList;
compilation.updateAsset(name, new RawSource(JSON.stringify(map)));
}
},
);
});
}
}
module.exports = SourceMapIgnoreListPlugin;