Skip to content

Commit

Permalink
feat(esbuild): add support for toolchains
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem committed Jun 2, 2021
1 parent daf722a commit 389feaf
Show file tree
Hide file tree
Showing 30 changed files with 284 additions and 215 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ browser_repositories(
)

# Setup esbuild dependencies
load("//packages/esbuild:esbuild_repo.bzl", "esbuild_dependencies")
load("//packages/esbuild:esbuild_repositories.bzl", "esbuild_repositories")

esbuild_dependencies()
esbuild_repositories()

#
# Dependencies to run stardoc & generating documentation
Expand Down
36 changes: 4 additions & 32 deletions examples/esbuild/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,14 @@ http_archive(
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.5.1/rules_nodejs-3.5.1.tar.gz"],
)

_ESBUILD_VERSION = "0.12.1"

http_archive(
name = "esbuild_darwin",
build_file_content = """exports_files(["bin/esbuild"])""",
sha256 = "efb34692bfa34db61139eb8e46cd6cf767a42048f41c8108267279aaf58a948f",
strip_prefix = "package",
urls = [
"https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-%s.tgz" % _ESBUILD_VERSION,
],
)

http_archive(
name = "esbuild_windows",
build_file_content = """exports_files(["esbuild.exe"])""",
sha256 = "10439647b11c7fd1d9647fd98d022fe2188b4877d2d0b4acbe857f4e764b17a9",
strip_prefix = "package",
urls = [
"https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-%s.tgz" % _ESBUILD_VERSION,
],
)

http_archive(
name = "esbuild_linux",
build_file_content = """exports_files(["bin/esbuild"])""",
sha256 = "de8409b90ec3c018ffd899b49ed5fc462c61b8c702ea0f9da013e0e1cd71549a",
strip_prefix = "package",
urls = [
"https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-%s.tgz" % _ESBUILD_VERSION,
],
)

load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")

npm_install(
name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
)

load("@npm//@bazel/esbuild:esbuild_repositories.bzl", "esbuild_repositories")

esbuild_repositories()
5 changes: 0 additions & 5 deletions examples/esbuild/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ esbuild(
minify = True,
# setting node as the platform will default us to CJS
platform = "node",
tool = select({
"@bazel_tools//src/conditions:darwin": "@esbuild_darwin//:bin/esbuild",
"@bazel_tools//src/conditions:linux_x86_64": "@esbuild_linux//:bin/esbuild",
"@bazel_tools//src/conditions:windows": "@esbuild_windows//:esbuild.exe",
}),
deps = [
":lib",
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"bazel:update-deleted-packages": "sed -i.bak \"/--deleted_packages/s#=.*#=$(find {examples,e2e}/*/* \\( -name BUILD -or -name BUILD.bazel \\) | xargs -n 1 dirname | paste -sd, -)#\" .bazelrc && rm .bazelrc.bak",
"update-codeowners": "./scripts/update_codeowners.sh",
"update-nodejs-versions": "node ./scripts/update-nodejs-versions.js > internal/node/node_versions.bzl",
"update-esbuild-versions": "node ./scripts/update-esbuild-versions.js > packages/esbuild/esbuild_repo.bzl",
"update-esbuild-versions": "node ./scripts/update-esbuild-versions.js",
"format": "git-clang-format",
"format-all": "clang-format --glob='{internal/**/,examples/**/}*.{js,ts}' -i",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && node ./scripts/on-version.js && bazel build //:release && node ./scripts/on-release.js && git stage version.bzl docs/install.md packages/create/index.js README.md CHANGELOG.md e2e/*/WORKSPACE examples/*/WORKSPACE",
Expand Down
25 changes: 18 additions & 7 deletions packages/esbuild/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ codeowners(

bzl_library(
name = "bzl",
srcs = glob(["**/*.bzl"]),
srcs = glob(["**/*.bzl"]) + [
"@bazel_tools//tools:bzl_srcs",
],
deps = [
"//packages/esbuild/toolchain:bzl",
"@bazel_skylib//lib:paths",
"@build_bazel_rules_nodejs//:bzl",
"@build_bazel_rules_nodejs//internal/common:bzl",
Expand All @@ -51,20 +54,28 @@ copy_file(
out = ":npm_version_check.js",
)

pkg_npm(
name = "npm_package",
filegroup(
name = "srcs",
srcs = [
"esbuild.bzl",
"esbuild_packages.bzl",
"esbuild_repositories.bzl",
"helpers.bzl",
"index.bzl",
"launcher.js",
"package.json",
],
build_file_content = """exports_files(["launcher.js"])
""",
)

pkg_npm(
name = "npm_package",
srcs = [
":srcs",
"//packages/esbuild/toolchain:srcs",
],
build_file_content = """exports_files(["launcher.js"])""",
substitutions = dict({
"@build_bazel_rules_nodejs//packages/esbuild:esbuild.bzl": "//@bazel/esbuild:esbuild.bzl",
"@build_bazel_rules_nodejs//packages/esbuild:launcher.js": "//@bazel/esbuild:launcher.js",
"@build_bazel_rules_nodejs//packages/esbuild": "//@bazel/esbuild",
}),
deps = [
":README.md",
Expand Down
15 changes: 6 additions & 9 deletions packages/esbuild/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ esbuild rule
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "node_modules_aspect", "run_node")
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "MODULE_MAPPINGS_ASPECT_RESULTS_NAME", "module_mappings_aspect")
load("@build_bazel_rules_nodejs//packages/esbuild/toolchain:toolchain.bzl", "TOOLCHAIN")
load(":helpers.bzl", "desugar_entry_point_names", "filter_files", "generate_path_mapping", "resolve_entry_point", "write_jsconfig_file")

def _esbuild_impl(ctx):
Expand Down Expand Up @@ -135,7 +136,7 @@ def _esbuild_impl(ctx):
execution_requirements = {"no-remote-exec": "1"}

launcher_args = ctx.actions.args()
launcher_args.add("--esbuild=%s" % ctx.executable.tool.path)
launcher_args.add("--esbuild=%s" % ctx.toolchains[TOOLCHAIN].binary.path)

run_node(
ctx = ctx,
Expand All @@ -148,7 +149,7 @@ def _esbuild_impl(ctx):
env = env,
executable = "launcher",
link_workspace_root = ctx.attr.link_workspace_root,
tools = [ctx.executable.tool],
tools = [ctx.toolchains[TOOLCHAIN].binary],
)

outputs_depset = depset(outputs)
Expand Down Expand Up @@ -307,15 +308,11 @@ edge16, node10, esnext). Default es2015.
See https://esbuild.github.io/api/#target for more details
""",
),
"tool": attr.label(
allow_single_file = True,
mandatory = True,
executable = True,
cfg = "exec",
doc = "An executable for the esbuild binary",
),
},
implementation = _esbuild_impl,
toolchains = [
str(TOOLCHAIN),
],
doc = """Runs the esbuild bundler under Bazel
For further information about esbuild, see https://esbuild.github.io/
Expand Down
70 changes: 70 additions & 0 deletions packages/esbuild/esbuild_packages.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Info for the esbuild packages used"""

### These values are updated automaticly via `yarn update-esbuild-versions`
_VERSION = "0.12.5"
_DARWIN_AMD64_SHA = "6037c279c2bd09ae6ff490f18ea95ab514db044df63db47268411dfc48a49b16"
_DARWIN_ARM64_SHA = "bb950e084302652a11c5d38aeae773fa6f46b864e7b89264c9354222aa8d326d"
_LINUX_AMD64_SHA = "ea9bae194f4d18297ccb00139c62326e3da40835bbadbe21848c3e2cded35a3a"
_LINUX_ARM64_SHA = "f70fbe27a5dab5dd6f19f76f60660ad59b8a6883652bb1fd878078cf922915ad"
_WINDOWS_AMD64_SHA = "ce16dc8d565f8b30b538496961ba17c1977ddd400d0f20b7e89482ecd0944d5d"

ESBUILD_PACKAGES = struct(
version = _VERSION,
platforms = dict({
"darwin_amd64": struct(
sha = _DARWIN_AMD64_SHA,
urls = [
"https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-%s.tgz" % _VERSION,
],
binary_path = "bin/esbuild",
exec_compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
),
"darwin_arm64": struct(
sha = _DARWIN_ARM64_SHA,
urls = [
"https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-%s.tgz" % _VERSION,
],
binary_path = "bin/esbuild",
exec_compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
),
"linux_amd64": struct(
sha = _LINUX_AMD64_SHA,
urls = [
"https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-%s.tgz" % _VERSION,
],
binary_path = "bin/esbuild",
exec_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"linux_arm64": struct(
sha = _LINUX_ARM64_SHA,
urls = [
"https://registry.npmjs.org/esbuild-linux-arm64/-/esbuild-linux-arm64-%s.tgz" % _VERSION,
],
binary_path = "bin/esbuild",
exec_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
),
"windows_amd64": struct(
sha = _WINDOWS_AMD64_SHA,
urls = [
"https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-%s.tgz" % _VERSION,
],
binary_path = "esbuild.exe",
exec_compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
),
}),
)
42 changes: 0 additions & 42 deletions packages/esbuild/esbuild_repo.bzl

This file was deleted.

30 changes: 30 additions & 0 deletions packages/esbuild/esbuild_repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Helper macro for fetching esbuild versions
"""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(":esbuild_packages.bzl", "ESBUILD_PACKAGES")

def _maybe(repo_rule, name, **kwargs):
if name not in native.existing_rules():
repo_rule(name = name, **kwargs)

def esbuild_repositories(name = ""):
"""Helper for fetching and setting up the esbuild versions and toolchains
Args:
name: currently unused
"""

for name, meta in ESBUILD_PACKAGES.platforms.items():
_maybe(
http_archive,
name = "esbuild_%s" % name,
urls = meta.urls,
strip_prefix = "package",
build_file_content = """exports_files(["%s"])""" % meta.binary_path,
sha256 = meta.sha,
)

toolchain_label = Label("@build_bazel_rules_nodejs//packages/esbuild/toolchain:esbuild_%s_toolchain" % name)
native.register_toolchains("@%s//%s:%s" % (toolchain_label.workspace_name, toolchain_label.package, toolchain_label.name))
5 changes: 5 additions & 0 deletions packages/esbuild/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ load(
"@build_bazel_rules_nodejs//packages/esbuild:esbuild.bzl",
_esbuild_macro = "esbuild_macro",
)
load(
"@build_bazel_rules_nodejs//packages/esbuild/toolchain:toolchain.bzl",
_configure_esbuild_toolchain = "configure_esbuild_toolchain",
)

esbuild = _esbuild_macro
configure_esbuild_toolchain = _configure_esbuild_toolchain
Loading

0 comments on commit 389feaf

Please sign in to comment.