generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
dzbarsky
wants to merge
1
commit into
aspect-build:main
Choose a base branch
from
dzbarsky:esbuild_transpiler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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