Skip to content

Commit 9067fd3

Browse files
devversionmmalerba
authored andcommitted
build: no longer require updating multiple package.json files for bazel (#14391)
* Replaces second "package.json" file with a small workspace that just aliases to the actual dependencies provided in the "@matdeps" workspace. This speeds up the analysis phase and also makes debugging easier. * Also proxies the "ng_package" so that it always includes the proper `README.md` file. Also this means that we can always replace the version placeholders. Similar to `angular/angular`. Similar to angular/angular@68074df. Credits to @gregmagolan for this. We just need a bit more work because we don't overwrite the default `compiler`, `xi18n` and `packager` targets to the actual source files (this is special to Angular because those contain the sources)
1 parent 63be232 commit 9067fd3

File tree

17 files changed

+94
-4303
lines changed

17 files changed

+94
-4303
lines changed

WORKSPACE

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ http_archive(
3535
sha256 = "72b0b4e517f43358f554c125e40e39f67688cd2738a8998b4a266981ed32f403",
3636
)
3737

38+
# We need to create a local repository called "npm" because currently Angular Material
39+
# stores all of it's NPM dependencies in the "@matdeps" repository. This is necessary because
40+
# we don't want to reserve the "npm" repository that is commonly used by downstream projects.
41+
# Since we still need the "npm" repository in order to use the Angular or TypeScript Bazel
42+
# rules, we create a local repository that is just defined in **this** workspace and is not
43+
# being shipped to downstream projects. This can be removed once downstream projects can
44+
# consume Angular Material completely from NPM.
45+
# TODO(devversion): remove once Angular Material can be consumed from NPM with Bazel.
46+
local_repository(
47+
name = "npm",
48+
path = "tools/npm-workspace"
49+
)
50+
3851
# Add sass rules
3952
http_archive(
4053
name = "io_bazel_rules_sass",
@@ -60,8 +73,7 @@ rules_angular_dependencies()
6073
load("@io_bazel_rules_sass//:package.bzl", "rules_sass_dependencies")
6174
rules_sass_dependencies()
6275

63-
load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version", "node_repositories",
64-
"yarn_install")
76+
load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version", "node_repositories")
6577

6678
# The minimum bazel version to use with this repo is 0.18.0
6779
check_bazel_version("0.18.0")
@@ -73,16 +85,6 @@ node_repositories(
7385
yarn_version = "1.12.1",
7486
)
7587

76-
# @npm is temporarily needed to build @rxjs from source since its ts_library
77-
# targets will depend on an @npm workspace by default.
78-
# TODO(gmagolan): remove this once rxjs ships with an named UMD bundle and we
79-
# are no longer building it from source.
80-
yarn_install(
81-
name = "npm",
82-
package_json = "//tools:npm/package.json",
83-
yarn_lock = "//tools:npm/yarn.lock",
84-
)
85-
8688
# Setup TypeScript Bazel workspace
8789
load("@build_bazel_rules_typescript//:defs.bzl", "ts_setup_workspace")
8890
ts_setup_workspace()

src/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load("//tools:defaults.bzl", "ts_library")
55
exports_files([
66
"bazel-tsconfig-build.json",
77
"bazel-tsconfig-test.json",
8+
"README.md",
89
])
910

1011
ts_library(

src/cdk-experimental/BUILD.bazel

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

3-
load("@angular//:index.bzl", "ng_package")
4-
load("//tools:defaults.bzl", "ng_module")
3+
load("//tools:defaults.bzl", "ng_module", "ng_package")
54
load("//:packages.bzl", "CDK_EXPERIMENTAL_PACKAGES", "CDK_EXPERIMENTAL_TARGETS",
6-
"CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
5+
"CDK_TARGETS", "ROLLUP_GLOBALS")
76

87
ng_module(
98
name = "cdk-experimental",
@@ -17,7 +16,6 @@ ng_package(
1716
srcs = ["package.json"],
1817
entry_point = "src/cdk-experimental/public_api.js",
1918
globals = ROLLUP_GLOBALS,
20-
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
2119
deps = CDK_EXPERIMENTAL_TARGETS,
2220
# TODO(devversion): Use the npm package for publishing. Right now this is disabled because
2321
# we build using AOT for serving & testing, but the `ng_package` rule should not include factory

src/cdk/BUILD.bazel

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

3-
load("@angular//:index.bzl", "ng_package")
4-
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
5-
load("//tools:defaults.bzl", "ng_module")
3+
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS")
4+
load("//tools:defaults.bzl", "ng_module", "ng_package")
65

76
# Root "@angular/cdk" entry-point that does not re-export individual entry-points.
87
ng_module(
@@ -20,7 +19,6 @@ ng_package(
2019
srcs = ["package.json"],
2120
entry_point = "src/cdk/public_api.js",
2221
globals = ROLLUP_GLOBALS,
23-
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
2422
deps = CDK_TARGETS,
2523
# TODO(devversion): Use the npm package for publishing. Right now this is disabled because
2624
# we build using AOT for serving & testing, but the `ng_package` rule should not include factory

src/lib/BUILD.bazel

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

3-
load("@angular//:index.bzl", "ng_package")
43
load("//tools:sass_bundle.bzl", "sass_bundle")
5-
load("//tools:defaults.bzl", "ng_module")
4+
load("//tools:defaults.bzl", "ng_module", "ng_package")
65
load("//:packages.bzl", "MATERIAL_PACKAGES", "MATERIAL_TARGETS", "MATERIAL_SCSS_LIBS",
7-
"ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
6+
"ROLLUP_GLOBALS")
87

98
# Root "@angular/material" entry-point.
109
ng_module(
@@ -40,7 +39,6 @@ ng_package(
4039
"//src/lib/prebuilt-themes:purple-green",
4140
],
4241
packages = ["//src/lib/schematics:npm_package"],
43-
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
4442
deps = MATERIAL_TARGETS,
4543
# TODO(devversion): Use the npm package for publishing. Right now this is disabled because
4644
# we build using AOT for serving & testing, but the `ng_package` rule should not include factory

src/material-examples/BUILD.bazel

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

3-
load("@angular//:index.bzl", "ng_package")
43
load("//:packages.bzl", "CDK_TARGETS", "MATERIAL_TARGETS", "ROLLUP_GLOBALS")
5-
load("//tools:defaults.bzl", "ng_module")
4+
load("//tools:defaults.bzl", "ng_module", "ng_package")
65
load("//tools/highlight-files:index.bzl", "highlight_files")
76

87
ng_module(

src/material-experimental/BUILD.bazel

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

3-
load("@angular//:index.bzl", "ng_package")
4-
load("//tools:defaults.bzl", "ng_module")
5-
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
3+
load("//tools:defaults.bzl", "ng_module", "ng_package")
4+
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS")
65

76
ng_module(
87
name = "material-experimental",
@@ -16,7 +15,6 @@ ng_package(
1615
srcs = ["package.json"],
1716
entry_point = "src/material-experimental/public_api.js",
1817
globals = ROLLUP_GLOBALS,
19-
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
2018
deps = [":material-experimental"],
2119
# TODO(devversion): re-enable once we have set up the proper compiler for the ng_package
2220
tags = ["manual"],

src/material-moment-adapter/BUILD.bazel

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

3-
load("@angular//:index.bzl", "ng_package")
4-
load("//tools:defaults.bzl", "ng_module")
5-
load("//:packages.bzl", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
3+
load("//tools:defaults.bzl", "ng_module", "ng_package")
4+
load("//:packages.bzl", "ROLLUP_GLOBALS")
65

76
ng_module(
87
name = "material-moment-adapter",
@@ -25,7 +24,6 @@ ng_package(
2524
srcs = ["package.json"],
2625
entry_point = "src/material-moment-adapter/public_api.js",
2726
globals = ROLLUP_GLOBALS,
28-
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
2927
deps = [":material-moment-adapter"],
3028
# TODO(devversion): re-enable once we have set up the proper compiler for the ng_package
3129
tags = ["manual"],

tools/defaults.bzl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Re-export of Bazel rules with repository-wide defaults
22

3-
load("@angular//:index.bzl", _ng_module = "ng_module")
3+
load("@angular//:index.bzl", _ng_module = "ng_module", _ng_package = "ng_package")
44
load("@build_bazel_rules_nodejs//:defs.bzl", _jasmine_node_test = "jasmine_node_test")
55
load("@build_bazel_rules_typescript//:defs.bzl", _ts_library = "ts_library",
66
_ts_web_test_suite = "ts_web_test_suite")
77
load("//tools/markdown-to-html:index.bzl", _markdown_to_html = "markdown_to_html")
8+
load("//:packages.bzl", "VERSION_PLACEHOLDER_REPLACEMENTS")
89

910
_DEFAULT_TSCONFIG_BUILD = "//src:bazel-tsconfig-build.json"
1011
_DEFAULT_TSCONFIG_TEST = "//src:bazel-tsconfig-test.json"
@@ -55,6 +56,19 @@ def ng_module(deps = [], tsconfig = None, testonly = False, **kwargs):
5556
**kwargs
5657
)
5758

59+
def ng_package(name, readme_md = None, **kwargs):
60+
# If no readme file has been specified explicitly, use the default readme for
61+
# release packages from "src/README.md".
62+
if not readme_md:
63+
readme_md = "//src:README.md"
64+
65+
_ng_package(
66+
name = name,
67+
readme_md = readme_md,
68+
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
69+
**kwargs
70+
)
71+
5872
def jasmine_node_test(deps = [], **kwargs):
5973
local_deps = [
6074
# Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/344
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
# Create an alias so that "@npm//@angular/bazel" is a valid Bazel target that refers to the
4+
# actual Bazel rules code from NPM. This is required by the "@angular//:@angular/bazel/ngc-wrapped
5+
# target. See: https://github.com/angular/angular/blob/master/BUILD.bazel#L45
6+
alias(
7+
name = "bazel",
8+
actual = "@matdeps//@angular/bazel",
9+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
# Workaround that ensures that our build targets using the Angular Bazel rules
4+
# can find the required binaries. This is a temporary workaround and can be removed
5+
# once we use the "npm" workspace for our own dependencies and consume Angular
6+
# completely from NPM.
7+
8+
alias(
9+
name = "xi18n",
10+
actual = "@matdeps//@angular/bazel/bin:xi18n",
11+
)
12+
13+
alias(
14+
name = "packager",
15+
actual = "@matdeps//@angular/bazel/bin:packager",
16+
)
17+
18+
alias(
19+
name = "ngc-wrapped",
20+
actual = "@matdeps//@angular/bazel/bin:ngc-wrapped",
21+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
# Workaround that ensures that our build targets using the Bazel TypeScript web test
4+
# rules can find the required source files. This is a temporary workaround and can be
5+
# removed once we use the "npm" workspace for our own dependencies and consume Angular
6+
# completely from NPM.
7+
8+
alias(
9+
name = "karma",
10+
actual = "@matdeps//@bazel/karma/bin:karma",
11+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
# Create an alias so that "@npm//@bazel/typescript" is a valid Bazel target that refers to the
4+
# actual TypeScript rules output from NPM. This is required because by default the TypeScript
5+
# rules expect the "tsc-wrapped" code to be available in the "npm" workspace.
6+
# See: https://github.com/bazelbuild/rules_typescript/blob/master/BUILD.bazel#L66
7+
alias(
8+
name = "typescript",
9+
actual = "@matdeps//@bazel/typescript",
10+
)

tools/npm-workspace/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Empty build file that marks this directory as a Bazel package.

tools/npm-workspace/WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "npm")

tools/npm/package.json

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

0 commit comments

Comments
 (0)