Skip to content

Commit 14189b2

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): remove direct require usage from Angular version check
To support future package building as ESM, the direct usage of require has now been removed from the Angular compatibility check code used by various builders. Additionally, the limited use of `@angular-devkit/core` has also been removed from the check code as it was only used to adjust string values for error messages which can be done directly instead.
1 parent 7a1a443 commit 14189b2

File tree

1 file changed

+20
-22
lines changed
  • packages/angular_devkit/build_angular/src/utils

1 file changed

+20
-22
lines changed

packages/angular_devkit/build_angular/src/utils/version.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,39 @@
88

99
/* eslint-disable no-console */
1010

11-
import { tags } from '@angular-devkit/core';
11+
import { createRequire } from 'node:module';
1212
import { SemVer, satisfies } from 'semver';
1313

1414
export function assertCompatibleAngularVersion(projectRoot: string): void | never {
1515
let angularCliPkgJson;
1616
let angularPkgJson;
17-
const resolveOptions = { paths: [projectRoot] };
17+
18+
// Create a custom require function for ESM compliance.
19+
// NOTE: The trailing slash is significant.
20+
const projectRequire = createRequire(projectRoot + '/');
1821

1922
try {
20-
const angularPackagePath = require.resolve('@angular/core/package.json', resolveOptions);
23+
const angularPackagePath = projectRequire.resolve('@angular/core/package.json');
2124

22-
angularPkgJson = require(angularPackagePath);
25+
angularPkgJson = projectRequire(angularPackagePath);
2326
} catch {
24-
console.error(tags.stripIndents`
25-
You seem to not be depending on "@angular/core". This is an error.
26-
`);
27+
console.error('You seem to not be depending on "@angular/core". This is an error.');
2728

2829
process.exit(2);
2930
}
3031

3132
if (!(angularPkgJson && angularPkgJson['version'])) {
32-
console.error(tags.stripIndents`
33-
Cannot determine versions of "@angular/core".
34-
This likely means your local installation is broken. Please reinstall your packages.
35-
`);
33+
console.error(
34+
'Cannot determine versions of "@angular/core".\n' +
35+
'This likely means your local installation is broken. Please reinstall your packages.',
36+
);
3637

3738
process.exit(2);
3839
}
3940

4041
try {
41-
const angularCliPkgPath = require.resolve('@angular/cli/package.json', resolveOptions);
42-
angularCliPkgJson = require(angularCliPkgPath);
42+
const angularCliPkgPath = projectRequire.resolve('@angular/cli/package.json');
43+
angularCliPkgJson = projectRequire(angularCliPkgPath);
4344
if (!(angularCliPkgJson && angularCliPkgJson['version'])) {
4445
return;
4546
}
@@ -55,19 +56,16 @@ export function assertCompatibleAngularVersion(projectRoot: string): void | neve
5556
return;
5657
}
5758

58-
const supportedAngularSemver =
59-
require('../../package.json')['peerDependencies']['@angular/compiler-cli'];
59+
const supportedAngularSemver = projectRequire('@angular-devkit/build-angular/package.json')[
60+
'peerDependencies'
61+
]['@angular/compiler-cli'];
6062
const angularVersion = new SemVer(angularPkgJson['version']);
6163

6264
if (!satisfies(angularVersion, supportedAngularSemver, { includePrerelease: true })) {
6365
console.error(
64-
tags.stripIndents`
65-
This version of CLI is only compatible with Angular versions ${supportedAngularSemver},
66-
but Angular version ${angularVersion} was found instead.
67-
68-
Please visit the link below to find instructions on how to update Angular.
69-
https://update.angular.io/
70-
` + '\n',
66+
`This version of CLI is only compatible with Angular versions ${supportedAngularSemver},\n` +
67+
`but Angular version ${angularVersion} was found instead.\n` +
68+
'Please visit the link below to find instructions on how to update Angular.\nhttps://update.angular.io/',
7169
);
7270

7371
process.exit(3);

0 commit comments

Comments
 (0)