Skip to content

Commit

Permalink
build: migrate @angular/ssr tests to rules_js
Browse files Browse the repository at this point in the history
Migrates the SSR tesst to run natively via `rules_js`. Notably, we still
need the bundling in between as the tests and SSR plain code is not
valid ESM technically; due to lack of extensions.

We'll need to revisit this in the future, or at the very least come up
with a `rules_js`-variant of the `spec_bundle`; but for this is
sufficient and unblocks other packages.
  • Loading branch information
devversion committed Jan 28, 2025
1 parent fece30a commit e2aa475
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=-1406867100
package.json=688000741
packages/angular/build/package.json=1789788715
packages/angular/cli/package.json=349838588
packages/angular/pwa/package.json=-1352285148
packages/angular/ssr/package.json=120782115
packages/angular_devkit/architect/package.json=-1496633956
packages/angular_devkit/architect_cli/package.json=1551210941
packages/angular_devkit/build_angular/package.json=-1437596637
packages/angular_devkit/build_webpack/package.json=373950017
packages/angular_devkit/core/package.json=339935828
packages/angular_devkit/schematics/package.json=673943597
packages/angular_devkit/schematics_cli/package.json=-356386813
packages/ngtools/webpack/package.json=-942726894
packages/schematics/angular/package.json=251715148
pnpm-lock.yaml=604009520
pnpm-workspace.yaml=1732591250
pnpm-lock.yaml=319177102
pnpm-workspace.yaml=-1635877240
yarn.lock=969972397
22 changes: 16 additions & 6 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,28 @@ rules_js_tsconfig(
)

rules_js_tsconfig(
name = "build-tsconfig-angular",
src = "tsconfig-build-ng.json",
name = "test-tsconfig",
src = "tsconfig-test.json",
deps = [
"tsconfig.json",
"//:node_modules/@types/jasmine",
"//:node_modules/@types/node",
],
)

rules_js_tsconfig(
name = "test-tsconfig",
src = "tsconfig-test.json",
name = "build-tsconfig-esm",
src = "tsconfig-build-esm.json",
deps = [
"tsconfig.json",
],
)

rules_js_tsconfig(
name = "test-tsconfig-esm",
src = "tsconfig-test-esm.json",
deps = [
":build-tsconfig-esm",
"//:node_modules/@types/jasmine",
"//:node_modules/@types/node",
],
Expand Down Expand Up @@ -92,8 +102,8 @@ config_setting(
)

ts_config(
name = "tsconfig-build-ng",
src = "tsconfig-build-ng.json",
name = "tsconfig-build-esm",
src = "tsconfig-build-esm.json",
deps = [
":tsconfig.json",
],
Expand Down
4 changes: 3 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,13 @@ npm_translate_lock(
data = [
"//:package.json",
"//:pnpm-workspace.yaml",
"//packages/angular/build:package.json",
"//packages/angular/cli:package.json",
"//packages/angular/pwa:package.json",
"//packages/angular/ssr:package.json",
"//packages/angular_devkit/architect:package.json",
"//packages/angular_devkit/architect_cli:package.json",
"//packages/angular_devkit/build_angular:package.json",
"//packages/angular_devkit/build_webpack:package.json",
"//packages/angular_devkit/core:package.json",
"//packages/angular_devkit/schematics:package.json",
Expand Down Expand Up @@ -245,5 +248,4 @@ http_archive(

load("@aspect_rules_jasmine//jasmine:dependencies.bzl", "rules_jasmine_dependencies")

# Fetch dependencies which users need as well
rules_jasmine_dependencies()
23 changes: 19 additions & 4 deletions packages/angular/ssr/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//@angular/build-tooling/bazel/api-golden:index.bzl", "api_golden_test_npm_package")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//tools:defaults.bzl", "ng_package")
load("//tools:interop.bzl", "ts_project")
load("//tools:defaults2.bzl", "ts_project")

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

Expand All @@ -25,7 +26,7 @@ ts_project(
],
module_name = "@angular/ssr",
source_map = True,
tsconfig = "//:build-tsconfig-angular",
tsconfig = "//:build-tsconfig-esm",
deps = [
"//:node_modules/@angular/common",
"//:node_modules/@angular/core",
Expand All @@ -37,7 +38,7 @@ ts_project(
)

ng_package(
name = "npm_package",
name = "angular_package",
package_name = "@angular/ssr",
srcs = [
":package.json",
Expand All @@ -51,7 +52,6 @@ ng_package(
nested_packages = [
"//packages/angular/ssr/schematics:npm_package",
],
tags = ["release-package"],
deps = [
":ssr",
"//packages/angular/ssr/node",
Expand All @@ -67,6 +67,21 @@ pkg_tar(
tags = ["manual"],
)

# TODO: Replace when `ng_package` creates a valid `rules_js`-compliant npm package.
npm_package(
name = "npm_package",
srcs = [":angular_package"],
replace_prefixes = {
"angular_package/": "",
},
tags = ["release-package"],
)

alias(
name = "pkg",
actual = ":npm_package",
)

api_golden_test_npm_package(
name = "ssr_api",
data = [
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/ssr/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ts_project(
],
module_name = "@angular/ssr/node",
source_map = True,
tsconfig = "//:build-tsconfig-angular",
tsconfig = "//:build-tsconfig-esm",
deps = [
"//:node_modules/@angular/core",
"//:node_modules/@angular/platform-server",
Expand Down
1 change: 1 addition & 0 deletions packages/angular/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@angular/ssr",
"version": "0.0.0-PLACEHOLDER",
"description": "Angular server side rendering utilities",
"type": "module",
"license": "MIT",
"homepage": "https://github.com/angular/angular-cli",
"keywords": [
Expand Down
12 changes: 8 additions & 4 deletions packages/angular/ssr/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
load("@npm//@angular/build-tooling/bazel/spec-bundling:index.bzl", "spec_bundle")
load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test")
load("//tools:interop.bzl", "ts_project")
load("//tools:defaults2.bzl", "jasmine_test", "ts_project")

ts_project(
name = "unit_test_lib",
testonly = True,
srcs = glob(
include = ["**/*.ts"],
),
tsconfig = "//:test-tsconfig-esm",
deps = [
"//:node_modules/@angular/common",
"//:node_modules/@angular/compiler",
Expand All @@ -19,6 +19,8 @@ ts_project(
],
)

# TODO: Replace this with a `rules_js` variant, or make the SSR package
# native ESM compatible! (import extensions etc.)
spec_bundle(
name = "esm_tests_bundled",
downlevel_async_await = False,
Expand All @@ -29,7 +31,9 @@ spec_bundle(
],
)

jasmine_node_test(
jasmine_test(
name = "test",
deps = [":esm_tests_bundled"],
data = [
":esm_tests_bundled",
],
)
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ packages:
- packages/angular_devkit/schematics_cli
- packages/angular/cli
- packages/angular/pwa
- packages/angular/ssr
- packages/schematics/angular
- packages/ngtools/webpack
5 changes: 3 additions & 2 deletions tools/bazel/npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def npm_package(
stamp_files = [],
pkg_json = "package.json",
extra_substitutions = {},
replace_prefixes = {},
**kwargs):
if name != "pkg":
fail("Expected npm_package to be named `pkg`. " +
Expand Down Expand Up @@ -96,12 +97,12 @@ def npm_package(
visibility = visibility,
# Note: Order matters here! Last file takes precedence after replaced prefixes.
srcs = deps + stamp_targets + [":final_package_json"],
replace_prefixes = {
replace_prefixes = dict({
"substituted_final/": "",
"substituted_with_tars/": "",
"substituted_with_snapshot_repos/": "",
"substituted/": "",
},
}, **replace_prefixes),
allow_overwrites = True,
**kwargs
)
Expand Down
8 changes: 5 additions & 3 deletions tools/defaults2.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def ts_project(**kwargs):
def npm_package(**kwargs):
_npm_package(**kwargs)

def jasmine_test(data = [], **kwargs):
def jasmine_test(data = [], args = [], **kwargs):
# Create relative path to root, from current package dir. Necessary as
# we change the `chdir` below to the package directory.
relative_to_root = "/".join([".."] * len(native.package_name().split("/")))
Expand All @@ -18,8 +18,10 @@ def jasmine_test(data = [], **kwargs):
chdir = native.package_name(),
args = [
"--require=%s/node_modules/source-map-support/register.js" % relative_to_root,
"**/*.js",
],
"**/*spec.js",
"**/*spec.mjs",
"**/*spec.cjs",
] + args,
data = data + ["//:node_modules/source-map-support"],
**kwargs
)
File renamed without changes.
14 changes: 14 additions & 0 deletions tsconfig-test-esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Root tsconfig file for use building all Angular packages. Note there is no rootDir
* and therefore any tsconfig in the package directory will need to define its own
* rootDir.
*/
{
"extends": "./tsconfig-build-esm.json",
"compilerOptions": {
"sourceMap": false,
"inlineSourceMap": true,
"inlineSources": true,
"types": ["node", "jasmine"]
}
}

0 comments on commit e2aa475

Please sign in to comment.