Skip to content

Commit

Permalink
feat: allow metafile generation to be optional (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem authored Jul 8, 2022
1 parent 3d07857 commit f3def54
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
5 changes: 3 additions & 2 deletions docs/esbuild.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ This can be useful if running many esbuild rule invocations in parallel, which h
For general use, leave this attribute unset.
""",
),
"metafile": attr.bool(
default = False,
doc = "If true, esbuild creates a metafile along with the output",
mandatory = False,
),
"minify": attr.bool(
default = False,
doc = """Minifies the bundle with the built in minification.
Expand Down Expand Up @@ -197,7 +202,7 @@ def _esbuild_impl(ctx):
# Also disable the log limit and show all logs
"logLevel": "warning",
"logLimit": 0,
"metafile": True,
"metafile": ctx.attr.metafile,
"platform": ctx.attr.platform,
"preserveSymlinks": True,
"sourcesContent": ctx.attr.sources_content,
Expand Down Expand Up @@ -269,10 +274,11 @@ def _esbuild_impl(ctx):
inputs.append(args_file)
launcher_args.add("--esbuild_args=%s" % args_file.short_path)

# add metafile
meta_file = ctx.actions.declare_file("%s_metadata.json" % ctx.attr.name)
outputs.append(meta_file)
launcher_args.add("--metafile=%s" % meta_file.short_path)
if ctx.attr.metafile:
# add metafile
meta_file = ctx.actions.declare_file("%s_metadata.json" % ctx.attr.name)
outputs.append(meta_file)
launcher_args.add("--metafile=%s" % meta_file.short_path)

# add reference to the users args file, these are merged within the launcher
if ctx.attr.args_file:
Expand Down
7 changes: 4 additions & 3 deletions esbuild/private/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ async function runOneBuild(args, userArgsFilePath, configFilePath) {
}
}

const metafile = getFlag('--metafile')

try {
const result = await esbuild.build(args)
writeFileSync(metafile, JSON.stringify(result.metafile))
if (result.metafile) {
const metafile = getFlag('--metafile');
writeFileSync(metafile, JSON.stringify(result.metafile));
}
} catch (e) {
console.error(e)
process.exit(1)
Expand Down
15 changes: 15 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ esbuild(
"name.js",
],
entry_point = "main.js",
metafile = True,
)

build_test(
name = "test",
targets = [":lib"],
)

sh_test(
name = "metafile_test",
size = "small",
srcs = [
"metafile_test.sh",
],
args = [
"$(locations :lib)",
],
data = [
":lib",
],
)
11 changes: 11 additions & 0 deletions examples/metafile_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -uo pipefail;

while [ "$#" -ne 0 ]; do
[[ "lib_metadata.json" == "$(basename $1)" ]] && exit 0;
shift;
done

echo "Expected lib_metadata.json to be produced" >&2
exit 1

0 comments on commit f3def54

Please sign in to comment.