Skip to content

Commit 2977ec7

Browse files
devversionzarend
authored andcommitted
build: run esbuild as build step before running browser or e2e tests
Similar to the reason why we switched the dev-app and e2e-app to use ESBuild, we will use ESBuild as a step in between for running browser Karma tests, or running e2e protractor/selenium-webdriver tests. This allows us to incorporate the framework linked ESM bundles and helps working around the change that no UMD bundles are available for APF v13 packages. Running ESBuild as a build step in between seems non-significant as on the other hand the module resolution in the browser for Karma seemed noticeable slower (due to it loading hundreds of files individually); and also the Karma browser setup required more manual maintenance (through RequireJS configurations).
1 parent 482d183 commit 2977ec7

File tree

16 files changed

+241
-105
lines changed

16 files changed

+241
-105
lines changed

.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ build:release --stamp
5151
build:snapshot-build --workspace_status_command="yarn -s ng-dev release build-env-stamp --mode=snapshot"
5252
build:snapshot-build --stamp
5353

54+
####################################
55+
# Bazel custom flags #
56+
####################################
57+
build --flag_alias=partial_compilation=@npm//@angular/bazel/src:partial_compilation
5458

5559
##################################
5660
# Always enable Ivy compilation #

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@
3939
"merge": "ng-dev pr merge",
4040
"approve-api": "node ./scripts/approve-api-golden.js",
4141
"approve-size-tests": "node ./scripts/approve-size-golden.js",
42-
"integration-tests": "bazel test --test_tag_filters=-view-engine-only,-linker-integration-test --build_tests_only -- //integration/... -//integration/size-test/...",
43-
"integration-tests:view-engine": "bazel test --test_tag_filters=view-engine-only --build_tests_only -- //integration/... -//integration/size-test/...",
44-
"integration-tests:partial-ivy": "bazel test --//tools:partial_compilation=True --test_tag_filters=partial-compilation-integration,-firefox --build_tests_only -- //integration/... //src/...",
42+
"integration-tests": "bazel test --test_tag_filters=-linker-integration-test --build_tests_only -- //integration/... -//integration/size-test/...",
43+
"integration-tests:partial-ivy": "bazel test --partial_compilation --test_tag_filters=partial-compilation-integration,-firefox --build_tests_only -- //integration/... //src/...",
4544
"integration-tests:size-test": "bazel test //integration/size-test/...",
4645
"check-mdc-tests": "ts-node --project scripts/tsconfig.json scripts/check-mdc-tests.ts",
4746
"check-mdc-exports": "ts-node --project scripts/tsconfig.json scripts/check-mdc-exports.ts",

src/cdk/a11y/focus-trap/focus-trap.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {PortalModule, CdkPortalOutlet, TemplatePortal} from '@angular/cdk/portal
1111
import {A11yModule, FocusTrap, CdkTrapFocus} from '../index';
1212
import {By} from '@angular/platform-browser';
1313

14-
1514
describe('FocusTrap', () => {
1615

1716
beforeEach(waitForAsync(() => {

src/cdk/testing/BUILD.bazel

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ filegroup(
2929

3030
ng_web_test_suite(
3131
name = "unit_tests",
32-
# We need to load Kagekiri statically since it is not a named AMD module and needs to
33-
# be manually configured through "require.js" which is used by "karma_web_test_suite".
34-
static_files = [
35-
"@npm//kagekiri",
36-
],
3732
deps = [
38-
":require-config.js",
3933
"//src/cdk/testing/tests:unit_test_sources",
4034
],
4135
)

src/cdk/testing/require-config.js

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

src/cdk/testing/tests/webdriver-test.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
load("//tools:defaults.bzl", "jasmine_node_test")
22
load("@io_bazel_rules_webtesting//web:web.bzl", "web_test")
33
load("//tools/server-test:index.bzl", "server_test")
4+
load("//tools/spec-bundling:index.bzl", "spec_bundle")
5+
6+
def webdriver_test(name, deps, tags = [], **kwargs):
7+
spec_bundle(
8+
name = "%s_bundle" % name,
9+
deps = deps,
10+
platform = "node",
11+
external = ["selenium-webdriver"],
12+
)
413

5-
def webdriver_test(name, tags = [], **kwargs):
614
jasmine_node_test(
715
name = "%s_jasmine_test" % name,
816
tags = tags + ["manual"],
17+
deps = ["%s_bundle" % name, "@npm//selenium-webdriver"],
918
**kwargs
1019
)
1120

src/material-date-fns-adapter/BUILD.bazel

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("//tools:defaults.bzl", "ng_module", "ng_package", "ng_test_library", "ng_web_test_suite")
2-
load("//tools/esbuild:index.bzl", "esbuild_amd")
32

43
package(default_visibility = ["//visibility:public"])
54

@@ -33,28 +32,10 @@ ng_test_library(
3332
ng_web_test_suite(
3433
name = "unit_tests",
3534
deps = [
36-
":amd_date_fns",
37-
":amd_date_fns_locales",
3835
":unit_test_sources",
3936
],
4037
)
4138

42-
esbuild_amd(
43-
name = "amd_date_fns",
44-
testonly = True,
45-
entry_point = "@npm//:node_modules/date-fns/esm/index.js",
46-
module_name = "date-fns",
47-
deps = ["@npm//date-fns"],
48-
)
49-
50-
esbuild_amd(
51-
name = "amd_date_fns_locales",
52-
testonly = True,
53-
entry_point = "@npm//:node_modules/date-fns/esm/locale/index.js",
54-
module_name = "date-fns/locale",
55-
deps = ["@npm//date-fns"],
56-
)
57-
5839
ng_package(
5940
name = "npm_package",
6041
srcs = ["package.json"],

src/material-luxon-adapter/BUILD.bazel

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ ng_test_library(
3333

3434
ng_web_test_suite(
3535
name = "unit_tests",
36-
# We need to load Luxon statically since it is not a named AMD module and needs to
37-
# be manually configured through "require.js" which is used by "karma_web_test_suite".
38-
static_files = [
39-
"@npm//luxon",
40-
],
4136
deps = [
42-
":require-config.js",
4337
":unit_test_sources",
4438
],
4539
)

src/material-moment-adapter/BUILD.bazel

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ ng_module(
1616
],
1717
)
1818

19-
ts_config(
20-
name = "tsconfig",
21-
src = "tsconfig-build.json",
22-
deps = ["//src:bazel-tsconfig-build.json"],
23-
)
24-
2519
ng_test_library(
2620
name = "unit_test_sources",
2721
srcs = glob(
@@ -38,13 +32,7 @@ ng_test_library(
3832

3933
ng_web_test_suite(
4034
name = "unit_tests",
41-
# We need to load Moment statically since it is not a named AMD module and needs to
42-
# be manually configured through "require.js" which is used by "karma_web_test_suite".
43-
static_files = [
44-
"@npm//moment",
45-
],
4635
deps = [
47-
":require-config.js",
4836
":unit_test_sources",
4937
],
5038
)

src/material-moment-adapter/adapter/moment-date-adapter.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {DEC, FEB, JAN, MAR} from '../../material/testing';
1313
import {MomentDateModule} from './index';
1414
import {MAT_MOMENT_DATE_ADAPTER_OPTIONS, MomentDateAdapter} from './moment-date-adapter';
1515

16-
import * as moment from 'moment';
16+
import moment from 'moment';
17+
18+
// Import all locales for specs relying on non-US locales.
19+
import 'moment/min/locales';
1720

1821
describe('MomentDateAdapter', () => {
1922
let adapter: MomentDateAdapter;

src/material-moment-adapter/require-config.js

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

tools/BUILD.bazel

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
2-
31
package(default_visibility = ["//visibility:public"])
42

53
exports_files([
64
"system-config-tmpl.js",
7-
"system-rxjs-operators.js",
85
])
96

10-
# Bazel config setting that matches if the partial compilation flag is set to `True`.
7+
# Bazel config setting that matches if the partial compilation flag
8+
# for `@angular/bazel` is set to `True`.
119
config_setting(
1210
name = "partial_compilation_enabled",
1311
flag_values = {
14-
":partial_compilation": "True",
12+
"@npm//@angular/bazel/src:partial_compilation": "True",
1513
},
1614
)
1715

tools/defaults.bzl

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ load("//:packages.bzl", "VERSION_PLACEHOLDER_REPLACEMENTS")
1111
load("//:pkg-externals.bzl", "PKG_EXTERNALS")
1212
load("//tools/markdown-to-html:index.bzl", _markdown_to_html = "markdown_to_html")
1313
load("//tools/linker-process:index.bzl", "linker_process")
14+
load("//tools/spec-bundling:index.bzl", "spec_bundle")
1415

1516
_DEFAULT_TSCONFIG_BUILD = "//src:bazel-tsconfig-build.json"
1617
_DEFAULT_TSCONFIG_TEST = "//src:tsconfig-test"
@@ -244,27 +245,15 @@ def ng_e2e_test_library(deps = [], tsconfig = None, **kwargs):
244245

245246
def karma_web_test_suite(name, **kwargs):
246247
web_test_args = {}
247-
test_deps = ["//tools/rxjs:rxjs_umd_modules"] + kwargs.get("deps", [])
248-
249-
# Note: Ideally we would not add all Angular and MDC UMD files to a test because
250-
# some might be unused. This would require some custom tooling to resolve the
251-
# correct named AMD files from transitive dependencies and is not worth the effort
252-
# given the UMD files being small and most of the packages being used anyway.
253-
# TODO(devversion): reconsider this if `rules_nodejs` can recognize named AMD files.
254-
kwargs["srcs"] = ["@npm//:node_modules/tslib/tslib.js"] + getAngularUmdTargets() + \
255-
MDC_PACKAGE_UMD_BUNDLES.values() + kwargs.get("srcs", [])
248+
test_deps = kwargs.get("deps", [])
249+
256250
kwargs["tags"] = ["partial-compilation-integration"] + kwargs.get("tags", [])
257-
kwargs["deps"] = select({
258-
# Based on whether partial compilation is enabled, use the linker processed dependencies.
259-
"//tools:partial_compilation_enabled": ["%s_linker_processed_deps" % name],
260-
"//conditions:default": test_deps,
261-
})
262-
263-
linker_process(
264-
name = "%s_linker_processed_deps" % name,
265-
srcs = test_deps,
266-
testonly = True,
267-
tags = ["manual"],
251+
kwargs["deps"] = ["%s_bundle" % name]
252+
253+
spec_bundle(
254+
name = "%s_bundle" % name,
255+
deps = test_deps,
256+
platform = "browser",
268257
)
269258

270259
# Set up default browsers if no explicit `browsers` have been specified.
@@ -306,13 +295,36 @@ def karma_web_test_suite(name, **kwargs):
306295
**kwargs
307296
)
308297

309-
def protractor_web_test_suite(**kwargs):
298+
def protractor_web_test_suite(name, deps, **kwargs):
299+
spec_bundle(
300+
name = "%s_bundle" % name,
301+
deps = deps,
302+
platform = "node",
303+
external = ["protractor", "selenium-webdriver"],
304+
)
305+
310306
_protractor_web_test_suite(
307+
name = name,
311308
browsers = ["@npm//@angular/dev-infra-private/bazel/browsers/chromium:chromium"],
309+
deps = ["%s_bundle" % name],
312310
**kwargs
313311
)
314312

315-
def ng_web_test_suite(deps = [], static_css = [], bootstrap = [], exclude_init_script = False, **kwargs):
313+
def ng_web_test_suite(deps = [], static_css = [], exclude_init_script = False, **kwargs):
314+
bootstrap = [
315+
# This matches the ZoneJS bundles used in default CLI projects. See:
316+
# https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/application/files/src/polyfills.ts.template#L58
317+
# https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/application/files/src/test.ts.template#L3
318+
# Note `zone.js/dist/zone.js` is aliased in the CLI to point to the evergreen
319+
# output that does not include legacy patches. See: https://github.com/angular/angular/issues/35157.
320+
# TODO: Consider adding the legacy patches when testing Saucelabs/Browserstack with Bazel.
321+
# CLI loads the legacy patches conditionally for ES5 legacy browsers. See:
322+
# https://github.com/angular/angular-cli/blob/277bad3895cbce6de80aa10a05c349b10d9e09df/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts#L141
323+
"@npm//:node_modules/zone.js/dist/zone-evergreen.js",
324+
"@npm//:node_modules/zone.js/dist/zone-testing.js",
325+
"@npm//:node_modules/reflect-metadata/Reflect.js",
326+
] + kwargs.pop("bootstrap", [])
327+
316328
# Always include a prebuilt theme in the test suite because otherwise tests, which depend on CSS
317329
# that is needed for measuring, will unexpectedly fail. Also always adding a prebuilt theme
318330
# reduces the amount of setup that is needed to create a test suite Bazel target. Note that the
@@ -330,12 +342,12 @@ def ng_web_test_suite(deps = [], static_css = [], bootstrap = [], exclude_init_s
330342
# loads the given CSS file.
331343
for css_label in static_css:
332344
css_id = "static-css-file-%s" % (css_label.replace("/", "_").replace(":", "-"))
333-
deps += [":%s" % css_id]
345+
bootstrap += [":%s" % css_id]
334346

335347
native.genrule(
336348
name = css_id,
337349
srcs = [css_label],
338-
outs = ["%s.js" % css_id],
350+
outs = ["%s.css.js" % css_id],
339351
output_to_bindir = True,
340352
cmd = """
341353
files=($(execpaths %s))
@@ -354,18 +366,6 @@ def ng_web_test_suite(deps = [], static_css = [], bootstrap = [], exclude_init_s
354366
karma_web_test_suite(
355367
# Depend on our custom test initialization script. This needs to be the first dependency.
356368
deps = deps if exclude_init_script else ["//test:angular_test_init"] + deps,
357-
bootstrap = [
358-
# This matches the ZoneJS bundles used in default CLI projects. See:
359-
# https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/application/files/src/polyfills.ts.template#L58
360-
# https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/application/files/src/test.ts.template#L3
361-
# Note `zone.js/dist/zone.js` is aliased in the CLI to point to the evergreen
362-
# output that does not include legacy patches. See: https://github.com/angular/angular/issues/35157.
363-
# TODO: Consider adding the legacy patches when testing Saucelabs/Browserstack with Bazel.
364-
# CLI loads the legacy patches conditionally for ES5 legacy browsers. See:
365-
# https://github.com/angular/angular-cli/blob/277bad3895cbce6de80aa10a05c349b10d9e09df/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts#L141
366-
"@npm//:node_modules/zone.js/dist/zone-evergreen.js",
367-
"@npm//:node_modules/zone.js/dist/zone-testing.js",
368-
"@npm//:node_modules/reflect-metadata/Reflect.js",
369-
] + bootstrap,
369+
bootstrap = bootstrap,
370370
**kwargs
371371
)

tools/spec-bundling/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
exports_files(["esbuild.config-tmpl.mjs"])
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// Note: This needs to be a workspace manifest path as this ESBuild config
10+
// is generated and can end up in arbitrary Bazel packages.
11+
import {
12+
createLinkerEsbuildPlugin
13+
} from 'angular_material/tools/angular/create_linker_esbuild_plugin.mjs';
14+
15+
// Conditionally, based on whether partial compilation is enabled, we run the
16+
// linker on all files part of the test.
17+
const plugins = TMPL_PARTIAL_COMPILATION_ENABLED ?
18+
[await createLinkerEsbuildPlugin(/.*/, /* ensureNoPartialDeclaration */ true)] : []
19+
20+
export default {
21+
// `tslib` sets the `module` condition to resolve to ESM.
22+
conditions: ['es2020', 'es2015', 'module'],
23+
// This ensures that we prioritize ES2020. RxJS would otherwise use the ESM5 output.
24+
mainFields: ['es2020', 'es2015', 'module', 'main'],
25+
// Use the `iife` format for the test entry-point as tests should run immediately.
26+
// For browser tests which are wrapped in an AMD header and footer, this works as well.
27+
format: 'iife',
28+
plugins,
29+
};

0 commit comments

Comments
 (0)