Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 984f1c0

Browse files
authored
build: fix random ngc resolve failures (#807)
Equivalent to angular/components#12103
1 parent a6b2960 commit 984f1c0

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

tools/package-tools/build-package.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {join} from 'path';
2-
import {red} from 'chalk';
32
import {PackageBundler} from './build-bundles';
43
import {buildConfig} from './build-config';
4+
import {
5+
addImportAsToAllMetadata,
6+
compileEntryPoint,
7+
renamePrivateReExportsToBeUnique,
8+
} from './compile-entry-point';
59
import {getSecondaryEntryPointsForPackage} from './secondary-entry-points';
6-
import {compileEntryPoint, renamePrivateReExportsToBeUnique} from './compile-entry-point';
7-
import {ngcCompile} from './ngc-compile';
810

911
const {packagesDir, outputDir} = buildConfig;
1012

@@ -74,7 +76,8 @@ export class BuildPackage {
7476

7577
/** Compiles the TypeScript test source files for the package. */
7678
async compileTests() {
77-
await this._compileTestEntryPoint(testsTsconfigName);
79+
return compileEntryPoint(this, testsTsconfigName)
80+
.then(() => addImportAsToAllMetadata(this));
7881
}
7982

8083
/** Creates all bundles for the package and all associated entry points. */
@@ -89,18 +92,6 @@ export class BuildPackage {
8992
.then(() => renamePrivateReExportsToBeUnique(this, p));
9093
}
9194

92-
/** Compiles the TypeScript sources of a primary or secondary entry point. */
93-
private _compileTestEntryPoint(tsconfigName: string, secondaryEntryPoint = ''): Promise<any> {
94-
const entryPointPath = join(this.sourceDir, secondaryEntryPoint);
95-
const entryPointTsconfigPath = join(entryPointPath, tsconfigName);
96-
97-
return ngcCompile(['-p', entryPointTsconfigPath]).catch(() => {
98-
const error = red(`Failed to compile ${secondaryEntryPoint} using ${entryPointTsconfigPath}`);
99-
console.error(error);
100-
return Promise.reject(error);
101-
});
102-
}
103-
10495
/** Stores the secondary entry-points for this package if they haven't been computed already. */
10596
private cacheSecondaryEntryPoints() {
10697
if (!this._secondaryEntryPoints) {

tools/package-tools/compile-entry-point.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ export async function compileEntryPoint(buildPackage: BuildPackage, tsconfigName
2626
});
2727
}
2828

29+
/**
30+
* Adds `importAs` property to all generated metadata.json files for a package's secondary
31+
* entry-points. This is necessary for unit tests so that the imports generated by using the
32+
* `providedIn` syntax are able to resolve symbols to the "expected" location (e.g.
33+
* "@angular/cdk/overlay" instead of a local relative path). Assumes that the package has already
34+
* been built.
35+
*/
36+
export function addImportAsToAllMetadata(buildPackage: BuildPackage) {
37+
for (const entryPoint of buildPackage.secondaryEntryPoints) {
38+
addImportAs(buildPackage.name, buildPackage.outputDir, entryPoint);
39+
}
40+
}
41+
2942
/** Renames `ɵa`-style re-exports generated by Angular to be unique across compilation units. */
3043
export function renamePrivateReExportsToBeUnique(buildPackage: BuildPackage,
3144
secondaryEntryPoint = '') {
@@ -44,6 +57,16 @@ export function renamePrivateReExportsToBeUnique(buildPackage: BuildPackage,
4457
}
4558
}
4659

60+
/** Adds `importAs` property to generated all metadata.json files for an entry-point. */
61+
function addImportAs(packageName: string, outputPath: string, secondaryEntryPoint: string): void {
62+
const path = join(outputPath, secondaryEntryPoint);
63+
glob(join(path, '**/*.+(metadata.json)')).forEach(metadataPath => {
64+
let metadata = JSON.parse(readFileSync(metadataPath, 'utf-8'));
65+
metadata[0]['importAs'] = `@angular/${packageName}/${secondaryEntryPoint}`;
66+
writeFileSync(metadataPath, JSON.stringify(metadata), 'utf-8');
67+
});
68+
}
69+
4770
/** Updates exports in designated folder with identifier specified. */
4871
function addIdToGlob(outputPath: string, entryPointId: number): void {
4972
glob(join(outputPath, '**/*.+(js|d.ts|metadata.json)')).forEach(filePath => {

tools/package-tools/gulp/build-tasks-gulp.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ export function createPackageBuildTasks(buildPackage: BuildPackage) {
5454
));
5555

5656
task(`${taskName}:build-no-bundles`, sequenceTask(
57+
// Build assets before building the ESM output. Since we compile with NGC, the compiler
58+
// tries to resolve all required assets.
59+
`${taskName}:assets`,
5760
// Build the ESM output that includes all test files. Also build assets for the package.
58-
[`${taskName}:build:esm:tests`, `${taskName}:assets`],
61+
`${taskName}:build:esm:tests`,
5962
// Inline assets into ESM output.
6063
`${taskName}:assets:inline`
6164
));

0 commit comments

Comments
 (0)