From 266198863242804e0cd79c83f151247194b37b06 Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Fri, 5 May 2023 23:51:14 +0200 Subject: [PATCH] feat(utils): skip ngcc processing in ng16 or higher (#2063) --- src/utils/ngcc-jest-processor.ts | 38 ++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/utils/ngcc-jest-processor.ts b/src/utils/ngcc-jest-processor.ts index f0645e009f..31f3f20350 100644 --- a/src/utils/ngcc-jest-processor.ts +++ b/src/utils/ngcc-jest-processor.ts @@ -11,13 +11,29 @@ let ngccPath = ''; try { ngccPath = require.resolve('@angular/compiler-cli/ngcc/main-ngcc.js'); } catch { - const compilerCliNgccPath = require.resolve('@angular/compiler-cli/ngcc'); - ngccPath = path.resolve(compilerCliNgccPath.substring(0, compilerCliNgccPath.lastIndexOf(path.sep)), 'main-ngcc.js'); + try { + const compilerCliNgccPath = require.resolve('@angular/compiler-cli/ngcc'); + const compilerCliNgccFolder = compilerCliNgccPath.substring(0, compilerCliNgccPath.lastIndexOf(path.sep)); + ngccPath = path.resolve(compilerCliNgccFolder, 'main-ngcc.js'); + } catch { + // No ngcc in NG16 + } } function findNodeModulesDirectory(): string { return ngccPath.substring(0, ngccPath.indexOf(ANGULAR_COMPILER_CLI_PKG_NAME)); } +function findAngularCompilerCliVersion(): string { + const path = require.resolve(ANGULAR_COMPILER_CLI_PKG_NAME); + const substringLength = path.indexOf(ANGULAR_COMPILER_CLI_PKG_NAME) + ANGULAR_COMPILER_CLI_PKG_NAME.length; + const ngCompilerCliFolder = path.substring(0, substringLength); + const ngCompilerCliPackageJson = `${ngCompilerCliFolder}/package.json`; + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { version } = require(ngCompilerCliPackageJson); + + return version; +} + const nodeModuleDirPath = findNodeModulesDirectory(); export const runNgccJestProcessor = (tsconfigPath: string | undefined): void => { @@ -55,10 +71,18 @@ export const runNgccJestProcessor = (tsconfigPath: string | undefined): void => throw new Error(`${errorMessage} NGCC failed ${errorMessage ? ', see above' : ''}.`); } } else { - console.log( - `Warning: Could not locate '@angular/compiler-cli' to run 'ngcc' automatically.` + - `Please make sure you are running 'ngcc-jest-processor.js' from root level of your project.` + - `'ngcc' must be run before running Jest`, - ); + const ngCompilerCliVersion = findAngularCompilerCliVersion(); + const [ngMajorVersion] = ngCompilerCliVersion.split('.'); + + if (parseInt(ngMajorVersion, 10) < 16) { + console.log( + `Warning: Could not locate '@angular/compiler-cli' to run 'ngcc' automatically.` + + `Please make sure you are running 'ngcc-jest-processor.js' from root level of your project.` + + `'ngcc' must be run before running Jest`, + ); + } else { + console.log(`@angular/compiler-cli@${ngCompilerCliVersion} detected. Skipping 'ngcc'`); + console.log(`Tip: To avoid this message you can remove 'jest-preset-angular/global-setup' from your jest config`); + } } };