Skip to content

Commit

Permalink
fix(esbuild): allow whitespace within args
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Oct 12, 2021
1 parent 8a48991 commit c8a3c28
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 24 deletions.
8 changes: 4 additions & 4 deletions docs/esbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ This will create an output directory containing all the code split chunks, along
**USAGE**

<pre>
esbuild(<a href="#esbuild-name">name</a>, <a href="#esbuild-args">args</a>, <a href="#esbuild-args_file">args_file</a>, <a href="#esbuild-config">config</a>, <a href="#esbuild-define">define</a>, <a href="#esbuild-deps">deps</a>, <a href="#esbuild-entry_point">entry_point</a>, <a href="#esbuild-entry_points">entry_points</a>, <a href="#esbuild-external">external</a>, <a href="#esbuild-format">format</a>,
esbuild(<a href="#esbuild-name">name</a>, <a href="#esbuild-args">args</a>, <a href="#esbuild-args_json">args_json</a>, <a href="#esbuild-config">config</a>, <a href="#esbuild-define">define</a>, <a href="#esbuild-deps">deps</a>, <a href="#esbuild-entry_point">entry_point</a>, <a href="#esbuild-entry_points">entry_points</a>, <a href="#esbuild-external">external</a>, <a href="#esbuild-format">format</a>,
<a href="#esbuild-launcher">launcher</a>, <a href="#esbuild-link_workspace_root">link_workspace_root</a>, <a href="#esbuild-max_threads">max_threads</a>, <a href="#esbuild-minify">minify</a>, <a href="#esbuild-node_context_data">node_context_data</a>, <a href="#esbuild-output">output</a>, <a href="#esbuild-output_css">output_css</a>,
<a href="#esbuild-output_dir">output_dir</a>, <a href="#esbuild-output_map">output_map</a>, <a href="#esbuild-platform">platform</a>, <a href="#esbuild-sourcemap">sourcemap</a>, <a href="#esbuild-sources_content">sources_content</a>, <a href="#esbuild-splitting">splitting</a>, <a href="#esbuild-srcs">srcs</a>, <a href="#esbuild-target">target</a>)
</pre>
Expand All @@ -123,11 +123,11 @@ Values are subject to $(location ...) expansion

Defaults to `{}`

<h4 id="esbuild-args_file">args_file</h4>
<h4 id="esbuild-args_json">args_json</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Internal use only
(*String*): Internal use only

Defaults to `None`
Defaults to `""`

<h4 id="esbuild-config">config</h4>

Expand Down
37 changes: 17 additions & 20 deletions packages/esbuild/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
esbuild rule
"""

load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "params_file")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo", "node_modules_aspect", "run_node")
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "LinkerPackageMappingInfo", "module_mappings_aspect")
load("@build_bazel_rules_nodejs//internal/common:expand_variables.bzl", "expand_variables")
Expand Down Expand Up @@ -151,9 +151,14 @@ def _esbuild_impl(ctx):
launcher_args.add("--metafile=%s" % meta_file.path)

# add reference to the users args file, these are merged within the launcher
if ctx.attr.args_file:
inputs.append(ctx.file.args_file)
launcher_args.add("--user_args=%s" % ctx.file.args_file.path)
if ctx.attr.args_json:
user_args_file = ctx.actions.declare_file("%s.user.args.json" % ctx.attr.name)
inputs.append(user_args_file)
ctx.actions.write(
output = user_args_file,
content = ctx.expand_location(ctx.attr.args_json),
)
launcher_args.add("--user_args=%s" % user_args_file.path)

if ctx.attr.config:
configs = ctx.attr.config[JSEcmaScriptModuleInfo].sources.to_list()
Expand Down Expand Up @@ -204,8 +209,7 @@ esbuild = rule(
doc = """A dict of extra arguments that are included in the call to esbuild, where the key is the argument name.
Values are subject to $(location ...) expansion""",
),
"args_file": attr.label(
allow_single_file = True,
"args_json": attr.string(
mandatory = False,
doc = "Internal use only",
),
Expand Down Expand Up @@ -393,24 +397,17 @@ def esbuild_macro(name, output_dir = False, splitting = False, **kwargs):
deps = kwargs.pop("deps", []) + ["@esbuild_npm//esbuild"]
entry_points = kwargs.get("entry_points", None)

# TODO(mattem): remove `args` and `args_file` in 5.x and everything can go via `config`
args_file = kwargs.pop("args_file", None)
if args_file:
fail("Setting 'args_file' is not supported, set 'config' instead")
# TODO(mattem): remove `args` and `args_json` in 5.x and everything can go via `config`
args_json = kwargs.pop("args_json", None)
if args_json:
fail("Setting 'args_json' is not supported, set 'config' instead")

args = kwargs.pop("args", {})
if args:
if type(args) != type(dict()):
fail("Expected 'args' to be of type dict")

args_file = "%s.user.args.json" % name
params_file(
name = "%s_args" % name,
testonly = kwargs.get("testonly", False),
out = args_file,
args = [json.encode(args)],
data = deps + srcs,
)
args_json = json.encode(args)

config = kwargs.pop("config", None)
if config:
Expand All @@ -423,7 +420,7 @@ def esbuild_macro(name, output_dir = False, splitting = False, **kwargs):
srcs = srcs,
splitting = splitting,
output_dir = True,
args_file = args_file,
args_json = args_json,
launcher = _launcher,
deps = deps,
**kwargs
Expand All @@ -441,7 +438,7 @@ def esbuild_macro(name, output_dir = False, splitting = False, **kwargs):
esbuild(
name = name,
srcs = srcs,
args_file = args_file,
args_json = args_json,
output = output,
output_map = output_map,
launcher = _launcher,
Expand Down
66 changes: 66 additions & 0 deletions packages/esbuild/test/banner/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
load("//:index.bzl", "generated_file_test")
load("//packages/esbuild:index.bzl", "esbuild")

esbuild(
name = "banner",
args = {
"banner": {
"js": "//hello",
},
},
entry_point = "main.js",
)

esbuild(
name = "banner_newline",
args = {
"banner": {
"js": "//header\n//comments",
},
},
entry_point = "main.js",
)

esbuild(
name = "banner_quotes",
args = {
"banner": {
"js": "\"use strict\"",
},
},
entry_point = "main.js",
)

esbuild(
name = "banner_spaces",
args = {
"banner": {
"js": "#!/usr/bin/env node",
},
},
entry_point = "main.js",
)

generated_file_test(
name = "banner_test",
src = "banner.golden.txt",
generated = "banner.js",
)

generated_file_test(
name = "banner_newline_test",
src = "banner.newline.golden.txt",
generated = "banner_newline.js",
)

generated_file_test(
name = "banner_quotes_test",
src = "banner.quotes.golden.txt",
generated = "banner_quotes.js",
)

generated_file_test(
name = "banner_spaces_test",
src = "banner.spaces.golden.txt",
generated = "banner_spaces.js",
)
6 changes: 6 additions & 0 deletions packages/esbuild/test/banner/banner.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//hello
(() => {
// packages/esbuild/test/banner/main.js
console.log("a script");
})();
//# sourceMappingURL=banner.js.map
7 changes: 7 additions & 0 deletions packages/esbuild/test/banner/banner.newline.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//header
//comments
(() => {
// packages/esbuild/test/banner/main.js
console.log("a script");
})();
//# sourceMappingURL=banner_newline.js.map
6 changes: 6 additions & 0 deletions packages/esbuild/test/banner/banner.quotes.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict"
(() => {
// packages/esbuild/test/banner/main.js
console.log("a script");
})();
//# sourceMappingURL=banner_quotes.js.map
6 changes: 6 additions & 0 deletions packages/esbuild/test/banner/banner.spaces.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node
(() => {
// packages/esbuild/test/banner/main.js
console.log("a script");
})();
//# sourceMappingURL=banner_spaces.js.map
1 change: 1 addition & 0 deletions packages/esbuild/test/banner/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("a script");

0 comments on commit c8a3c28

Please sign in to comment.