Skip to content

Commit 4168fb3

Browse files
devversiontinayuangao
authored andcommitted
build: stop hardcoding rollup globals for CDK entry points (#6150)
* Now the rollup globals for the CDK secondary entry-points are no longer hard-coded. There is already a method called `getSecondaryEntryPointsForPackage` which can be used.
1 parent 8a23157 commit 4168fb3

File tree

5 files changed

+96
-77
lines changed

5 files changed

+96
-77
lines changed

tools/package-tools/rollup-globals.json

Lines changed: 0 additions & 71 deletions
This file was deleted.

tools/package-tools/rollup-globals.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {getSecondaryEntryPointsForPackage} from './secondary-entry-points';
2+
3+
/** Method that converts dash-case strings to a camel-based string. */
4+
const dashCaseToCamelCase = (str: string) => str.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
5+
6+
/** List of available secondary entry-points for the CDK package. */
7+
const cdkSecondaryEntryPoints = getSecondaryEntryPointsForPackage('cdk');
8+
9+
/** Object with all CDK entry points in the format of Rollup globals. */
10+
const rollupCdkEntryPoints = cdkSecondaryEntryPoints.reduce((globals: any, entryPoint: string) => {
11+
globals[`@angular/cdk/${entryPoint}`] = `ng.cdk.${dashCaseToCamelCase(entryPoint)}`;
12+
return globals;
13+
}, {});
14+
15+
/** Map of globals that are used inside of the different packages. */
16+
export const rollupGlobals = {
17+
'tslib': 'tslib',
18+
19+
'@angular/animations': 'ng.animations',
20+
'@angular/core': 'ng.core',
21+
'@angular/common': 'ng.common',
22+
'@angular/forms': 'ng.forms',
23+
'@angular/http': 'ng.http',
24+
'@angular/router': 'ng.router',
25+
'@angular/platform-browser': 'ng.platformBrowser',
26+
'@angular/platform-server': 'ng.platformServer',
27+
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
28+
'@angular/platform-browser/animations': 'ng.platformBrowser.animations',
29+
'@angular/core/testing': 'ng.core.testing',
30+
'@angular/common/testing': 'ng.common.testing',
31+
'@angular/http/testing': 'ng.http.testing',
32+
33+
34+
'@angular/material': 'ng.material',
35+
'@angular/cdk': 'ng.cdk',
36+
37+
// Include secondary entry-points of the CDK package
38+
...rollupCdkEntryPoints,
39+
40+
// Some packages are not really needed for the UMD bundles, but for the missingRollupGlobals rule.
41+
// TODO(devversion): remove by adding minimatch and better globbing to rules
42+
'@angular/cdk/testing': 'ng.cdk.testing',
43+
'@angular/material-examples': 'ng.materialExamples',
44+
45+
'rxjs/BehaviorSubject': 'Rx',
46+
'rxjs/Observable': 'Rx',
47+
'rxjs/Subject': 'Rx',
48+
'rxjs/Subscription': 'Rx',
49+
'rxjs/Observer': 'Rx',
50+
'rxjs/Scheduler': 'Rx',
51+
'rxjs/observable/combineLatest': 'Rx.Observable',
52+
'rxjs/observable/forkJoin': 'Rx.Observable',
53+
'rxjs/observable/fromEvent': 'Rx.Observable',
54+
'rxjs/observable/merge': 'Rx.Observable',
55+
'rxjs/observable/of': 'Rx.Observable',
56+
'rxjs/observable/throw': 'Rx.Observable',
57+
'rxjs/operator/auditTime': 'Rx.Observable.prototype',
58+
'rxjs/operator/catch': 'Rx.Observable.prototype',
59+
'rxjs/operator/debounceTime': 'Rx.Observable.prototype',
60+
'rxjs/operator/do': 'Rx.Observable.prototype',
61+
'rxjs/operator/filter': 'Rx.Observable.prototype',
62+
'rxjs/operator/finally': 'Rx.Observable.prototype',
63+
'rxjs/operator/first': 'Rx.Observable.prototype',
64+
'rxjs/operator/let': 'Rx.Observable.prototype',
65+
'rxjs/operator/map': 'Rx.Observable.prototype',
66+
'rxjs/operator/share': 'Rx.Observable.prototype',
67+
'rxjs/operator/startWith': 'Rx.Observable.prototype',
68+
'rxjs/operator/switchMap': 'Rx.Observable.prototype',
69+
'rxjs/operator/takeUntil': 'Rx.Observable.prototype',
70+
'rxjs/operator/toPromise': 'Rx.Observable.prototype',
71+
72+
'rxjs/add/observable/merge': 'Rx.Observable',
73+
'rxjs/add/observable/fromEvent': 'Rx.Observable',
74+
'rxjs/add/observable/of': 'Rx.Observable',
75+
'rxjs/add/observable/interval': 'Rx.Observable',
76+
'rxjs/add/operator/startWith': 'Rx.Observable.prototype',
77+
'rxjs/add/operator/map': 'Rx.Observable.prototype',
78+
'rxjs/add/operator/debounceTime': 'Rx.Observable.prototype',
79+
'rxjs/add/operator/distinctUntilChanged': 'Rx.Observable.prototype',
80+
'rxjs/add/operator/first': 'Rx.Observable.prototype',
81+
'rxjs/add/operator/catch': 'Rx.Observable.prototype',
82+
'rxjs/add/operator/switchMap': 'Rx.Observable.prototype'
83+
};

tools/package-tools/rollup-helpers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {buildConfig} from './build-config';
22
import {rollupRemoveLicensesPlugin} from './rollup-remove-licenses';
3+
import {rollupGlobals} from './rollup-globals';
34

45
// There are no type definitions available for these imports.
56
const rollup = require('rollup');
67
const rollupNodeResolutionPlugin = require('rollup-plugin-node-resolve');
7-
const ROLLUP_GLOBALS = require('./rollup-globals.json');
88

99
export type BundleConfig = {
1010
entry: string;
@@ -17,7 +17,7 @@ export type BundleConfig = {
1717
export function createRollupBundle(config: BundleConfig): Promise<any> {
1818
const bundleOptions = {
1919
context: 'this',
20-
external: Object.keys(ROLLUP_GLOBALS),
20+
external: Object.keys(rollupGlobals),
2121
entry: config.entry,
2222
plugins: [rollupRemoveLicensesPlugin]
2323
};
@@ -29,7 +29,7 @@ export function createRollupBundle(config: BundleConfig): Promise<any> {
2929
banner: buildConfig.licenseBanner,
3030
format: config.format,
3131
dest: config.dest,
32-
globals: ROLLUP_GLOBALS,
32+
globals: rollupGlobals,
3333
sourceMap: true
3434
};
3535

@@ -38,7 +38,7 @@ export function createRollupBundle(config: BundleConfig): Promise<any> {
3838
if (config.format === 'umd') {
3939
bundleOptions.plugins.push(rollupNodeResolutionPlugin());
4040

41-
const external = Object.keys(ROLLUP_GLOBALS);
41+
const external = Object.keys(rollupGlobals);
4242
external.splice(external.indexOf('tslib'), 1);
4343
bundleOptions.external = external;
4444
}

tools/tslint-rules/missingRollupGlobalsRule.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const path = require('path');
22
const Lint = require('tslint');
33

4+
// Since the packaging is based on TypeScript and is only compiled at run-time using ts-node, the
5+
// custom TSLint rule is not able to read the map of rollup globals. Because the custom rules
6+
// for TSLint are written in JavaScript we also need to use ts-node here to read the globals.
7+
require('ts-node').register({
8+
project: path.join(__dirname, '../gulp/tsconfig.json')
9+
});
10+
411
/**
512
* Rule that enforces that the specified external packages have been included in our Rollup config.
613
* Usage: [true, './path/to/rollup/config.json']
@@ -22,7 +29,7 @@ class Walker extends Lint.RuleWalker {
2229
const [configPath, ...whitelist] = options.ruleArguments;
2330

2431
this._configPath = path.resolve(process.cwd(), configPath);
25-
this._config = require(this._configPath);
32+
this._config = require(this._configPath).rollupGlobals;
2633
this._enabled = !whitelist.length || whitelist.some(p => new RegExp(p).test(file.fileName));
2734
}
2835

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
],
3838
"missing-rollup-globals": [
3939
true,
40-
"./tools/package-tools/rollup-globals.json",
40+
"./tools/package-tools/rollup-globals.ts",
4141
"^((?!tools).)*$"
4242
],
4343
"one-line": [

0 commit comments

Comments
 (0)