Skip to content

Commit

Permalink
feat(utils): skip ngcc processing in ng16 or higher (#2063)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai authored May 5, 2023
1 parent 56f96a8 commit 2661988
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/utils/ngcc-jest-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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`);
}
}
};

0 comments on commit 2661988

Please sign in to comment.