-
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.
fix(@angular-devkit/build-angular): add sourcemap `x_google_ignoreLis…
…t` support for esbuild builder When using the esbuild-based browser application builder with source maps enabled, the Chrome DevTools `x_google_ignoreList` extension will be added to JavaScript source maps. This extension supports an improved developer experience with Chrome DevTools. For more information, please see https://developer.chrome.com/articles/x-google-ignore-list/ (cherry picked from commit b0d04f7)
- Loading branch information
1 parent
a5cb461
commit 766c146
Showing
4 changed files
with
102 additions
and
0 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
85 changes: 85 additions & 0 deletions
85
.../angular_devkit/build_angular/src/builders/browser-esbuild/sourcemap-ignorelist-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,85 @@ | ||
/** | ||
* @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 type { Plugin } from 'esbuild'; | ||
|
||
/** | ||
* The field identifier for the sourcemap Chrome Devtools ignore list extension. | ||
* | ||
* Following the naming conventions from https://sourcemaps.info/spec.html#h.ghqpj1ytqjbm | ||
*/ | ||
const IGNORE_LIST_ID = 'x_google_ignoreList'; | ||
|
||
/** | ||
* Minimal sourcemap object required to create the ignore list. | ||
*/ | ||
interface SourceMap { | ||
sources: string[]; | ||
[IGNORE_LIST_ID]?: number[]; | ||
} | ||
|
||
/** | ||
* Creates an esbuild plugin that updates generated sourcemaps to include the Chrome | ||
* DevTools ignore list extension. All source files that originate from a node modules | ||
* directory are added to the ignore list by this plugin. | ||
* | ||
* For more information, see https://developer.chrome.com/articles/x-google-ignore-list/ | ||
* @returns An esbuild plugin. | ||
*/ | ||
export function createSourcemapIngorelistPlugin(): Plugin { | ||
return { | ||
name: 'angular-sourcemap-ignorelist', | ||
setup(build): void { | ||
if (!build.initialOptions.sourcemap) { | ||
return; | ||
} | ||
|
||
build.onEnd((result) => { | ||
if (!result.outputFiles) { | ||
return; | ||
} | ||
|
||
for (const file of result.outputFiles) { | ||
// Only process sourcemap files | ||
if (!file.path.endsWith('.map')) { | ||
continue; | ||
} | ||
|
||
const contents = Buffer.from(file.contents); | ||
|
||
// Avoid parsing sourcemaps that have no node modules references | ||
if (!contents.includes('node_modules/')) { | ||
continue; | ||
} | ||
|
||
const map = JSON.parse(contents.toString('utf-8')) as SourceMap; | ||
const ignoreList = []; | ||
|
||
// Check and store the index of each source originating from a node modules directory | ||
for (let index = 0; index < map.sources.length; ++index) { | ||
if ( | ||
map.sources[index].startsWith('node_modules/') || | ||
map.sources[index].includes('/node_modules/') | ||
) { | ||
ignoreList.push(index); | ||
} | ||
} | ||
|
||
// Avoid regenerating the source map if nothing changed | ||
if (ignoreList.length === 0) { | ||
continue; | ||
} | ||
|
||
// Update the sourcemap in the output file | ||
map[IGNORE_LIST_ID] = ignoreList; | ||
file.contents = Buffer.from(JSON.stringify(map), 'utf-8'); | ||
} | ||
}); | ||
}, | ||
}; | ||
} |
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