Skip to content

Commit

Permalink
build: add tslint rule to verify rollup globals config
Browse files Browse the repository at this point in the history
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 angular#5930.
  • Loading branch information
crisbeto committed Jul 22, 2017
1 parent 374aaff commit 6bf2ff1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 59 deletions.
61 changes: 61 additions & 0 deletions tools/package-tools/rollup-globals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"tslib": "tslib",

"@angular/animations": "ng.animations",
"@angular/core": "ng.core",
"@angular/common": "ng.common",
"@angular/forms": "ng.forms",
"@angular/http": "ng.http",
"@angular/router": "ng.router",
"@angular/platform-browser": "ng.platformBrowser",
"@angular/platform-server": "ng.platformServer",
"@angular/platform-browser-dynamic": "ng.platformBrowserDynamic",
"@angular/platform-browser/animations": "ng.platformBrowser.animations",
"@angular/core/testing": "ng.core.testing",
"@angular/common/testing": "ng.common.testing",
"@angular/http/testing": "ng.http.testing",

"@angular/material": "ng.material",
"@angular/cdk": "ng.cdk",
"@angular/cdk/testing": "ng.cdk.testing",
"@angular/material-examples": "ng.materialExamples",

"rxjs/BehaviorSubject": "Rx",
"rxjs/Observable": "Rx",
"rxjs/Subject": "Rx",
"rxjs/Subscription": "Rx",
"rxjs/Observer": "Rx",
"rxjs/Scheduler": "Rx",
"rxjs/observable/combineLatest": "Rx.Observable",
"rxjs/observable/forkJoin": "Rx.Observable",
"rxjs/observable/fromEvent": "Rx.Observable",
"rxjs/observable/merge": "Rx.Observable",
"rxjs/observable/of": "Rx.Observable",
"rxjs/observable/throw": "Rx.Observable",
"rxjs/operator/auditTime": "Rx.Observable.prototype",
"rxjs/operator/catch": "Rx.Observable.prototype",
"rxjs/operator/debounceTime": "Rx.Observable.prototype",
"rxjs/operator/do": "Rx.Observable.prototype",
"rxjs/operator/filter": "Rx.Observable.prototype",
"rxjs/operator/finally": "Rx.Observable.prototype",
"rxjs/operator/first": "Rx.Observable.prototype",
"rxjs/operator/let": "Rx.Observable.prototype",
"rxjs/operator/map": "Rx.Observable.prototype",
"rxjs/operator/share": "Rx.Observable.prototype",
"rxjs/operator/startWith": "Rx.Observable.prototype",
"rxjs/operator/switchMap": "Rx.Observable.prototype",
"rxjs/operator/takeUntil": "Rx.Observable.prototype",
"rxjs/operator/toPromise": "Rx.Observable.prototype",

"rxjs/add/observable/merge": "Rx.Observable",
"rxjs/add/observable/fromEvent": "Rx.Observable",
"rxjs/add/observable/of": "Rx.Observable",
"rxjs/add/observable/interval": "Rx.Observable",
"rxjs/add/operator/startWith": "Rx.Observable.prototype",
"rxjs/add/operator/map": "Rx.Observable.prototype",
"rxjs/add/operator/debounceTime": "Rx.Observable.prototype",
"rxjs/add/operator/distinctUntilChanged": "Rx.Observable.prototype",
"rxjs/add/operator/first": "Rx.Observable.prototype",
"rxjs/add/operator/catch": "Rx.Observable.prototype",
"rxjs/add/operator/switchMap": "Rx.Observable.prototype"
}
60 changes: 1 addition & 59 deletions tools/package-tools/rollup-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,7 @@ import {rollupRemoveLicensesPlugin} from './rollup-remove-licenses';
// There are no type definitions available for these imports.
const rollup = require('rollup');
const rollupNodeResolutionPlugin = require('rollup-plugin-node-resolve');

const ROLLUP_GLOBALS = {
// Import tslib rather than having TypeScript output its helpers multiple times.
// See https://github.com/Microsoft/tslib
'tslib': 'tslib',

// Angular dependencies
'@angular/animations': 'ng.animations',
'@angular/core': 'ng.core',
'@angular/common': 'ng.common',
'@angular/forms': 'ng.forms',
'@angular/http': 'ng.http',
'@angular/platform-browser': 'ng.platformBrowser',
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
'@angular/platform-browser/animations': 'ng.platformBrowser.animations',

// Local Angular packages inside of Material.
'@angular/material': 'ng.material',
'@angular/cdk': 'ng.cdk',

// RxJS dependencies
'rxjs/BehaviorSubject': 'Rx',
'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx',
'rxjs/Subscription': 'Rx',
'rxjs/observable/combineLatest': 'Rx.Observable',
'rxjs/observable/forkJoin': 'Rx.Observable',
'rxjs/observable/fromEvent': 'Rx.Observable',
'rxjs/observable/merge': 'Rx.Observable',
'rxjs/observable/of': 'Rx.Observable',
'rxjs/observable/throw': 'Rx.Observable',
'rxjs/operator/auditTime': 'Rx.Observable.prototype',
'rxjs/operator/catch': 'Rx.Observable.prototype',
'rxjs/operator/debounceTime': 'Rx.Observable.prototype',
'rxjs/operator/do': 'Rx.Observable.prototype',
'rxjs/operator/filter': 'Rx.Observable.prototype',
'rxjs/operator/finally': 'Rx.Observable.prototype',
'rxjs/operator/first': 'Rx.Observable.prototype',
'rxjs/operator/let': 'Rx.Observable.prototype',
'rxjs/operator/map': 'Rx.Observable.prototype',
'rxjs/operator/share': 'Rx.Observable.prototype',
'rxjs/operator/startWith': 'Rx.Observable.prototype',
'rxjs/operator/switchMap': 'Rx.Observable.prototype',
'rxjs/operator/takeUntil': 'Rx.Observable.prototype',
'rxjs/operator/toPromise': 'Rx.Observable.prototype',

// RxJS imports for the examples package
'rxjs/add/observable/merge': 'Rx.Observable',
'rxjs/add/observable/fromEvent': 'Rx.Observable',
'rxjs/add/observable/of': 'Rx.Observable',
'rxjs/add/observable/interval': 'Rx.Observable',
'rxjs/add/operator/startWith': 'Rx.Observable.prototype',
'rxjs/add/operator/map': 'Rx.Observable.prototype',
'rxjs/add/operator/debounceTime': 'Rx.Observable.prototype',
'rxjs/add/operator/distinctUntilChanged': 'Rx.Observable.prototype',
'rxjs/add/operator/first': 'Rx.Observable.prototype',
'rxjs/add/operator/catch': 'Rx.Observable.prototype',
'rxjs/add/operator/switchMap': 'Rx.Observable.prototype',
};
const ROLLUP_GLOBALS = require('./rollup-globals.json');

export type BundleConfig = {
entry: string;
Expand Down
40 changes: 40 additions & 0 deletions tools/tslint-rules/missingRollupGlobalsRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const path = require('path');
const Lint = require('tslint');

/**
* Rule that enforces that the specified external packages have been included in our Rollup config.
* Usage: [true, './path/to/rollup/config.json']
*/
class Rule extends Lint.Rules.AbstractRule {
apply(file) {
return this.applyWithWalker(new Walker(file, this.getOptions()));
}
}

class Walker extends Lint.RuleWalker {
constructor(file, options) {
super(...arguments);

if (!options.ruleArguments.length) {
throw Error('missing-rollup-globals: The Rollup config path has to be specified.');
}

this._configPath = path.resolve(process.cwd(), options.ruleArguments[0]);
this._config = require(this._configPath);
}

visitImportDeclaration(node) {
// Parse out the module name. The first and last characters are the quote marks.
const module = node.moduleSpecifier.getText().slice(1, -1);
const isExternal = !module.startsWith('.') && !module.startsWith('/');

// Check whether the module is external and whether it's in our config.
if (isExternal && !this._config[module]) {
this.addFailureAtNode(node, `Module "${module}" is missing from file ${this._configPath}.`);
}

super.visitImportDeclaration(node);
}
}

exports.Rule = Rule;
4 changes: 4 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"src/lib",
"src/cdk"
],
"missing-rollup-globals": [
true,
"./tools/package-tools/rollup-globals.json"
],
"one-line": [
true,
"check-catch",
Expand Down

0 comments on commit 6bf2ff1

Please sign in to comment.