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

Commit

Permalink
build: avoid incorrect provider definition renaming (#1191)
Browse files Browse the repository at this point in the history
NGC already supports lazy providers and generates definitions for
these providers. Previously those definitions were called
`ngInjectableDef`. With recent changes in Angular, these definition
members have been renamed to `ɵprov`. This conflicts with the legacy
build system that was originally designed for View Engine. The build
system / package-tools rename this member to be unique.

The logic for renaming private members generated by the NGC flat module
bundler exists because individual secondary entry-points are re-exported
in the primary entry-point, and we want to avoid symbol conflicts there.

To fix this in a more reasonable way (same concept used in
`@angular/bazel`), we specify a unique prefix for each entry-point by
leveraging the `flatModulePrivateSymbolPrefix` option.
  • Loading branch information
devversion authored Feb 5, 2020
1 parent f8e9d75 commit 12b58a1
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/lib/core/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/flex-layout/core",
"flatModulePrivateSymbolPrefix": "core_private",
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/extended/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/flex-layout/extended",
"flatModulePrivateSymbolPrefix": "extended_private",
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/flex/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/flex-layout/flex",
"flatModulePrivateSymbolPrefix": "flex_private",
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/grid/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/flex-layout/grid",
"flatModulePrivateSymbolPrefix": "grid_private",
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/server/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/flex-layout/server",
"flatModulePrivateSymbolPrefix": "server_private",
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/flex-layout",
"flatModulePrivateSymbolPrefix": "flex_layout_private",
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
Expand Down
9 changes: 2 additions & 7 deletions tools/package-tools/build-package.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {join} from 'path';
import {PackageBundler} from './build-bundles';
import {buildConfig} from './build-config';
import {
addImportAsToAllMetadata,
compileEntryPoint,
renamePrivateReExportsToBeUnique,
} from './compile-entry-point';
import {addImportAsToAllMetadata, compileEntryPoint} from './compile-entry-point';
import {getSecondaryEntryPointsForPackage} from './secondary-entry-points';

const {packagesDir, outputDir} = buildConfig;
Expand Down Expand Up @@ -97,8 +93,7 @@ export class BuildPackage {
/** Compiles TS into both ES2015 and ES5, then updates exports. */
private async _compileBothTargets(p = '') {
return compileEntryPoint(this, buildTsconfigName, p)
.then(() => compileEntryPoint(this, buildTsconfigName, p, this.esm5OutputDir))
.then(() => renamePrivateReExportsToBeUnique(this, p));
.then(() => compileEntryPoint(this, buildTsconfigName, p, this.esm5OutputDir));
}

/** Stores the secondary entry-points for this package if they haven't been computed already. */
Expand Down
34 changes: 0 additions & 34 deletions tools/package-tools/compile-entry-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import {red} from 'chalk';
import {BuildPackage} from './build-package';
import {ngcCompile} from './ngc-compile';

/** Incrementing ID counter. */
let nextId = 0;

/** Compiles the TypeScript sources of a primary or secondary entry point. */
export async function compileEntryPoint(buildPackage: BuildPackage, tsconfigName: string,
secondaryEntryPoint = '', es5OutputPath?: string) {
Expand Down Expand Up @@ -39,24 +36,6 @@ export function addImportAsToAllMetadata(buildPackage: BuildPackage) {
}
}

/** Renames `ɵa`-style re-exports generated by Angular to be unique across compilation units. */
export function renamePrivateReExportsToBeUnique(buildPackage: BuildPackage,
secondaryEntryPoint = '') {
// When we compiled the typescript sources with ngc, we do entry-point individually.
// If the root-level module re-exports multiple of these entry-points, the private-export
// identifiers (e.g., `ɵa`) generated by ngc will collide. We work around this by suffixing
// each of these identifiers with an ID specific to this entry point. We make this
// replacement in the js, d.ts, and metadata output.
if (buildPackage.exportsSecondaryEntryPointsAtRoot && secondaryEntryPoint) {
const entryPointId = nextId++;
const outputPath = join(buildPackage.outputDir, secondaryEntryPoint);
const esm5OutputPath = join(buildPackage.esm5OutputDir, secondaryEntryPoint);

addIdToGlob(outputPath, entryPointId);
addIdToGlob(esm5OutputPath, entryPointId);
}
}

/** 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);
Expand All @@ -66,16 +45,3 @@ function addImportAs(packageName: string, outputPath: string, secondaryEntryPoin
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 => {
let fileContent = readFileSync(filePath, 'utf-8');
// We check for double ɵ to avoid mangling symbols like `ɵɵdefineInjectable`.
fileContent = fileContent.replace(/ɵ(ɵ)?[a-z]+/g,
(match, isDoubleTheta) => {
return isDoubleTheta ? match : match + entryPointId;
});
writeFileSync(filePath, fileContent, 'utf-8');
});
}

0 comments on commit 12b58a1

Please sign in to comment.