Skip to content

Commit

Permalink
chore: install source map support lazily (#26445)
Browse files Browse the repository at this point in the history
This way we allow importing from `@playwright/test` without affecting
stack traces.

Fixes #26346.
  • Loading branch information
dgozman committed Aug 21, 2023
1 parent bcc30bc commit 32a309c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
42 changes: 25 additions & 17 deletions packages/playwright-test/src/transform/compilationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,31 @@ const fileDependencies = new Map<string, Set<string>>();
// Dependencies resolved by the external bundler.
const externalDependencies = new Map<string, Set<string>>();

Error.stackTraceLimit = 200;

sourceMapSupport.install({
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap(source) {
if (!sourceMaps.has(source))
return null;
const sourceMapPath = sourceMaps.get(source)!;
if (!fs.existsSync(sourceMapPath))
return null;
return {
map: JSON.parse(fs.readFileSync(sourceMapPath, 'utf-8')),
url: source
};
}
});
let sourceMapSupportInstalled = false;

export function installSourceMapSupportIfNeeded() {
if (sourceMapSupportInstalled)
return;
sourceMapSupportInstalled = true;

Error.stackTraceLimit = 200;

sourceMapSupport.install({
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap(source) {
if (!sourceMaps.has(source))
return null;
const sourceMapPath = sourceMaps.get(source)!;
if (!fs.existsSync(sourceMapPath))
return null;
return {
map: JSON.parse(fs.readFileSync(sourceMapPath, 'utf-8')),
url: source
};
}
});
}

function _innerAddToCompilationCache(filename: string, options: { codePath: string, sourceMapPath: string, moduleUrl?: string }) {
sourceMaps.set(options.moduleUrl || filename, options.sourceMapPath);
Expand Down
4 changes: 3 additions & 1 deletion packages/playwright-test/src/transform/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Module from 'module';
import type { BabelPlugin, BabelTransformFunction } from './babelBundle';
import { createFileMatcher, fileIsModule, resolveImportSpecifierExtension } from '../util';
import type { Matcher } from '../util';
import { getFromCompilationCache, currentFileDepsCollector, belongsToNodeModules } from './compilationCache';
import { getFromCompilationCache, currentFileDepsCollector, belongsToNodeModules, installSourceMapSupportIfNeeded } from './compilationCache';

const version = require('../../package.json').version;

Expand Down Expand Up @@ -213,6 +213,8 @@ export async function requireOrImport(file: string) {
}

function installTransform(): () => void {
installSourceMapSupportIfNeeded();

let reverted = false;

const originalResolveFilename = (Module as any)._resolveFilename;
Expand Down

0 comments on commit 32a309c

Please sign in to comment.