|
|
|
|
|
|
|
|
|
|
|
const webpack = require('@rspack/core') |
|
|
|
|
|
|
|
|
|
|
|
const IGNORE_LIST = 'ignoreList' |
|
|
const PLUGIN_NAME = 'devtools-ignore-plugin' |
|
|
function defaultShouldIgnorePath(path) { |
|
|
return path.includes('/node_modules/') || path.includes('/webpack/') |
|
|
} |
|
|
function defaultIsSourceMapAsset(name) { |
|
|
return name.endsWith('.map') |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = class DevToolsIgnorePlugin { |
|
|
options |
|
|
constructor(options = {}) { |
|
|
this.options = { |
|
|
shouldIgnorePath: options.shouldIgnorePath ?? defaultShouldIgnorePath, |
|
|
isSourceMapAsset: options.isSourceMapAsset ?? defaultIsSourceMapAsset, |
|
|
} |
|
|
} |
|
|
apply(compiler) { |
|
|
const { RawSource } = compiler.webpack.sources |
|
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
|
|
compilation.hooks.processAssets.tap( |
|
|
{ |
|
|
name: PLUGIN_NAME, |
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING, |
|
|
additionalAssets: true, |
|
|
}, |
|
|
(assets) => { |
|
|
for (const [name, asset] of Object.entries(assets)) { |
|
|
|
|
|
|
|
|
|
|
|
if (!this.options.isSourceMapAsset(name)) { |
|
|
|
|
|
continue |
|
|
} |
|
|
const mapContent = asset.source().toString() |
|
|
if (!mapContent) { |
|
|
continue |
|
|
} |
|
|
const sourcemap = JSON.parse(mapContent) |
|
|
const ignoreList = [] |
|
|
for (const [index, path] of sourcemap.sources.entries()) { |
|
|
if (this.options.shouldIgnorePath(path)) { |
|
|
ignoreList.push(index) |
|
|
} |
|
|
} |
|
|
sourcemap[IGNORE_LIST] = ignoreList |
|
|
compilation.updateAsset( |
|
|
name, |
|
|
new RawSource(JSON.stringify(sourcemap)) |
|
|
) |
|
|
} |
|
|
} |
|
|
) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|