| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * The implementation of this plugin is based on similar webpack plugin in Angular CLI |
| 10 | * https://github.com/angular/angular-cli/blob/16d8c552e99bfe65ded9e843e917dbb95eb8ec01/packages/angular_devkit/build_angular/src/tools/webpack/plugins/devtools-ignore-plugin.ts |
| 11 | * and devtools-ignore-webpack-plugin |
| 12 | * https://github.com/mondaychen/devtools-ignore-webpack-plugin/blob/d15274e4d2fdb74f73aa644f14773a5523823999/src/index.ts |
| 13 | * which both are licensed under MIT |
| 14 | */ |
| 15 | |
| 16 | const {Compilation} = require('webpack'); |
| 17 | |
| 18 | const IGNORE_LIST = 'ignoreList'; |
| 19 | const PLUGIN_NAME = 'source-map-ignore-list-plugin'; |
| 20 | |
| 21 | class SourceMapIgnoreListPlugin { |
| 22 | constructor({shouldIgnoreSource}) { |
| 23 | this.shouldIgnoreSource = shouldIgnoreSource; |
| 24 | } |
| 25 | |
| 26 | apply(compiler) { |
| 27 | const {RawSource} = compiler.webpack.sources; |
| 28 | |
| 29 | compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => { |
| 30 | compilation.hooks.processAssets.tap( |
| 31 | { |
| 32 | name: PLUGIN_NAME, |
| 33 | stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING, |
| 34 | additionalAssets: true, |
| 35 | }, |
| 36 | assets => { |
| 37 | // eslint-disable-next-line no-for-of-loops/no-for-of-loops |
| 38 | for (const [name, asset] of Object.entries(assets)) { |
| 39 | if (!name.endsWith('.map')) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | const mapContent = asset.source().toString(); |
| 44 | if (!mapContent) { |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | const map = JSON.parse(mapContent); |
| 49 | const ignoreList = []; |
| 50 | |
| 51 | const sourcesCount = map.sources.length; |
| 52 | for ( |
| 53 | let potentialSourceIndex = 0; |
| 54 | potentialSourceIndex < sourcesCount; |
| 55 | ++potentialSourceIndex |
| 56 | ) { |
| 57 | const source = map.sources[potentialSourceIndex]; |
| 58 | |
| 59 | if (this.shouldIgnoreSource(name, source)) { |
| 60 | ignoreList.push(potentialSourceIndex); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | map[IGNORE_LIST] = ignoreList; |
| 65 | compilation.updateAsset(name, new RawSource(JSON.stringify(map))); |
| 66 | } |
| 67 | }, |
| 68 | ); |
| 69 | }); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | module.exports = SourceMapIgnoreListPlugin; |