Skip to content

Commit 4a24929

Browse files
committed
build: add tslint rule to verify rollup globals config
Adds a custom tslint rule that ensures that we've added all of the necessary external modules to the Rollup config. This is helpful, because forgetting to add a module will log a warning, but it won't break the build necessarily, which is why we occasionally need PRs like #5930.
1 parent 374aaff commit 4a24929

File tree

3 files changed

+102
-59
lines changed

3 files changed

+102
-59
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"tslib": "tslib",
3+
4+
"@angular/animations": "ng.animations",
5+
"@angular/core": "ng.core",
6+
"@angular/common": "ng.common",
7+
"@angular/forms": "ng.forms",
8+
"@angular/http": "ng.http",
9+
"@angular/router": "ng.router",
10+
"@angular/platform-browser": "ng.platformBrowser",
11+
"@angular/platform-server": "ng.platformServer",
12+
"@angular/platform-browser-dynamic": "ng.platformBrowserDynamic",
13+
"@angular/platform-browser/animations": "ng.platformBrowser.animations",
14+
"@angular/core/testing": "ng.core.testing",
15+
"@angular/common/testing": "ng.common.testing",
16+
"@angular/http/testing": "ng.http.testing",
17+
18+
"@angular/material": "ng.material",
19+
"@angular/cdk": "ng.cdk",
20+
"@angular/cdk/testing": "ng.cdk.testing",
21+
22+
"rxjs/BehaviorSubject": "Rx",
23+
"rxjs/Observable": "Rx",
24+
"rxjs/Subject": "Rx",
25+
"rxjs/Subscription": "Rx",
26+
"rxjs/Observer": "Rx",
27+
"rxjs/Scheduler": "Rx",
28+
"rxjs/observable/combineLatest": "Rx.Observable",
29+
"rxjs/observable/forkJoin": "Rx.Observable",
30+
"rxjs/observable/fromEvent": "Rx.Observable",
31+
"rxjs/observable/merge": "Rx.Observable",
32+
"rxjs/observable/of": "Rx.Observable",
33+
"rxjs/observable/throw": "Rx.Observable",
34+
"rxjs/operator/auditTime": "Rx.Observable.prototype",
35+
"rxjs/operator/catch": "Rx.Observable.prototype",
36+
"rxjs/operator/debounceTime": "Rx.Observable.prototype",
37+
"rxjs/operator/do": "Rx.Observable.prototype",
38+
"rxjs/operator/filter": "Rx.Observable.prototype",
39+
"rxjs/operator/finally": "Rx.Observable.prototype",
40+
"rxjs/operator/first": "Rx.Observable.prototype",
41+
"rxjs/operator/let": "Rx.Observable.prototype",
42+
"rxjs/operator/map": "Rx.Observable.prototype",
43+
"rxjs/operator/share": "Rx.Observable.prototype",
44+
"rxjs/operator/startWith": "Rx.Observable.prototype",
45+
"rxjs/operator/switchMap": "Rx.Observable.prototype",
46+
"rxjs/operator/takeUntil": "Rx.Observable.prototype",
47+
"rxjs/operator/toPromise": "Rx.Observable.prototype",
48+
49+
"rxjs/add/observable/merge": "Rx.Observable",
50+
"rxjs/add/observable/fromEvent": "Rx.Observable",
51+
"rxjs/add/observable/of": "Rx.Observable",
52+
"rxjs/add/observable/interval": "Rx.Observable",
53+
"rxjs/add/operator/startWith": "Rx.Observable.prototype",
54+
"rxjs/add/operator/map": "Rx.Observable.prototype",
55+
"rxjs/add/operator/debounceTime": "Rx.Observable.prototype",
56+
"rxjs/add/operator/distinctUntilChanged": "Rx.Observable.prototype",
57+
"rxjs/add/operator/first": "Rx.Observable.prototype",
58+
"rxjs/add/operator/catch": "Rx.Observable.prototype",
59+
"rxjs/add/operator/switchMap": "Rx.Observable.prototype"
60+
}

tools/package-tools/rollup-helpers.ts

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,7 @@ import {rollupRemoveLicensesPlugin} from './rollup-remove-licenses';
44
// There are no type definitions available for these imports.
55
const rollup = require('rollup');
66
const rollupNodeResolutionPlugin = require('rollup-plugin-node-resolve');
7-
8-
const ROLLUP_GLOBALS = {
9-
// Import tslib rather than having TypeScript output its helpers multiple times.
10-
// See https://github.com/Microsoft/tslib
11-
'tslib': 'tslib',
12-
13-
// Angular dependencies
14-
'@angular/animations': 'ng.animations',
15-
'@angular/core': 'ng.core',
16-
'@angular/common': 'ng.common',
17-
'@angular/forms': 'ng.forms',
18-
'@angular/http': 'ng.http',
19-
'@angular/platform-browser': 'ng.platformBrowser',
20-
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
21-
'@angular/platform-browser/animations': 'ng.platformBrowser.animations',
22-
23-
// Local Angular packages inside of Material.
24-
'@angular/material': 'ng.material',
25-
'@angular/cdk': 'ng.cdk',
26-
27-
// RxJS dependencies
28-
'rxjs/BehaviorSubject': 'Rx',
29-
'rxjs/Observable': 'Rx',
30-
'rxjs/Subject': 'Rx',
31-
'rxjs/Subscription': 'Rx',
32-
'rxjs/observable/combineLatest': 'Rx.Observable',
33-
'rxjs/observable/forkJoin': 'Rx.Observable',
34-
'rxjs/observable/fromEvent': 'Rx.Observable',
35-
'rxjs/observable/merge': 'Rx.Observable',
36-
'rxjs/observable/of': 'Rx.Observable',
37-
'rxjs/observable/throw': 'Rx.Observable',
38-
'rxjs/operator/auditTime': 'Rx.Observable.prototype',
39-
'rxjs/operator/catch': 'Rx.Observable.prototype',
40-
'rxjs/operator/debounceTime': 'Rx.Observable.prototype',
41-
'rxjs/operator/do': 'Rx.Observable.prototype',
42-
'rxjs/operator/filter': 'Rx.Observable.prototype',
43-
'rxjs/operator/finally': 'Rx.Observable.prototype',
44-
'rxjs/operator/first': 'Rx.Observable.prototype',
45-
'rxjs/operator/let': 'Rx.Observable.prototype',
46-
'rxjs/operator/map': 'Rx.Observable.prototype',
47-
'rxjs/operator/share': 'Rx.Observable.prototype',
48-
'rxjs/operator/startWith': 'Rx.Observable.prototype',
49-
'rxjs/operator/switchMap': 'Rx.Observable.prototype',
50-
'rxjs/operator/takeUntil': 'Rx.Observable.prototype',
51-
'rxjs/operator/toPromise': 'Rx.Observable.prototype',
52-
53-
// RxJS imports for the examples package
54-
'rxjs/add/observable/merge': 'Rx.Observable',
55-
'rxjs/add/observable/fromEvent': 'Rx.Observable',
56-
'rxjs/add/observable/of': 'Rx.Observable',
57-
'rxjs/add/observable/interval': 'Rx.Observable',
58-
'rxjs/add/operator/startWith': 'Rx.Observable.prototype',
59-
'rxjs/add/operator/map': 'Rx.Observable.prototype',
60-
'rxjs/add/operator/debounceTime': 'Rx.Observable.prototype',
61-
'rxjs/add/operator/distinctUntilChanged': 'Rx.Observable.prototype',
62-
'rxjs/add/operator/first': 'Rx.Observable.prototype',
63-
'rxjs/add/operator/catch': 'Rx.Observable.prototype',
64-
'rxjs/add/operator/switchMap': 'Rx.Observable.prototype',
65-
};
7+
const ROLLUP_GLOBALS = require('./rollup-globals.json');
668

679
export type BundleConfig = {
6810
entry: string;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const Lint = require('tslint');
2+
3+
/**
4+
* Rule that enforces that the specified external packages have been included in our Rollup config.
5+
* Usage: [true, { config: './path/to/rollup/config.json', scope: ['^rxjs', '^@angular'] }]
6+
*/
7+
class Rule extends Lint.Rules.AbstractRule {
8+
apply(file) {
9+
return this.applyWithWalker(new Walker(file, this.getOptions()));
10+
}
11+
}
12+
13+
class Walker extends Lint.RuleWalker {
14+
constructor(file, options) {
15+
super(...arguments);
16+
17+
const args = options.ruleArguments[0];
18+
19+
if (!args.config) {
20+
throw Error('missing-rollup-externals: The `config` argument has to be specified.');
21+
}
22+
23+
this._scope = (args.scope || []).map(p => new RegExp(p));
24+
this._configPath = args.config;
25+
this._config = this._scope.length ? require(args.config) : {};
26+
}
27+
28+
visitImportDeclaration(node) {
29+
// Parse out the module name. The first and last characters are the quote marks.
30+
const module = node.moduleSpecifier.getText().slice(1, -1);
31+
32+
// Check whether we match any of the scopes while missing the module from the config.
33+
if (this._scope.some(p => p.test(module)) && !this._config[module]) {
34+
this.addFailureAtNode(node, `Module "${module}" is missing from ${this._configPath}.`);
35+
}
36+
37+
super.visitImportDeclaration(node);
38+
}
39+
}
40+
41+
exports.Rule = Rule;

0 commit comments

Comments
 (0)