-
Notifications
You must be signed in to change notification settings - Fork 20
/
defs.bzl
210 lines (182 loc) · 6.26 KB
/
defs.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
Contains rules for building typescript and other source files for the CLI.
"""
load("@npm//:@angular-devkit/architect-cli/package_json.bzl", architect_cli = "bin")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
load("@aspect_rules_js//js:defs.bzl", "js_library")
# NOTE:
# *_DEPS are propagated as deps of the final output
# *_CONFIG are dependencies only of the architect actions and not propagated
# Global dependencies such as common config files, tools
COMMON_CONFIG = [
"//:ng-config",
"//:node_modules/@angular-devkit/build-angular",
"//:node_modules/@angular-devkit/architect-cli",
]
# Common dependencies of Angular CLI applications
APPLICATION_CONFIG = [
":tsconfig.app.json",
":package.json",
]
APPLICATION_DEPS = [
"//:node_modules/@angular/common",
"//:node_modules/@angular/core",
"//:node_modules/@angular/platform-browser",
"//:node_modules/@angular/platform-browser-dynamic",
"//:node_modules/@types/node",
"//:node_modules/rxjs",
"//:node_modules/tslib",
"//:node_modules/zone.js",
]
# Common dependencies of Angular CLI libraries
LIBRARY_CONFIG = [
":tsconfig.lib.json",
":tsconfig.lib.prod.json",
":package.json",
]
LIBRARY_DEPS = [
"//:node_modules/@angular/common",
"//:node_modules/@angular/core",
"//:node_modules/@types/node",
"//:node_modules/rxjs",
"//:node_modules/tslib",
]
# Common dependencies of Angular CLI test suites
TEST_CONFIG = [
":tsconfig.spec.json",
":karma.conf.js",
"//:node_modules/@types/jasmine",
"//:node_modules/karma-chrome-launcher",
"//:node_modules/karma",
"//:node_modules/karma-jasmine",
"//:node_modules/karma-jasmine-html-reporter",
"//:node_modules/karma-coverage",
]
TEST_DEPS = LIBRARY_DEPS + [
"//:node_modules/@angular/compiler",
"//:node_modules/@angular/platform-browser",
"//:node_modules/@angular/platform-browser-dynamic",
"//:node_modules/jasmine-core",
"//:node_modules/zone.js",
]
# JQ expressions to update Angular project output paths from dist/* to projects/*/dist
JQ_DIST_REPLACE_TSCONFIG = ".compilerOptions.paths |= map_values(map(gsub(\"^dist/(?<p>.+)$\"; \"projects/\"+.p+\"/dist\")))"
JQ_DIST_REPLACE_NG_PACKAGE = ".dest = \"dist\""
"""
Prepares Angular config files
"""
def ng_config(name):
if name != "ng-config":
fail("NG config name must be 'ng-config'")
# Root config files used throughout
copy_to_bin(
name = "angular",
srcs = ["angular.json"],
)
# NOTE: project dist directories are under the project dir unlike the Angular CLI default of the root dist folder
jq(
name = "tsconfig",
srcs = ["tsconfig.json"],
filter = JQ_DIST_REPLACE_TSCONFIG,
)
native.filegroup(
name = name,
srcs = [":angular", ":tsconfig"],
)
"""
Sets up Angular targets for building and serving the app
"""
def ng_app(name, project_name = None, deps = [], test_deps = [], **kwargs):
"""
Bazel macro for compiling an NG application project. Creates {name}, test, serve targets.
Args:
name: the rule name
project_name: the Angular CLI project name, to the rule name
deps: dependencies of the library
test_deps: additional dependencies for tests
**kwargs: extra args passed to main Angular CLI rules
"""
srcs = native.glob(
["src/**/*"],
exclude = [
"src/**/*.spec.ts",
"src/test.ts",
"dist/",
],
)
test_srcs = native.glob(["src/test.ts", "src/**/*.spec.ts"])
project_name = project_name if project_name else name
architect_cli.architect(
name = name,
chdir = native.package_name(),
args = ["%s:build" % project_name],
out_dirs = ["dist/%s" % project_name],
srcs = srcs + deps + APPLICATION_DEPS + APPLICATION_CONFIG + COMMON_CONFIG,
**kwargs
)
architect_cli.architect_binary(
name = "serve",
chdir = native.package_name(),
args = ["%s:serve" % project_name],
data = srcs + deps + APPLICATION_DEPS + APPLICATION_CONFIG + COMMON_CONFIG,
**kwargs
)
architect_cli.architect_test(
name = "test",
chdir = native.package_name(),
args = ["%s:test" % project_name],
data = srcs + test_srcs + deps + test_deps + TEST_DEPS + TEST_CONFIG + COMMON_CONFIG,
log_level = "debug",
**kwargs
)
def ng_lib(name, project_name = None, deps = [], test_deps = [], **kwargs):
"""
Bazel macro for compiling an NG library project. Creates {name}, test, targets.
Args:
name: the rule name
project_name: the Angular CLI project name, defaults to current directory name
deps: dependencies of the library
test_deps: additional dependencies for tests
**kwargs: extra args passed to main Angular CLI rules
"""
srcs = native.glob(
["src/**/*"],
exclude = [
"src/**/*.spec.ts",
"src/test.ts",
"dist/",
],
)
test_srcs = srcs + native.glob(["src/test.ts", "src/**/*.spec.ts"])
project_name = project_name if project_name else native.package_name().split("/").pop()
# NOTE: dist directories are under the project dir instead of the Angular CLI default of the root dist folder
jq(
name = "ng-package",
srcs = ["ng-package.json"],
filter = JQ_DIST_REPLACE_NG_PACKAGE,
visibility = ["//visibility:private"],
)
architect_cli.architect(
name = "_%s" % name,
chdir = native.package_name(),
args = ["%s:build" % project_name],
out_dirs = ["dist"],
srcs = srcs + deps + LIBRARY_DEPS + LIBRARY_CONFIG + COMMON_CONFIG + [":ng-package"],
visibility = ["//visibility:private"],
**kwargs
)
architect_cli.architect_test(
name = "test",
chdir = native.package_name(),
args = ["%s:test" % project_name, "--no-watch"],
data = test_srcs + deps + test_deps + TEST_DEPS + TEST_CONFIG + COMMON_CONFIG + [":ng-package"],
log_level = "debug",
**kwargs
)
# Output the compiled library and its dependencies
js_library(
name = name,
srcs = [":_%s" % name],
deps = deps + LIBRARY_DEPS,
)