-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
912311a
commit 32a04e0
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(["*"]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;})'}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |