Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add esbuild transpiler #62

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion esbuild/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,3 +62,10 @@ def esbuild(name, output_dir = False, splitting = False, config = None, **kwargs
deps = deps,
**kwargs
)

esbuild_transpiler = rule(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets create a macro here similar to the swc one to hide things like js_outs etc: https://github.com/aspect-build/rules_swc/blob/785f94c2bf39d3ddac4cbfd9d7786f6067209f77/swc/defs.bzl

implementation = _esbuild_transpiler.implementation,
attrs = _esbuild_transpiler.attrs,
toolchains = _esbuild_transpiler.toolchains,
)

96 changes: 96 additions & 0 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member

@jbedard jbedard Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a mnemonic and progress message, see swc

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,
)
Loading