Skip to content

Commit 8f3695e

Browse files
gkalpakmhevery
authored andcommitted
refactor(ngcc): add packageName property to EntryPoint interface (angular#37040)
This commit adds a `packageName` property to the `EntryPoint` interface. In a subsequent commit this will be used to retrieve the correct ngcc configuration for each package, regardless of its path. PR Close angular#37040
1 parent 829ddf9 commit 8f3695e

File tree

7 files changed

+222
-11
lines changed

7 files changed

+222
-11
lines changed

packages/compiler-cli/ngcc/src/packages/entry_point.ts

+45-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export interface EntryPoint extends JsonObject {
2828
name: string;
2929
/** The path to this entry point. */
3030
path: AbsoluteFsPath;
31+
/**
32+
* The name of the package that contains this entry-point (e.g. `@angular/core` or
33+
* `@angular/common`).
34+
*/
35+
packageName: string;
3136
/** The path to the package that contains this entry-point. */
3237
packagePath: AbsoluteFsPath;
3338
/** The parsed package.json file for this entry-point. */
@@ -131,7 +136,8 @@ export function getEntryPointInfo(
131136
const loadedEntryPointPackageJson = (packagePackageJsonPath === entryPointPackageJsonPath) ?
132137
loadedPackagePackageJson :
133138
loadPackageJson(fs, entryPointPackageJsonPath);
134-
const packageVersion = getPackageVersion(loadedPackagePackageJson);
139+
const {packageName, packageVersion} = getPackageNameAndVersion(
140+
fs, packagePath, loadedPackagePackageJson, loadedEntryPointPackageJson);
135141
const entryPointConfig =
136142
config.getPackageConfig(packagePath, packageVersion).entryPoints[entryPointPath];
137143
let entryPointPackageJson: EntryPointPackageJson;
@@ -173,6 +179,7 @@ export function getEntryPointInfo(
173179
const entryPointInfo: EntryPoint = {
174180
name: entryPointPackageJson.name,
175181
path: entryPointPath,
182+
packageName,
176183
packagePath,
177184
packageJson: entryPointPackageJson,
178185
typings: resolve(entryPointPath, typings),
@@ -304,15 +311,44 @@ function guessTypingsFromPackageJson(
304311
}
305312

306313
/**
307-
* Find the version of the package at `packageJsonPath`.
314+
* Find or infer the name and version of a package.
308315
*
309-
* The version is read off of the `version` property of the package's `package.json` file (if
310-
* available).
316+
* - The name is computed based on the `name` property of the package's or the entry-point's
317+
* `package.json` file (if available) or inferred from the package's path.
318+
* - The version is read off of the `version` property of the package's `package.json` file (if
319+
* available).
311320
*
312-
* @param packageJson the parsed `package.json` of the package (if available).
313-
* @returns the version string or `null` if the `pckage.json` file is missing or doesn't contain a
314-
* version.
321+
* @param fs The `FileSystem` instance to use for parsing `packagePath` (if needed).
322+
* @param packagePath the absolute path to the package.
323+
* @param packagePackageJson the parsed `package.json` of the package (if available).
324+
* @param entryPointPackageJson the parsed `package.json` of an entry-point (if available).
325+
* @returns the computed name and version of the package.
315326
*/
316-
function getPackageVersion(packageJson: EntryPointPackageJson|null): string|null {
317-
return packageJson?.version ?? null;
327+
function getPackageNameAndVersion(
328+
fs: FileSystem, packagePath: AbsoluteFsPath, packagePackageJson: EntryPointPackageJson|null,
329+
entryPointPackageJson: EntryPointPackageJson|
330+
null): {packageName: string, packageVersion: string|null} {
331+
let packageName: string;
332+
333+
if (packagePackageJson !== null) {
334+
// We have a valid `package.json` for the package: Get the package name from that.
335+
packageName = packagePackageJson.name;
336+
} else if (entryPointPackageJson !== null) {
337+
// We have a valid `package.json` for the entry-point: Get the package name from that.
338+
// This might be a secondary entry-point, so make sure we only keep the main package's name
339+
// (e.g. only keep `@angular/common` from `@angular/common/http`).
340+
packageName = /^(?:@[^/]+\/)?[^/]*/.exec(entryPointPackageJson.name)![0];
341+
} else {
342+
// We don't have a valid `package.json`: Infer the package name from the package's path.
343+
const lastSegment = fs.basename(packagePath);
344+
const secondLastSegment = fs.basename(fs.dirname(packagePath));
345+
346+
packageName =
347+
secondLastSegment.startsWith('@') ? `${secondLastSegment}/${lastSegment}` : lastSegment;
348+
}
349+
350+
return {
351+
packageName,
352+
packageVersion: packagePackageJson?.version ?? null,
353+
};
318354
}

packages/compiler-cli/ngcc/test/dependencies/dependency_resolver_spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -60,38 +60,47 @@ runInEachFileSystem(() => {
6060
first = {
6161
name: 'first',
6262
path: _('/first'),
63+
packageName: 'first',
6364
packagePath: _('/first'),
6465
packageJson: {esm5: './index.js'},
6566
typings: _('/first/index.d.ts'),
6667
compiledByAngular: true,
6768
ignoreMissingDependencies: false,
6869
} as EntryPoint;
6970
second = {
71+
name: 'second',
7072
path: _('/second'),
73+
packageName: 'second',
7174
packagePath: _('/second'),
7275
packageJson: {esm2015: './sub/index.js'},
7376
typings: _('/second/sub/index.d.ts'),
7477
compiledByAngular: true,
7578
ignoreMissingDependencies: false,
7679
} as EntryPoint;
7780
third = {
81+
name: 'third',
7882
path: _('/third'),
83+
packageName: 'third',
7984
packagePath: _('/third'),
8085
packageJson: {fesm5: './index.js'},
8186
typings: _('/third/index.d.ts'),
8287
compiledByAngular: true,
8388
ignoreMissingDependencies: false,
8489
} as EntryPoint;
8590
fourth = {
91+
name: 'fourth',
8692
path: _('/fourth'),
93+
packageName: 'fourth',
8794
packagePath: _('/fourth'),
8895
packageJson: {fesm2015: './sub2/index.js'},
8996
typings: _('/fourth/sub2/index.d.ts'),
9097
compiledByAngular: true,
9198
ignoreMissingDependencies: false,
9299
} as EntryPoint;
93100
fifth = {
101+
name: 'fifth',
94102
path: _('/fifth'),
103+
packageName: 'fifth',
95104
packagePath: _('/fifth'),
96105
packageJson: {module: './index.js'},
97106
typings: _('/fifth/index.d.ts'),
@@ -100,7 +109,9 @@ runInEachFileSystem(() => {
100109
} as EntryPoint;
101110

102111
sixthIgnoreMissing = {
112+
name: 'sixth',
103113
path: _('/sixth'),
114+
packageName: 'sixth',
104115
packagePath: _('/sixth'),
105116
packageJson: {module: './index.js'},
106117
typings: _('/sixth/index.d.ts'),
@@ -291,6 +302,7 @@ runInEachFileSystem(() => {
291302
const testEntryPoint = {
292303
name: 'test-package',
293304
path: _('/project/node_modules/test-package'),
305+
packageName: 'test-package',
294306
packagePath: _('/project/node_modules/test-package'),
295307
packageJson: {esm5: './index.js'},
296308
typings: _('/project/node_modules/test-package/index.d.ts'),

packages/compiler-cli/ngcc/test/helpers/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function makeTestEntryPoint(
2222
return {
2323
name: entryPointName,
2424
path: absoluteFrom(`/node_modules/${entryPointName}`),
25+
packageName,
2526
packagePath: absoluteFrom(`/node_modules/${packageName}`),
2627
packageJson: {name: entryPointName},
2728
typings: absoluteFrom(`/node_modules/${entryPointName}/index.d.ts`),

packages/compiler-cli/ngcc/test/integration/ngcc_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ runInEachFileSystem(() => {
15721572
},
15731573
{
15741574
name: _('/node_modules/deep_import/package.json'),
1575-
contents: '{"name": "deep-import", "es2015": "./index.js", "typings": "./index.d.ts"}',
1575+
contents: '{"name": "deep_import", "es2015": "./index.js", "typings": "./index.d.ts"}',
15761576
},
15771577
{
15781578
name: _('/node_modules/deep_import/entry_point.js'),

packages/compiler-cli/ngcc/test/packages/entry_point_bundle_spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ runInEachFileSystem(() => {
168168
const entryPoint: EntryPoint = {
169169
name: 'test',
170170
path: absoluteFrom('/node_modules/test'),
171+
packageName: 'test',
171172
packagePath: absoluteFrom('/node_modules/test'),
172173
packageJson: {name: 'test'},
173174
typings: absoluteFrom('/node_modules/test/index.d.ts'),
@@ -218,6 +219,7 @@ runInEachFileSystem(() => {
218219
const entryPoint: EntryPoint = {
219220
name: 'test',
220221
path: absoluteFrom('/node_modules/test'),
222+
packageName: 'test',
221223
packagePath: absoluteFrom('/node_modules/test'),
222224
packageJson: {name: 'test'},
223225
typings: absoluteFrom('/node_modules/test/index.d.ts'),
@@ -240,6 +242,7 @@ runInEachFileSystem(() => {
240242
const entryPoint: EntryPoint = {
241243
name: 'internal',
242244
path: absoluteFrom('/node_modules/internal'),
245+
packageName: 'internal',
243246
packagePath: absoluteFrom('/node_modules/internal'),
244247
packageJson: {name: 'internal'},
245248
typings: absoluteFrom('/node_modules/internal/index.d.ts'),
@@ -262,6 +265,7 @@ runInEachFileSystem(() => {
262265
const entryPoint: EntryPoint = {
263266
name: 'test',
264267
path: absoluteFrom('/node_modules/test'),
268+
packageName: 'test',
265269
packagePath: absoluteFrom('/node_modules/test'),
266270
packageJson: {name: 'test'},
267271
typings: absoluteFrom('/node_modules/test/index.d.ts'),
@@ -285,6 +289,7 @@ runInEachFileSystem(() => {
285289
const entryPoint: EntryPoint = {
286290
name: 'secondary',
287291
path: absoluteFrom('/node_modules/primary/secondary'),
292+
packageName: 'primary',
288293
packagePath: absoluteFrom('/node_modules/primary'),
289294
packageJson: {name: 'secondary'},
290295
typings: absoluteFrom('/node_modules/primary/secondary/index.d.ts'),

packages/compiler-cli/ngcc/test/packages/entry_point_manifest_spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ runInEachFileSystem(() => {
149149
entryPoint: {
150150
name: 'some_package/valid_entry_point',
151151
path: _Abs('/project/node_modules/some_package/valid_entry_point'),
152+
packageName: 'some_package',
152153
packagePath: _Abs('/project/node_modules/some_package'),
153154
packageJson: jasmine.any(Object),
154155
typings:

0 commit comments

Comments
 (0)