Skip to content

Commit 74a9c46

Browse files
devversionjosephperrott
authored andcommitted
build: fix incorrect amd module names (angular#14786)
* Our UMD bundles currently have wrong AMD module names for secondary entry-points which are dash-cased. e.g. AMD module name for the bottom-sheet is `@angular/material/bottomSheet`. * Our UMD bundles currently reference incorrect rollup globals. e.g. the AMD module name is `ng.material-experimental.{secondary}`, while it should be `ng.materialExperimental`.
1 parent b2a6b7a commit 74a9c46

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

tools/package-tools/build-bundles.ts

+38-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ const bundlesDir = join(buildConfig.outputDir, 'bundles');
1717

1818
/** Utility for creating bundles from raw ngc output. */
1919
export class PackageBundler {
20-
constructor(private buildPackage: BuildPackage) {}
20+
21+
/** Name of the AMD module for the primary entry point of the build package. */
22+
private readonly primaryAmdModuleName: string;
23+
24+
constructor(private buildPackage: BuildPackage) {
25+
this.primaryAmdModuleName = this.getAmdModuleName(buildPackage.name);
26+
}
2127

2228
/** Creates all bundles for the package and all associated entry points (UMD, ES5, ES2015). */
2329
async createBundles() {
@@ -35,8 +41,8 @@ export class PackageBundler {
3541
return this.bundleEntryPoint({
3642
entryFile: this.buildPackage.entryFilePath,
3743
esm5EntryFile: join(this.buildPackage.esm5OutputDir, 'index.js'),
38-
importName: `@angular/${this.buildPackage.name}`,
39-
moduleName: `ng.${this.buildPackage.name}`,
44+
importName: `@angular/${packageName}`,
45+
moduleName: this.primaryAmdModuleName,
4046
esm2015Dest: join(bundlesDir, `${packageName}.js`),
4147
esm5Dest: join(bundlesDir, `${packageName}.es5.js`),
4248
umdDest: join(bundlesDir, `${packageName}.umd.js`),
@@ -45,21 +51,20 @@ export class PackageBundler {
4551
}
4652

4753
/** Bundles a single secondary entry-point w/ given entry file, e.g. @angular/cdk/a11y */
48-
private async bundleSecondaryEntryPoint(entryPoint: string) {
54+
private async bundleSecondaryEntryPoint(entryPointName: string) {
4955
const packageName = this.buildPackage.name;
50-
const entryFile = join(this.buildPackage.outputDir, entryPoint, 'index.js');
51-
const esm5EntryFile = join(this.buildPackage.esm5OutputDir, entryPoint, 'index.js');
52-
const dashedEntryName = dashCaseToCamelCase(entryPoint);
56+
const entryFile = join(this.buildPackage.outputDir, entryPointName, 'index.js');
57+
const esm5EntryFile = join(this.buildPackage.esm5OutputDir, entryPointName, 'index.js');
5358

5459
return this.bundleEntryPoint({
5560
entryFile,
5661
esm5EntryFile,
57-
importName: `@angular/${this.buildPackage.name}/${dashedEntryName}`,
58-
moduleName: `ng.${packageName}.${dashedEntryName}`,
59-
esm2015Dest: join(bundlesDir, `${packageName}`, `${entryPoint}.js`),
60-
esm5Dest: join(bundlesDir, `${packageName}`, `${entryPoint}.es5.js`),
61-
umdDest: join(bundlesDir, `${packageName}-${entryPoint}.umd.js`),
62-
umdMinDest: join(bundlesDir, `${packageName}-${entryPoint}.umd.min.js`),
62+
importName: `@angular/${packageName}/${entryPointName}`,
63+
moduleName: this.getAmdModuleName(packageName, entryPointName),
64+
esm2015Dest: join(bundlesDir, `${packageName}`, `${entryPointName}.js`),
65+
esm5Dest: join(bundlesDir, `${packageName}`, `${entryPointName}.es5.js`),
66+
umdDest: join(bundlesDir, `${packageName}-${entryPointName}.umd.js`),
67+
umdMinDest: join(bundlesDir, `${packageName}-${entryPointName}.umd.min.js`),
6368
});
6469
}
6570

@@ -154,7 +159,7 @@ export class PackageBundler {
154159
// secondary entry-points from the rollup globals because we want the UMD for the
155160
// primary entry-point to include *all* of the sources for those entry-points.
156161
if (this.buildPackage.exportsSecondaryEntryPointsAtRoot &&
157-
config.moduleName === `ng.${this.buildPackage.name}`) {
162+
config.moduleName === this.primaryAmdModuleName) {
158163

159164
const importRegex = new RegExp(`@angular/${this.buildPackage.name}/.+`);
160165
external = external.filter(e => !importRegex.test(e));
@@ -185,8 +190,26 @@ export class PackageBundler {
185190
return map;
186191
}, {} as {[key: string]: string});
187192
}
188-
}
189193

194+
/**
195+
* Gets the AMD module name for a package and an optional entry point. This is consistent
196+
* to the module name format being used in "angular/angular".
197+
*/
198+
private getAmdModuleName(packageName: string, entryPointName?: string) {
199+
// For example, the AMD module name for the "@angular/material-examples" package should be
200+
// "ng.materialExamples". We camel-case the package name in case it contains dashes.
201+
let amdModuleName = `ng.${dashCaseToCamelCase(packageName)}`;
202+
203+
if (entryPointName) {
204+
// For example, the "@angular/material/bottom-sheet" entry-point should be converted into
205+
// the following AMD module name: "ng.material.bottomSheet". Similar to the package name,
206+
// the entry-point name needs to be camel-cased in case it contains dashes.
207+
amdModuleName += `.${dashCaseToCamelCase(entryPointName)}`;
208+
}
209+
210+
return amdModuleName;
211+
}
212+
}
190213

191214
/** Configuration for creating library bundles. */
192215
interface BundlesConfig {

0 commit comments

Comments
 (0)