豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 32a04e0

Browse files
committed
feat(bazel): introduce canonical place for esbuild bazel rules
Intoduces a canonical place for ESbuild bazel rules/macros used across the Angular organization. This includes a macro for the default esbuild rule to set proper defaults for e.g. source-maps. Also a macro for building AMD bundles is exposed. This one is useful until non-AMD modules can be consumed in the Karma Bazel rules.
1 parent 912311a commit 32a04e0

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

bazel/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ filegroup(
88
"//bazel/benchmark:files",
99
"//bazel/browsers:files",
1010
"//bazel/constraints:files",
11+
"//bazel/esbuild:files",
1112
"//bazel/integration:files",
1213
"//bazel/remote-execution:files",
1314
],

bazel/esbuild/BUILD.bazel

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+
exports_files(["esbuild-amd-config.mjs"])
4+
5+
# Make source files available for distribution via pkg_npm
6+
filegroup(
7+
name = "files",
8+
srcs = glob(["*"]),
9+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import url from 'url';
2+
import path from 'path';
3+
4+
/** Path to the ESBuild configuration maintained by the user. */
5+
const userConfigExecPath = 'TMPL_CONFIG_PATH';
6+
7+
/** User ESBuild config. Empty if none is loaded. */
8+
let userConfig = {};
9+
10+
if (userConfigExecPath !== '') {
11+
const userConfigPath = path.join(process.cwd(), userConfigExecPath);
12+
const userConfigUrl = url.pathToFileURL(userConfigPath);
13+
14+
// Load the user config, assuming it is set as `default` export. This is
15+
// usually an `export default` statement or a named export named `default`.
16+
userConfig = (await import(userConfigUrl)).default;
17+
}
18+
19+
export default {
20+
...userConfig,
21+
globalName: '__exports',
22+
format: 'iife',
23+
banner: {js: 'define("TMPL_MODULE_NAME", [], function() {'},
24+
footer: {js: 'return __exports;})'},
25+
};

bazel/esbuild/index.bzl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
load("@npm//@bazel/esbuild:index.bzl", _esbuild = "esbuild", _esbuild_config = "esbuild_config")
2+
load("//bazel:expand_template.bzl", "expand_template")
3+
4+
# Re-export of the actual esbuild definitions.
5+
esbuild_config = _esbuild_config
6+
7+
def esbuild(
8+
name,
9+
# Inline source contents to make debugging easier. Contents are inlined by default
10+
# in ESBuild but `@bazel/esbuild` sets the default to `false`. Inlining sources is
11+
# helpful as otherwise developers would need to manually wire up the Bazel execroot
12+
# as workspace in the Chrome devtools.
13+
# https://github.com/bazelbuild/rules_nodejs/blob/c30a26c13d20dac48dc9f220370cb02a317b13f3/packages/esbuild/esbuild.bzl#L333.
14+
sources_content = True,
15+
**kwargs):
16+
_esbuild(
17+
name = name,
18+
sources_content = sources_content,
19+
**kwargs
20+
)
21+
22+
def esbuild_amd(name, entry_point, module_name, testonly = False, config = None, deps = [], **kwargs):
23+
"""Generates an AMD bundle for the specified entry-point with the given AMD module name."""
24+
expand_template(
25+
name = "%s_config" % name,
26+
testonly = testonly,
27+
template = "//bazel/esbuild:esbuild-amd-config.mjs",
28+
output_name = "%s_config.mjs" % name,
29+
substitutions = {
30+
"TMPL_MODULE_NAME": module_name,
31+
"TMPL_CONFIG_PATH": "$(execpath %s)" % config if config else "",
32+
},
33+
data = [config] if config else None,
34+
)
35+
36+
_esbuild_config(
37+
name = "%s_config_lib" % name,
38+
testonly = testonly,
39+
config_file = "%s_config" % name,
40+
# Adds the user configuration and its deps as dependency of the AMD ESBuild config.
41+
# https://github.com/bazelbuild/rules_nodejs/blob/a892caf5a040bae5eeec174a3cf6250f02caf364/packages/esbuild/esbuild_config.bzl#L23.
42+
deps = [config, "%s_deps" % config] if config else None,
43+
)
44+
45+
esbuild(
46+
name = name,
47+
testonly = testonly,
48+
deps = deps,
49+
entry_point = entry_point,
50+
config = "%s_config_lib" % name,
51+
**kwargs
52+
)

0 commit comments

Comments
 (0)