From e23be685e07a78ef14dd744ac844193dec09bd2c Mon Sep 17 00:00:00 2001 From: David Zbarsky Date: Sun, 28 Aug 2022 11:42:38 -0400 Subject: [PATCH] Add esbuild transpiler --- esbuild/defs.bzl | 9 +++- esbuild/private/esbuild.bzl | 96 +++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/esbuild/defs.bzl b/esbuild/defs.bzl index 5aa74b3..484a6de 100644 --- a/esbuild/defs.bzl +++ b/esbuild/defs.bzl @@ -2,7 +2,7 @@ load("@bazel_skylib//lib:types.bzl", "types") load("@bazel_skylib//rules:write_file.bzl", "write_file") -load("//esbuild/private:esbuild.bzl", _esbuild = "esbuild_bundle") +load("//esbuild/private:esbuild.bzl", _esbuild = "esbuild_bundle", _esbuild_transpiler = "esbuild_transpiler") def esbuild(name, output_dir = False, splitting = False, config = None, **kwargs): """esbuild helper macro around the `esbuild_bundle` rule @@ -62,3 +62,10 @@ def esbuild(name, output_dir = False, splitting = False, config = None, **kwargs deps = deps, **kwargs ) + +esbuild_transpiler = rule( + implementation = _esbuild_transpiler.implementation, + attrs = _esbuild_transpiler.attrs, + toolchains = _esbuild_transpiler.toolchains, +) + diff --git a/esbuild/private/esbuild.bzl b/esbuild/private/esbuild.bzl index 4061cfc..8c7d1a6 100644 --- a/esbuild/private/esbuild.bzl +++ b/esbuild/private/esbuild.bzl @@ -378,3 +378,99 @@ Runs the esbuild bundler under Bazel For further information about esbuild, see https://esbuild.github.io/ """, ) + +_transpiler_attrs = { + "srcs": attr.label_list( + allow_files = True, + default = [], + doc = """Source files to be made available to esbuild""", + ), + "data": js_lib_helpers.JS_LIBRARY_DATA_ATTR, + "js_outs": attr.output_list(doc = """list of expected JavaScript output files. +There must be one for each entry in srcs, and in the same order."""), + "map_outs": attr.output_list(doc = """list of expected source map output files. +Can be empty, meaning no source maps should be produced. +If non-empty, there must be one for each entry in srcs, and in the same order."""), +} + +def _esbuild_transpiler_impl(ctx): + esbuild_toolinfo = ctx.toolchains["@aspect_rules_esbuild//esbuild:toolchain_type"].esbuildinfo + + args = ctx.actions.args() + args.add("--log-level=silent") + + if ctx.outputs.map_outs: + # CLI doesn't let us pick the map output location + # We happen to use the same convention as esbuild. + args.add("--sourcemap=external") + + for i, src in enumerate(ctx.files.srcs): + out = ctx.outputs.js_outs[i] + + src_args = ctx.actions.args() + src_args.add(src) + src_args.add(out, format="--outfile=%s") + + action_outputs = [ctx.outputs.js_outs[i]] + if ctx.outputs.map_outs: + action_outputs.append(ctx.outputs.map_outs[i]) + + ctx.actions.run( + inputs = [src], + executable = esbuild_toolinfo.tool_files[0], + arguments = [args, src_args], + outputs = action_outputs, + ) + + + output_sources_depset = depset(ctx.outputs.js_outs + ctx.outputs.map_outs) + + transitive_sources = js_lib_helpers.gather_transitive_sources( + sources = output_sources_depset, + targets = ctx.attr.srcs, + ) + + transitive_declarations = js_lib_helpers.gather_transitive_declarations( + declarations = [], + targets = ctx.attr.srcs, + ) + + npm_linked_packages = js_lib_helpers.gather_npm_linked_packages( + srcs = ctx.attr.srcs, + deps = [], + ) + + npm_package_store_deps = js_lib_helpers.gather_npm_package_store_deps( + targets = ctx.attr.data, + ) + + runfiles = js_lib_helpers.gather_runfiles( + ctx = ctx, + sources = transitive_sources, + data = ctx.attr.data, + deps = ctx.attr.srcs, + ) + + return [ + js_info( + npm_linked_package_files = npm_linked_packages.direct_files, + npm_linked_packages = npm_linked_packages.direct, + npm_package_store_deps = npm_package_store_deps, + sources = output_sources_depset, + transitive_declarations = transitive_declarations, + transitive_npm_linked_package_files = npm_linked_packages.transitive_files, + transitive_npm_linked_packages = npm_linked_packages.transitive, + transitive_sources = transitive_sources, + ), + DefaultInfo( + files = output_sources_depset, + runfiles = runfiles, + ), + ] + + +esbuild_transpiler = struct( + implementation = _esbuild_transpiler_impl, + attrs = _transpiler_attrs, + toolchains = lib.toolchains, +)