Skip to content

Commit 6bf2ff1

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 6bf2ff1

File tree

4 files changed

+106
-59
lines changed

4 files changed

+106
-59
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
"@angular/material-examples": "ng.materialExamples",
22+
23+
"rxjs/BehaviorSubject": "Rx",
24+
"rxjs/Observable": "Rx",
25+
"rxjs/Subject": "Rx",
26+
"rxjs/Subscription": "Rx",
27+
"rxjs/Observer": "Rx",
28+
"rxjs/Scheduler": "Rx",
29+
"rxjs/observable/combineLatest": "Rx.Observable",
30+
"rxjs/observable/forkJoin": "Rx.Observable",
31+
"rxjs/observable/fromEvent": "Rx.Observable",
32+
"rxjs/observable/merge": "Rx.Observable",
33+
"rxjs/observable/of": "Rx.Observable",
34+
"rxjs/observable/throw": "Rx.Observable",
35+
"rxjs/operator/auditTime": "Rx.Observable.prototype",
36+
"rxjs/operator/catch": "Rx.Observable.prototype",
37+
"rxjs/operator/debounceTime": "Rx.Observable.prototype",
38+
"rxjs/operator/do": "Rx.Observable.prototype",
39+
"rxjs/operator/filter": "Rx.Observable.prototype",
40+
"rxjs/operator/finally": "Rx.Observable.prototype",
41+
"rxjs/operator/first": "Rx.Observable.prototype",
42+
"rxjs/operator/let": "Rx.Observable.prototype",
43+
"rxjs/operator/map": "Rx.Observable.prototype",
44+
"rxjs/operator/share": "Rx.Observable.prototype",
45+
"rxjs/operator/startWith": "Rx.Observable.prototype",
46+
"rxjs/operator/switchMap": "Rx.Observable.prototype",
47+
"rxjs/operator/takeUntil": "Rx.Observable.prototype",
48+
"rxjs/operator/toPromise": "Rx.Observable.prototype",
49+
50+
"rxjs/add/observable/merge": "Rx.Observable",
51+
"rxjs/add/observable/fromEvent": "Rx.Observable",
52+
"rxjs/add/observable/of": "Rx.Observable",
53+
"rxjs/add/observable/interval": "Rx.Observable",
54+
"rxjs/add/operator/startWith": "Rx.Observable.prototype",
55+
"rxjs/add/operator/map": "Rx.Observable.prototype",
56+
"rxjs/add/operator/debounceTime": "Rx.Observable.prototype",
57+
"rxjs/add/operator/distinctUntilChanged": "Rx.Observable.prototype",
58+
"rxjs/add/operator/first": "Rx.Observable.prototype",
59+
"rxjs/add/operator/catch": "Rx.Observable.prototype",
60+
"rxjs/add/operator/switchMap": "Rx.Observable.prototype"
61+
}

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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const path = require('path');
2+
const Lint = require('tslint');
3+
4+
/**
5+
* Rule that enforces that the specified external packages have been included in our Rollup config.
6+
* Usage: [true, './path/to/rollup/config.json']
7+
*/
8+
class Rule extends Lint.Rules.AbstractRule {
9+
apply(file) {
10+
return this.applyWithWalker(new Walker(file, this.getOptions()));
11+
}
12+
}
13+
14+
class Walker extends Lint.RuleWalker {
15+
constructor(file, options) {
16+
super(...arguments);
17+
18+
if (!options.ruleArguments.length) {
19+
throw Error('missing-rollup-globals: The Rollup config path has to be specified.');
20+
}
21+
22+
this._configPath = path.resolve(process.cwd(), options.ruleArguments[0]);
23+
this._config = require(this._configPath);
24+
}
25+
26+
visitImportDeclaration(node) {
27+
// Parse out the module name. The first and last characters are the quote marks.
28+
const module = node.moduleSpecifier.getText().slice(1, -1);
29+
const isExternal = !module.startsWith('.') && !module.startsWith('/');
30+
31+
// Check whether the module is external and whether it's in our config.
32+
if (isExternal && !this._config[module]) {
33+
this.addFailureAtNode(node, `Module "${module}" is missing from file ${this._configPath}.`);
34+
}
35+
36+
super.visitImportDeclaration(node);
37+
}
38+
}
39+
40+
exports.Rule = Rule;

tslint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"src/lib",
3636
"src/cdk"
3737
],
38+
"missing-rollup-globals": [
39+
true,
40+
"./tools/package-tools/rollup-globals.json"
41+
],
3842
"one-line": [
3943
true,
4044
"check-catch",

0 commit comments

Comments
 (0)