Skip to content

Commit 16159b3

Browse files
devversionjelbourn
authored andcommitted
build: remove hard-coded karma systemjs config (#18025)
Removes the hard-coded Karma SystemJS config in favor of a shared SystemJS config that is used by both the dev-app and the legacy unit tests. The config is generated through Bazel and leverages the entry-point/package configuration we need to main in Bazel. That way we only have one canonical place for package/entry-point configuration (the `config.bzl` files per package).
1 parent 3b242f0 commit 16159b3

12 files changed

+310
-184
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ jobs:
240240
steps:
241241
- *checkout_code
242242
- *restore_cache
243+
- *setup_bazel_ci_config
243244
- *yarn_download
244245
- *yarn_install
245246

@@ -261,6 +262,7 @@ jobs:
261262
steps:
262263
- *checkout_code
263264
- *restore_cache
265+
- *setup_bazel_ci_config
264266
- *yarn_download
265267
- *yarn_install
266268

packages.bzl

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ VERSION_PLACEHOLDER_REPLACEMENTS = {
1313

1414
# List of default Angular library UMD bundles which are not processed by ngcc.
1515
ANGULAR_NO_NGCC_BUNDLES = [
16-
"@npm//:node_modules/@angular/compiler/bundles/compiler.umd.js",
16+
("@angular/compiler", ["compiler.umd.js"]),
1717
]
1818

1919
# List of Angular library UMD bundles which will are processed by ngcc.
@@ -29,20 +29,39 @@ ANGULAR_NGCC_BUNDLES = [
2929
("@angular/router", ["router.umd.js"]),
3030
]
3131

32-
ANGULAR_LIBRARY_VIEW_ENGINE_UMDS = ANGULAR_NO_NGCC_BUNDLES + [
33-
"@npm//:node_modules/%s/bundles/%s" % (pkgName, bundleName)
34-
for pkgName, bundleNames in ANGULAR_NGCC_BUNDLES
35-
for bundleName in bundleNames
36-
]
37-
ANGULAR_LIBRARY_IVY_UMDS = ANGULAR_NO_NGCC_BUNDLES + [
38-
"@npm//:node_modules/%s/__ivy_ngcc__/bundles/%s" % (pkgName, bundleName)
39-
for pkgName, bundleNames in ANGULAR_NGCC_BUNDLES
40-
for bundleName in bundleNames
41-
]
32+
"""
33+
Gets a dictionary of all packages and their bundle names.
34+
"""
35+
36+
def getFrameworkPackageBundles():
37+
res = {}
38+
for pkgName, bundleNames in ANGULAR_NGCC_BUNDLES + ANGULAR_NO_NGCC_BUNDLES:
39+
res[pkgName] = res.get(pkgName, []) + bundleNames
40+
return res
41+
42+
"""
43+
Gets a list of labels which resolve to the UMD bundles of the given packages.
44+
"""
45+
46+
def getUmdFilePaths(packages, ngcc_artifacts):
47+
tmpl = "@npm//:node_modules/%s" + ("/__ivy_ngcc__" if ngcc_artifacts else "") + "/bundles/%s"
48+
return [
49+
tmpl % (pkgName, bundleName)
50+
for pkgName, bundleNames in packages
51+
for bundleName in bundleNames
52+
]
53+
54+
ANGULAR_PACKAGE_BUNDLES = getFrameworkPackageBundles()
55+
56+
ANGULAR_LIBRARY_VIEW_ENGINE_UMDS = getUmdFilePaths(ANGULAR_NO_NGCC_BUNDLES, False) + \
57+
getUmdFilePaths(ANGULAR_NGCC_BUNDLES, False)
58+
59+
ANGULAR_LIBRARY_IVY_UMDS = getUmdFilePaths(ANGULAR_NO_NGCC_BUNDLES, False) + \
60+
getUmdFilePaths(ANGULAR_NGCC_BUNDLES, True)
4261

4362
"""
4463
Gets the list of targets for the Angular library UMD bundles. Conditionally
45-
switches between View Engine or Ivy UMD bundles based on the
64+
switches between View Engine or Ivy UMD bundles based on the
4665
"--config={ivy,view-engine}" flag.
4766
"""
4867

src/dev-app/BUILD.bazel

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

33
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")
44
load("@build_bazel_rules_nodejs//internal/common:devmode_js_sources.bzl", "devmode_js_sources")
5-
load("//src/cdk:config.bzl", "CDK_ENTRYPOINTS")
6-
load("//src/cdk-experimental:config.bzl", "CDK_EXPERIMENTAL_ENTRYPOINTS")
7-
load("//src/material:config.bzl", "MATERIAL_ENTRYPOINTS")
8-
load("//src/material-experimental:config.bzl", "MATERIAL_EXPERIMENTAL_ENTRYPOINTS")
5+
load("//tools:create-system-config.bzl", "create_system_config")
96
load("//tools:defaults.bzl", "ng_module", "sass_binary")
10-
load("//tools/bazel:expand_template.bzl", "expand_template")
117
load("//tools/dev-server:index.bzl", "dev_server")
128

139
ng_module(
@@ -103,17 +99,9 @@ sass_binary(
10399
],
104100
)
105101

106-
expand_template(
102+
create_system_config(
107103
name = "system-config",
108-
configuration_env_vars = ["angular_ivy_enabled"],
109104
output_name = "system-config.js",
110-
substitutions = {
111-
"$CDK_ENTRYPOINTS_TMPL": str(CDK_ENTRYPOINTS),
112-
"$CDK_EXPERIMENTAL_ENTRYPOINTS_TMPL": str(CDK_EXPERIMENTAL_ENTRYPOINTS),
113-
"$MATERIAL_ENTRYPOINTS_TMPL": str(MATERIAL_ENTRYPOINTS),
114-
"$MATERIAL_EXPERIMENTAL_ENTRYPOINTS_TMPL": str(MATERIAL_EXPERIMENTAL_ENTRYPOINTS),
115-
},
116-
template = "system-config-tmpl.js",
117105
)
118106

119107
# File group for all static files which are needed to serve the dev-app. These files are
@@ -124,10 +112,10 @@ filegroup(
124112
srcs = [
125113
"favicon.ico",
126114
"index.html",
127-
"system-rxjs-operators.js",
128115
":system-config",
129116
":theme",
130117
"//src/dev-app/icon:icon_demo_assets",
118+
"//tools:system-rxjs-operators.js",
131119
"@npm//:node_modules/@material/animation/dist/mdc.animation.js",
132120
"@npm//:node_modules/@material/auto-init/dist/mdc.autoInit.js",
133121
"@npm//:node_modules/@material/base/dist/mdc.base.js",

src/dev-app/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
<script src="https://www.youtube.com/iframe_api"></script>
3333
<script src="https://maps.googleapis.com/maps/api/js"></script>
3434
<script>
35+
System.config({
36+
map: {
37+
// Maps imports where the AMD module names start with workspace name (commonly done in Bazel).
38+
// This is needed for compatibility with dynamic runfile resolution of the devserver and the
39+
// static runfile resolution done in the "pkg_web" rule. In the built web package, the output
40+
// tree artifact serves as workspace root and root of the current dev-app Bazel package.
41+
'angular_material': '',
42+
'angular_material/src/dev-app': '',
43+
}
44+
});
3545
System.import('main').catch(console.error.bind(console));
3646
</script>
3747
</html>

src/dev-app/system-config-tmpl.js

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

test/BUILD.bazel

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

3+
load("//tools:create-system-config.bzl", "create_system_config")
34
load("//tools:defaults.bzl", "ts_library")
45

56
exports_files(["bazel-karma-local-config.js"])
@@ -19,3 +20,14 @@ ts_library(
1920
"@npm//@types/jasmine",
2021
],
2122
)
23+
24+
# Creates a SystemJS configuration file that can be used for the Karma legacy
25+
# unit tests. This allows us to only have one canonical SystemJS configuration
26+
# file, and we don't need to repeat package/entry-point configurations.
27+
create_system_config(
28+
name = "system-config",
29+
base_url = "base/",
30+
node_modules_base_path = "node_modules/",
31+
output_name = "system-config.js",
32+
packages_dir = "dist/packages",
33+
)

test/karma.conf.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ module.exports = config => {
5050
{pattern: 'node_modules/@angular/**/*', included: false, watched: false},
5151
{pattern: 'node_modules/rxjs/**/*', included: false, watched: false},
5252

53-
{pattern: 'test/karma-system-config.js', included: true, watched: false},
53+
// The Karma system configuration is built by Bazel. The built System config
54+
// is copied into the "dist/" folder so that the Karma config can use it.
55+
{pattern: 'dist/karma-system-config.js', included: true, watched: false},
5456
{pattern: 'test/karma-test-shim.js', included: true, watched: false},
5557

58+
// Needed for exposing the RxJS operators through the RxJS UMD bundle. This
59+
// is done for performance reasons since fetching individual files is slow.
60+
{pattern: 'tools/system-rxjs-operators.js', included: false, watched: false},
61+
5662
// Include a Material theme in the test suite. Also include the MDC theme as
5763
// karma runs tests for the MDC prototype components as well.
5864
{

tools/BUILD.bazel

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

3+
exports_files([
4+
"system-config-tmpl.js",
5+
"system-rxjs-operators.js",
6+
])
7+
38
# Workaround for https://github.com/bazelbuild/bazel-toolchains/issues/356. We need the
49
# "SYS_ADMIN" capability in order to run browsers with sandbox enabled.
510
platform(

tools/create-system-config.bzl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
load("//:packages.bzl", "ANGULAR_PACKAGE_BUNDLES")
2+
load("//src/cdk:config.bzl", "CDK_ENTRYPOINTS")
3+
load("//src/cdk-experimental:config.bzl", "CDK_EXPERIMENTAL_ENTRYPOINTS")
4+
load("//src/material:config.bzl", "MATERIAL_ENTRYPOINTS", "MATERIAL_TESTING_ENTRYPOINTS")
5+
load("//src/material-experimental:config.bzl", "MATERIAL_EXPERIMENTAL_ENTRYPOINTS")
6+
load("//tools/bazel:expand_template.bzl", "expand_template")
7+
8+
"""
9+
Macro that builds a SystemJS configuration for all packages and entry-points
10+
configured in "config.bzl" files of the workspace. The SystemJS configuration
11+
can be used in the dev-app and for building the legacy unit tests SystemJS config.
12+
"""
13+
14+
def create_system_config(
15+
name,
16+
output_name,
17+
# In Bazel the package output follows the same folder structure as the source
18+
# code. This attribute makes the packages directory configurable since in the
19+
# legacy output, the package output is located in "dist/packages".
20+
packages_dir = "src",
21+
# In Bazel, the node modules can be resolved without having to specify the
22+
# path to the "node_modules" folder. In the legacy tests, this is not the case.
23+
node_modules_base_path = "",
24+
# In Bazel, files can be resolved without having to use Karma's default "base/"
25+
# directory. For the legacy tests this needs to be configurable for now.
26+
base_url = ""):
27+
expand_template(
28+
name = name,
29+
output_name = output_name,
30+
configuration_env_vars = ["angular_ivy_enabled"],
31+
substitutions = {
32+
"$ANGULAR_PACKAGE_BUNDLES": str(ANGULAR_PACKAGE_BUNDLES),
33+
"$BASE_URL": base_url,
34+
"$CDK_ENTRYPOINTS_TMPL": str(CDK_ENTRYPOINTS),
35+
"$CDK_EXPERIMENTAL_ENTRYPOINTS_TMPL": str(CDK_EXPERIMENTAL_ENTRYPOINTS),
36+
"$MATERIAL_ENTRYPOINTS_TMPL": str(MATERIAL_ENTRYPOINTS + MATERIAL_TESTING_ENTRYPOINTS),
37+
"$MATERIAL_EXPERIMENTAL_ENTRYPOINTS_TMPL": str(MATERIAL_EXPERIMENTAL_ENTRYPOINTS),
38+
"$NODE_MODULES_BASE_PATH": node_modules_base_path,
39+
"$PACKAGES_DIR": packages_dir,
40+
},
41+
template = "//tools:system-config-tmpl.js",
42+
)

0 commit comments

Comments
 (0)