Skip to content

Speed up sourcemap lookups by checking existence of .map files #1780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/common/sourceMaps/sourceMapRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import { Dirent } from 'fs';
import { xxHash32 } from 'js-xxhash';
import { basename } from 'path';
import { FileGlobList } from '../fileGlobList';
import { readfile, stat } from '../fsUtils';
import { parseSourceMappingUrl } from '../sourceUtils';
Expand Down Expand Up @@ -67,13 +69,23 @@ export interface ISearchStrategy {
*/
export const createMetadataForFile = async (
compiledPath: string,
siblings: Dirent[],
fileContents?: string,
): Promise<Required<ISourceMapMetadata> | undefined> => {
if (typeof fileContents === 'undefined') {
fileContents = await readfile(compiledPath);
let sourceMapUrl;
const compiledFileName = basename(compiledPath);
for (const sibling of siblings) {
if (sibling.isFile() && sibling.name === `${compiledFileName}.map`) {
sourceMapUrl = sibling.name;
break;
}
}
if (!sourceMapUrl) {
if (typeof fileContents === 'undefined') {
fileContents = await readfile(compiledPath);
}
sourceMapUrl = parseSourceMappingUrl(fileContents);
}

let sourceMapUrl = parseSourceMappingUrl(fileContents);
if (!sourceMapUrl) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/common/sourceMaps/turboGlobStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface ITurboGlobStreamOptions<E> {
/** Cache state, will be updated. */
cache: CacheTree<IGlobCached<E>>;
/** File to transform a path into extracted data emitted on onFile */
fileProcessor: (path: string) => Promise<E>;
fileProcessor: (path: string, siblings: Dirent[]) => Promise<E>;
}

const forwardSlashRe = /\//g;
Expand All @@ -60,7 +60,7 @@ export class TurboGlobStream<E> {

private readonly filter?: (path: string, previousData?: E) => boolean;
private readonly ignore: ((path: string) => boolean)[];
private readonly processor: (path: string) => Promise<E>;
private readonly processor: (path: string, siblings: Dirent[]) => Promise<E>;
private readonly fileEmitter = new EventEmitter<E>();
public readonly onFile = this.fileEmitter.event;
private readonly errorEmitter = new EventEmitter<{ path: string; error: Error }>();
Expand Down Expand Up @@ -299,7 +299,9 @@ export class TurboGlobStream<E> {
const platformPath = sep === '/' ? path : path.replace(forwardSlashRe, sep);
let extracted: E;
try {
extracted = await this.processor(platformPath);
const dirPath = platformPath.substring(0, platformPath.lastIndexOf(sep));
const siblings = await this.readdir(dirPath);
extracted = await this.processor(platformPath, siblings);
} catch (error) {
this.errorEmitter.fire({ path: platformPath, error });
return;
Expand Down
5 changes: 3 additions & 2 deletions src/common/sourceMaps/turboSearchStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { truthy } from '../objUtils';
import { fixDriveLetterAndSlashes } from '../pathUtils';
import { CacheTree } from './cacheTree';
import {
createMetadataForFile,
ISearchStrategy,
ISourcemapStreamOptions,
createMetadataForFile,
} from './sourceMapRepository';
import { IGlobCached, TurboGlobStream } from './turboGlobStream';

Expand Down Expand Up @@ -82,7 +82,8 @@ export class TurboSearchStrategy implements ISearchStrategy {
cwd: glob.cwd,
cache,
filter: opts.filter,
fileProcessor: file => createMetadataForFile(file).then(m => m && opts.processMap(m)),
fileProcessor: (file, siblings) =>
createMetadataForFile(file, siblings).then(m => m && opts.processMap(m)),
});

tgs.onError(({ path, error }) => {
Expand Down