Skip to content

Commit

Permalink
feat(bazel): introduce canonical place for esbuild bazel rules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
devversion committed Nov 16, 2021
1 parent 912311a commit 32a04e0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ filegroup(
"//bazel/benchmark:files",
"//bazel/browsers:files",
"//bazel/constraints:files",
"//bazel/esbuild:files",
"//bazel/integration:files",
"//bazel/remote-execution:files",
],
Expand Down
9 changes: 9 additions & 0 deletions bazel/esbuild/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package(default_visibility = ["//visibility:public"])

exports_files(["esbuild-amd-config.mjs"])

# Make source files available for distribution via pkg_npm
filegroup(
name = "files",
srcs = glob(["*"]),
)
25 changes: 25 additions & 0 deletions bazel/esbuild/esbuild-amd-config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import url from 'url';
import path from 'path';

/** Path to the ESBuild configuration maintained by the user. */
const userConfigExecPath = 'TMPL_CONFIG_PATH';

/** User ESBuild config. Empty if none is loaded. */
let userConfig = {};

if (userConfigExecPath !== '') {
const userConfigPath = path.join(process.cwd(), userConfigExecPath);
const userConfigUrl = url.pathToFileURL(userConfigPath);

// Load the user config, assuming it is set as `default` export. This is
// usually an `export default` statement or a named export named `default`.
userConfig = (await import(userConfigUrl)).default;
}

export default {
...userConfig,
globalName: '__exports',
format: 'iife',
banner: {js: 'define("TMPL_MODULE_NAME", [], function() {'},
footer: {js: 'return __exports;})'},
};
52 changes: 52 additions & 0 deletions bazel/esbuild/index.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
load("@npm//@bazel/esbuild:index.bzl", _esbuild = "esbuild", _esbuild_config = "esbuild_config")
load("//bazel:expand_template.bzl", "expand_template")

# Re-export of the actual esbuild definitions.
esbuild_config = _esbuild_config

def esbuild(
name,
# Inline source contents to make debugging easier. Contents are inlined by default
# in ESBuild but `@bazel/esbuild` sets the default to `false`. Inlining sources is
# helpful as otherwise developers would need to manually wire up the Bazel execroot
# as workspace in the Chrome devtools.
# https://github.com/bazelbuild/rules_nodejs/blob/c30a26c13d20dac48dc9f220370cb02a317b13f3/packages/esbuild/esbuild.bzl#L333.
sources_content = True,
**kwargs):
_esbuild(
name = name,
sources_content = sources_content,
**kwargs
)

def esbuild_amd(name, entry_point, module_name, testonly = False, config = None, deps = [], **kwargs):
"""Generates an AMD bundle for the specified entry-point with the given AMD module name."""
expand_template(
name = "%s_config" % name,
testonly = testonly,
template = "//bazel/esbuild:esbuild-amd-config.mjs",
output_name = "%s_config.mjs" % name,
substitutions = {
"TMPL_MODULE_NAME": module_name,
"TMPL_CONFIG_PATH": "$(execpath %s)" % config if config else "",
},
data = [config] if config else None,
)

_esbuild_config(
name = "%s_config_lib" % name,
testonly = testonly,
config_file = "%s_config" % name,
# Adds the user configuration and its deps as dependency of the AMD ESBuild config.
# https://github.com/bazelbuild/rules_nodejs/blob/a892caf5a040bae5eeec174a3cf6250f02caf364/packages/esbuild/esbuild_config.bzl#L23.
deps = [config, "%s_deps" % config] if config else None,
)

esbuild(
name = name,
testonly = testonly,
deps = deps,
entry_point = entry_point,
config = "%s_config_lib" % name,
**kwargs
)

0 comments on commit 32a04e0

Please sign in to comment.