refactor(ivy): ngcc - make `EntryPoint` an interface rather than a type (#26906)
By inverting the relationship between `EntryPointPaths` and `EntryPointFormat` we can have interfaces rather than types. Thanks to @gkalpak for this idea. PR Close #26906
This commit is contained in:
parent
0c6e1f4a1b
commit
84ce45ca16
|
@ -10,23 +10,27 @@ import * as path from 'canonical-path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The possible values for the format of an entry-point.
|
|
||||||
*/
|
|
||||||
export type EntryPointFormat = 'esm5' | 'fesm5' | 'esm2015' | 'fesm2015' | 'umd';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An object containing paths to the entry-points for each format.
|
* An object containing paths to the entry-points for each format.
|
||||||
*/
|
*/
|
||||||
export type EntryPointPaths = {
|
export interface EntryPointPaths {
|
||||||
[Format in EntryPointFormat]?: string;
|
esm5?: string;
|
||||||
};
|
fesm5?: string;
|
||||||
|
esm2015?: string;
|
||||||
|
fesm2015?: string;
|
||||||
|
umd?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The possible values for the format of an entry-point.
|
||||||
|
*/
|
||||||
|
export type EntryPointFormat = keyof(EntryPointPaths);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An object containing information about an entry-point, including paths
|
* An object containing information about an entry-point, including paths
|
||||||
* to each of the possible entry-point formats.
|
* to each of the possible entry-point formats.
|
||||||
*/
|
*/
|
||||||
export type EntryPoint = EntryPointPaths & {
|
export interface EntryPoint extends EntryPointPaths {
|
||||||
/** The name of the package (e.g. `@angular/core`). */
|
/** The name of the package (e.g. `@angular/core`). */
|
||||||
name: string;
|
name: string;
|
||||||
/** The path to the package that contains this entry-point. */
|
/** The path to the package that contains this entry-point. */
|
||||||
|
@ -35,8 +39,11 @@ export type EntryPoint = EntryPointPaths & {
|
||||||
path: string;
|
path: string;
|
||||||
/** The path to a typings (.d.ts) file for this entry-point. */
|
/** The path to a typings (.d.ts) file for this entry-point. */
|
||||||
typings: string;
|
typings: string;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The properties that may be loaded from the `package.json` file.
|
||||||
|
*/
|
||||||
interface EntryPointPackageJson {
|
interface EntryPointPackageJson {
|
||||||
name: string;
|
name: string;
|
||||||
fesm2015?: string;
|
fesm2015?: string;
|
||||||
|
@ -66,13 +73,13 @@ function loadEntryPointPackage(packageJsonPath: string): {[key: string]: any}|nu
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to get entry point info from the given path.
|
* Try to get an entry point from the given path.
|
||||||
* @param pkgPath the absolute path to the containing npm package
|
* @param packagePath the absolute path to the containing npm package
|
||||||
* @param entryPoint the absolute path to the potential entry point.
|
* @param entryPointPath the absolute path to the potential entry point.
|
||||||
* @returns Info about the entry point if it is valid, `null` otherwise.
|
* @returns Info about the entry point if it is valid, `null` otherwise.
|
||||||
*/
|
*/
|
||||||
export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoint|null {
|
export function getEntryPointInfo(packagePath: string, entryPointPath: string): EntryPoint|null {
|
||||||
const packageJsonPath = path.resolve(entryPoint, 'package.json');
|
const packageJsonPath = path.resolve(entryPointPath, 'package.json');
|
||||||
if (!fs.existsSync(packageJsonPath)) {
|
if (!fs.existsSync(packageJsonPath)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -101,33 +108,33 @@ export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoi
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also we need to have a metadata.json file
|
// Also there must exist a `metadata.json` file next to the typings entry-point.
|
||||||
const metadataPath = path.resolve(entryPoint, typings.replace(/\.d\.ts$/, '') + '.metadata.json');
|
const metadataPath = path.resolve(entryPointPath, typings.replace(/\.d\.ts$/, '') + '.metadata.json');
|
||||||
if (!fs.existsSync(metadataPath)) {
|
if (!fs.existsSync(metadataPath)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const entryPointInfo: EntryPoint = {
|
const entryPointInfo: EntryPoint = {
|
||||||
name,
|
name,
|
||||||
package: pkgPath,
|
package: packagePath,
|
||||||
path: entryPoint,
|
path: entryPointPath,
|
||||||
typings: path.resolve(entryPoint, typings),
|
typings: path.resolve(entryPointPath, typings),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (esm2015) {
|
if (esm2015) {
|
||||||
entryPointInfo.esm2015 = path.resolve(entryPoint, esm2015);
|
entryPointInfo.esm2015 = path.resolve(entryPointPath, esm2015);
|
||||||
}
|
}
|
||||||
if (fesm2015) {
|
if (fesm2015) {
|
||||||
entryPointInfo.fesm2015 = path.resolve(entryPoint, fesm2015);
|
entryPointInfo.fesm2015 = path.resolve(entryPointPath, fesm2015);
|
||||||
}
|
}
|
||||||
if (fesm5) {
|
if (fesm5) {
|
||||||
entryPointInfo.fesm5 = path.resolve(entryPoint, fesm5);
|
entryPointInfo.fesm5 = path.resolve(entryPointPath, fesm5);
|
||||||
}
|
}
|
||||||
if (esm5) {
|
if (esm5) {
|
||||||
entryPointInfo.esm5 = path.resolve(entryPoint, esm5);
|
entryPointInfo.esm5 = path.resolve(entryPointPath, esm5);
|
||||||
}
|
}
|
||||||
if (main) {
|
if (main) {
|
||||||
entryPointInfo.umd = path.resolve(entryPoint, main);
|
entryPointInfo.umd = path.resolve(entryPointPath, main);
|
||||||
}
|
}
|
||||||
|
|
||||||
return entryPointInfo;
|
return entryPointInfo;
|
||||||
|
|
Loading…
Reference in New Issue