chore: add importAs to metadata generated for unit tests

This is necessary so that, when generating ngInjectableDefs, ngc can
correctly determine the import location for injected dependencies.
Without this, the generated imports use a relative path to dist instead
of e.g. "@angular/cdk/overlay". In real life it's not a problem because
the packages are resolved to node_modules.
This commit is contained in:
Jeremy Elbourn
2018-05-23 17:30:09 -07:00
committed by Andrew Seguin
parent 3681ddfbfa
commit de120ad5d4
3 changed files with 5186 additions and 5972 deletions
+5150 -5965
View File
File diff suppressed because it is too large Load Diff
+13 -7
View File
@@ -3,7 +3,11 @@ import {red} from 'chalk';
import {PackageBundler} from './build-bundles';
import {buildConfig} from './build-config';
import {getSecondaryEntryPointsForPackage} from './secondary-entry-points';
import {compileEntryPoint, renamePrivateReExportsToBeUnique} from './compile-entry-point';
import {
addImportAsToAllMetadata,
compileEntryPoint,
renamePrivateReExportsToBeUnique,
} from './compile-entry-point';
import {ngcCompile} from './ngc-compile';
const {packagesDir, outputDir} = buildConfig;
@@ -92,13 +96,15 @@ export class BuildPackage {
/** Compiles the TypeScript sources of a primary or secondary entry point. */
private _compileTestEntryPoint(tsconfigName: string, secondaryEntryPoint = ''): Promise<any> {
const entryPointPath = join(this.sourceDir, secondaryEntryPoint);
const entryPointTsconfigPath = join(entryPointPath, tsconfigName);
const tsconfigPath = join(entryPointPath, tsconfigName);
return ngcCompile(['-p', entryPointTsconfigPath]).catch(() => {
const error = red(`Failed to compile ${secondaryEntryPoint} using ${entryPointTsconfigPath}`);
console.error(error);
return Promise.reject(error);
});
return ngcCompile(['-p', tsconfigPath])
.then(() => addImportAsToAllMetadata(this))
.catch(() => {
const error = red(`Failed to compile ${secondaryEntryPoint} using ${tsconfigPath}`);
console.error(error);
return Promise.reject(error);
});
}
/** Stores the secondary entry-points for this package if they haven't been computed already. */
@@ -26,6 +26,19 @@ export async function compileEntryPoint(buildPackage: BuildPackage, tsconfigName
});
}
/**
* Adds `importAs` property to all generated metadata.json files for a package's secondary
* entry-points. This is necessary for unit tests so that the imports generated by using the
* `providedIn` syntax are able to resolve symbols to the "expected" location (e.g.
* "@angular/cdk/overlay" instead of a local relative path). Assumes that the package has already
* been built.
*/
export function addImportAsToAllMetadata(buildPackage: BuildPackage) {
for (const entryPoint of buildPackage.secondaryEntryPoints) {
addImportAs(buildPackage.name, buildPackage.outputDir, entryPoint);
}
}
/** Renames `ɵa`-style re-exports generated by Angular to be unique across compilation units. */
export function renamePrivateReExportsToBeUnique(buildPackage: BuildPackage,
secondaryEntryPoint = '') {
@@ -44,6 +57,16 @@ export function renamePrivateReExportsToBeUnique(buildPackage: BuildPackage,
}
}
/** Adds `importAs` property to generated all metadata.json files for an entry-point. */
function addImportAs(packageName: string, outputPath: string, secondaryEntryPoint: string): void {
const path = join(outputPath, secondaryEntryPoint);
glob(join(path, '**/*.+(metadata.json)')).forEach(metadataPath => {
let metadata = JSON.parse(readFileSync(metadataPath, 'utf-8'));
metadata[0]['importAs'] = `@angular/${packageName}/${secondaryEntryPoint}`;
writeFileSync(metadataPath, JSON.stringify(metadata), 'utf-8');
});
}
/** Updates exports in designated folder with identifier specified. */
function addIdToGlob(outputPath: string, entryPointId: number): void {
glob(join(outputPath, '**/*.+(js|d.ts|metadata.json)')).forEach(filePath => {