Skip to content
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

perf(transformer): cache useNativeEsbuild result #1309

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Changes from all 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
22 changes: 15 additions & 7 deletions src/ng-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import type { ProjectConfigTsJest, TransformOptionsTsJest } from 'ts-jest/dist/t
import { NgJestCompiler } from './compiler/ng-jest-compiler';
import { NgJestConfig } from './config/ng-jest-config';

// Cache the result between multiple transformer instances
// to avoid spawning multiple processes (which can have a major
// performance impact when used with multiple projects).
let useNativeEsbuild: boolean;

export class NgJestTransformer extends TsJestTransformer {
#ngJestLogger: Logger;
#esbuildImpl: typeof import('esbuild');
Expand All @@ -26,14 +31,17 @@ export class NgJestTransformer extends TsJestTransformer {
},
targets: process.env.NG_JEST_LOG ?? undefined,
});
let useNativeEsbuild = false;
try {
const esbuildCheckPath = require.resolve('@angular-devkit/build-angular/esbuild-check.js');
const { status, error } = spawnSync(process.execPath, [esbuildCheckPath]);
useNativeEsbuild = status === 0 && error === undefined;
} catch (e) {
useNativeEsbuild = false;

if (useNativeEsbuild === undefined) {
try {
const esbuildCheckPath = require.resolve('@angular-devkit/build-angular/esbuild-check.js');
const { status, error } = spawnSync(process.execPath, [esbuildCheckPath]);
useNativeEsbuild = status === 0 && error === undefined;
} catch (e) {
useNativeEsbuild = false;
}
}

this.#esbuildImpl = useNativeEsbuild ? require('esbuild') : require('esbuild-wasm');
}

Expand Down