-
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/cli): 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>
- Loading branch information
1 parent
3d6ed0c
commit 5632024
Showing
3 changed files
with
181 additions
and
2 deletions.
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
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
/** | ||
* @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_devtoolsIgnore'; | ||
|
||
const PLUGIN_NAME = 'devtools-ignore-plugin'; | ||
|
||
interface Options { | ||
vendors?: false | { test: RegExp }; | ||
runtime?: false | { test: RegExp }; | ||
} | ||
/** | ||
* 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 { | ||
options: Options; | ||
|
||
constructor(options: Options) { | ||
this.options = options; | ||
} | ||
|
||
apply(compiler: Compiler) { | ||
const { SourceMapSource } = 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) => { | ||
if (!this.options.vendors || !this.options.runtime) { | ||
return; | ||
} | ||
const vendorsTest = this.options.vendors.test; | ||
const runtimeTest = this.options.runtime.test; | ||
|
||
for (const [name, asset] of Object.entries(assets)) { | ||
const source = asset.source(); | ||
const map = asset.map() as Object & { | ||
sources: string[]; | ||
[IGNORE_LIST]: number[]; | ||
}; | ||
if (!map) { | ||
continue; | ||
} | ||
|
||
map[IGNORE_LIST] = Object.entries(map.sources) | ||
.filter(([, source]) => source.match(vendorsTest) || source.match(runtimeTest)) | ||
.map(([index]) => +index); | ||
|
||
compilation.assets[name] = new SourceMapSource(source, name, map); | ||
} | ||
}, | ||
); | ||
}); | ||
} | ||
} |