Skip to content

Commit

Permalink
perf(transformer): cache checking esbuild result (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorin-davidoi authored Feb 11, 2022
1 parent b56ebd5 commit 3412142
Showing 1 changed file with 15 additions and 7 deletions.
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

0 comments on commit 3412142

Please sign in to comment.