Skip to content

Commit 1cd823b

Browse files
committed
build: check bazel rollup globals
We currently need to also maintain a list of rollup globals for bazel. Since these globals are impacting the release output we need to check these somehow in order to be sure that we do not accidentally bundle other entry-points into an entry-point.
1 parent 0ada165 commit 1cd823b

File tree

5 files changed

+130
-2
lines changed

5 files changed

+130
-2
lines changed

.circleci/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,16 @@ jobs:
265265
steps:
266266
- *checkout_code
267267
- *restore_cache
268+
- *setup_bazel_ci_config
268269
- *yarn_download
269270
- *yarn_install
271+
- *setup_bazel_binary
272+
273+
- run:
274+
name: Checking rollup globals
275+
command: |
276+
bazel build //:rollup_globals
277+
yarn check-rollup-globals $(bazel info bazel-bin)/rollup_globals.json
270278
271279
- run: ./scripts/circleci/lint-bazel-files.sh
272280
- run: yarn gulp ci:lint

BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
package(default_visibility = ["//visibility:public"])
22

3+
load("//:packages.bzl", "ROLLUP_GLOBALS")
4+
35
exports_files(["LICENSE"])
6+
7+
genrule(
8+
name = "rollup_globals",
9+
outs = ["rollup_globals.json"],
10+
cmd = "echo '%s' > $@" % ROLLUP_GLOBALS,
11+
)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"format:ts": "git-clang-format HEAD $(git diff HEAD --name-only | grep -v \"\\.d\\.ts\")",
3535
"format:bazel": "yarn -s bazel:buildifier --lint=fix --mode=fix",
3636
"format": "yarn -s format:ts && yarn -s format:bazel",
37-
"cherry-pick-patch": "ts-node --project tools/cherry-pick-patch/ tools/cherry-pick-patch/cherry-pick-patch.ts"
37+
"cherry-pick-patch": "ts-node --project tools/cherry-pick-patch/ tools/cherry-pick-patch/cherry-pick-patch.ts",
38+
"check-rollup-globals": "ts-node --project scripts/ scripts/check-rollup-globals.ts"
3839
},
3940
"version": "8.2.0",
4041
"requiredAngularVersion": "^8.0.0 || ^9.0.0-0",

packages.bzl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,39 @@ VERSION_PLACEHOLDER_REPLACEMENTS = {
132132
"0.0.0-NG": ANGULAR_PACKAGE_VERSION,
133133
}
134134

135-
# Base rollup globals for everything in the repo.
135+
# Base rollup globals for everything in the repo. Note that we want to disable
136+
# sorting of the globals as we manually group dict entries.
137+
# buildifier: disable=unsorted-dict-items
136138
ROLLUP_GLOBALS = {
139+
# Framework packages.
140+
"@angular/animations": "ng.animations",
141+
"@angular/common": "ng.common",
142+
"@angular/common/http": "ng.common.http",
143+
"@angular/common/http/testing": "ng.common.http.testing",
144+
"@angular/common/testing": "ng.common.testing",
145+
"@angular/core": "ng.core",
146+
"@angular/core/testing": "ng.core.testing",
147+
"@angular/forms": "ng.forms",
148+
"@angular/platform-browser": "ng.platformBrowser",
149+
"@angular/platform-browser-dynamic": "ng.platformBrowserDynamic",
150+
"@angular/platform-browser-dynamic/testing": "ng.platformBrowserDynamic.testing",
151+
"@angular/platform-browser/animations": "ng.platformBrowser.animations",
152+
"@angular/platform-server": "ng.platformServer",
153+
"@angular/router": "ng.router",
154+
155+
# Primary entry-points in the project.
137156
"@angular/cdk": "ng.cdk",
138157
"@angular/cdk-experimental": "ng.cdkExperimental",
139158
"@angular/google-maps": "ng.googleMaps",
140159
"@angular/material": "ng.material",
141160
"@angular/material-experimental": "ng.materialExperimental",
161+
"@angular/material-moment-adapter": "ng.materialMomentAdapter",
142162
"@angular/youtube-player": "ng.youtubePlayer",
143163

164+
# Special tertiary entry-points for which globals are not generated
165+
# automatically (e.g. since they are treated as their own npm package).
166+
"@angular/cdk/testing": "ng.cdk.testing",
167+
144168
# MDC Web
145169
"@material/animation": "mdc.animation",
146170
"@material/auto-init": "mdc.autoInit",
@@ -172,7 +196,12 @@ ROLLUP_GLOBALS = {
172196
"@material/tab-scroller": "mdc.tabScroller",
173197
"@material/text-field": "mdc.textField",
174198
"@material/top-app-bar": "mdc.topAppBar",
199+
200+
# Third-party libraries.
175201
"moment": "moment",
202+
"protractor": "protractor",
203+
"rxjs": "rxjs",
204+
"rxjs/operators": "rxjs.operators",
176205
"tslib": "tslib",
177206
}
178207

@@ -195,6 +224,15 @@ ROLLUP_GLOBALS.update({
195224
for p in MATERIAL_PACKAGES
196225
})
197226

227+
# Rollup globals for material testing entry-points.
228+
# e.g., {"@angular/material/dialog/testing": "ng.material.dialog.testing"}
229+
# TODO(devversion): consider removing this if we wire up tertiary entry-points in the
230+
# release package. We should not hardcode these tertiary entry-points.
231+
ROLLUP_GLOBALS.update({
232+
"@angular/material/%s/testing" % p: "ng.material.%s.testing" % p
233+
for p in MATERIAL_PACKAGES
234+
})
235+
198236
# Rollup globals for material experimental subpackages, e.g.,
199237
# {"@angular/material-experimental/list": "ng.materialExperimental.list"}
200238
ROLLUP_GLOBALS.update({

scripts/check-rollup-globals.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Script that goes through each source file that will be included in the
3+
* release output and checks if any module imports are not part of the
4+
* specified rollup globals file.
5+
*
6+
* This script is used to validate Bazel rollup globals. We use a genrule to
7+
* convert the Starlark rollup globals dict into a JSON file which then can
8+
* be passed to this script to ensure that the rollup globals are up-to-date.
9+
*/
10+
11+
import {green, red, yellow} from 'chalk';
12+
import {readFileSync} from 'fs';
13+
import * as minimatch from 'minimatch';
14+
import {join, relative} from 'path';
15+
import * as ts from 'typescript';
16+
17+
const projectRoot = join(__dirname, '../');
18+
const args = process.argv.slice(2);
19+
20+
if (args.length !== 1) {
21+
console.error(red('No rollup globals file has been specified.'));
22+
process.exit(1);
23+
}
24+
25+
const rollupGlobals = JSON.parse(readFileSync(args[0], 'utf8'));
26+
const configFile = ts.readJsonConfigFile(join(projectRoot, 'tsconfig.json'), ts.sys.readFile);
27+
const parsedConfig = ts.parseJsonSourceFileConfigFileContent(configFile, ts.sys, projectRoot);
28+
const filesToCheckGlob = [
29+
'src/**/!(*.spec).ts',
30+
'!src/+(a11y-demo|e2e-app|universal-app|dev-app)/**/*.ts',
31+
'!src/**/schematics/**/*.ts',
32+
];
33+
34+
const failures = new Map<string, string[]>();
35+
36+
parsedConfig.fileNames.forEach(fileName => {
37+
const relativeFileName = relative(projectRoot, fileName);
38+
if (!filesToCheckGlob.every(g => minimatch(relativeFileName, g))) {
39+
return;
40+
}
41+
42+
const sourceFile =
43+
ts.createSourceFile(fileName, readFileSync(fileName, 'utf8'), ts.ScriptTarget.Latest, true);
44+
45+
const visitNode = (node: ts.Node) => {
46+
if (ts.isImportDeclaration(node)) {
47+
// Parse out the module name. The first and last characters are the quote marks.
48+
const module = node.moduleSpecifier.getText().slice(1, -1);
49+
const isExternal = !module.startsWith('.') && !module.startsWith('/');
50+
51+
// Check whether the module is external and whether it's in our rollup globals.
52+
if (isExternal && !rollupGlobals[module]) {
53+
failures.set(fileName, (failures.get(fileName) || []).concat(module));
54+
}
55+
}
56+
57+
ts.forEachChild(node, visitNode);
58+
};
59+
60+
ts.forEachChild(sourceFile, visitNode);
61+
});
62+
63+
if (failures.size) {
64+
console.error(red(' ✘ Rollup globals are not up-to-date.'));
65+
console.error();
66+
67+
failures.forEach((missingGlobals, fileName) => {
68+
console.error(yellow(` ⮑ ${fileName}:`));
69+
missingGlobals.forEach(g => console.error(` - ${g}`));
70+
});
71+
} else {
72+
console.info(green(' ✓ Rollup globals are up-to-date.'));
73+
}

0 commit comments

Comments
 (0)