Skip to content

Commit

Permalink
perf(@angular-devkit/build-angular): minor sourcemap ignorelist impro…
Browse files Browse the repository at this point in the history
…vements for esbuild builder

This provides a minor performance benefit for the generation of the Chrome sourcemap ignorelist
generation. Memory is shared were possible and string searching is reduced in certain cases.

(cherry picked from commit ac95732)
  • Loading branch information
clydin authored and alan-agius4 committed May 15, 2023
1 parent 117e8d0 commit e5c1d43
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import type { Plugin } from 'esbuild';
*/
const IGNORE_LIST_ID = 'x_google_ignoreList';

/**
* The UTF-8 bytes for the node modules check text used to avoid unnecessary parsing
* of a full source map if not present in the source map data.
*/
const NODE_MODULE_BYTES = Buffer.from('node_modules/', 'utf-8');

/**
* Minimal sourcemap object required to create the ignore list.
*/
Expand Down Expand Up @@ -50,10 +56,15 @@ export function createSourcemapIngorelistPlugin(): Plugin {
continue;
}

const contents = Buffer.from(file.contents);
// Create a Buffer object that shares the memory of the output file contents
const contents = Buffer.from(
file.contents.buffer,
file.contents.byteOffset,
file.contents.byteLength,
);

// Avoid parsing sourcemaps that have no node modules references
if (!contents.includes('node_modules/')) {
if (!contents.includes(NODE_MODULE_BYTES)) {
continue;
}

Expand All @@ -62,10 +73,8 @@ export function createSourcemapIngorelistPlugin(): Plugin {

// 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/')
) {
const location = map.sources[index].indexOf('node_modules/');
if (location === 0 || (location > 0 && map.sources[index][location - 1] === '/')) {
ignoreList.push(index);
}
}
Expand Down

0 comments on commit e5c1d43

Please sign in to comment.