-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/build-angular): Identify third-party sources in …
…sourcemaps This PR includes a webpack plugin which adds a field to source maps that identifies which sources are vendored or runtime-injected (aka third-party) sources. These will be consumed by Chrome DevTools to automatically ignore-list sources. When vendor source map processing is enabled, this is interpreted as the developer intending to debug third-party code; in this case, the feature is disabled. Signed-off-by: Victor Porof <victorporof@chromium.org> (cherry picked from commit f3087dc)
- Loading branch information
1 parent
163618e
commit b5f6d86
Showing
3 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
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
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
69 changes: 69 additions & 0 deletions
69
packages/angular_devkit/build_angular/src/webpack/plugins/devtools-ignore-plugin.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { Compilation, Compiler } from 'webpack'; | ||
|
||
// Following the naming conventions from | ||
// https://sourcemaps.info/spec.html#h.ghqpj1ytqjbm | ||
const IGNORE_LIST = 'x_google_ignoreList'; | ||
|
||
const PLUGIN_NAME = 'devtools-ignore-plugin'; | ||
|
||
interface SourceMap { | ||
sources: string[]; | ||
[IGNORE_LIST]: number[]; | ||
} | ||
|
||
/** | ||
* This plugin adds a field to source maps that identifies which sources are | ||
* vendored or runtime-injected (aka third-party) sources. These are consumed by | ||
* Chrome DevTools to automatically ignore-list sources. | ||
*/ | ||
export class DevToolsIgnorePlugin { | ||
apply(compiler: 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)) { | ||
// Instead of using `asset.map()` to fetch the source maps from | ||
// SourceMapSource assets, process them directly as a RawSource. | ||
// This is because `.map()` is slow and can take several seconds. | ||
if (!name.endsWith('.map')) { | ||
// Ignore non source map files. | ||
continue; | ||
} | ||
|
||
const mapContent = asset.source().toString(); | ||
if (!mapContent) { | ||
continue; | ||
} | ||
|
||
const map = JSON.parse(mapContent) as SourceMap; | ||
const ignoreList = []; | ||
|
||
for (const [index, path] of map.sources.entries()) { | ||
if (path.includes('/node_modules/') || path.startsWith('webpack/')) { | ||
ignoreList.push(index); | ||
} | ||
} | ||
|
||
map[IGNORE_LIST] = ignoreList; | ||
compilation.updateAsset(name, new RawSource(JSON.stringify(map))); | ||
} | ||
}, | ||
); | ||
}); | ||
} | ||
} |