Skip to content

Commit

Permalink
feat(esbuild): allow for .ts, .tsx and .jsx entry points
Browse files Browse the repository at this point in the history
Only enable code splitting when user opts into it. Previously
there was no way to produce an output directory without also
enabling code splitting.
  • Loading branch information
Daniel Muller authored and alexeagle committed Jul 2, 2021
1 parent 3be2902 commit e3edb28
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
14 changes: 10 additions & 4 deletions docs/esbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ This will create an output directory containing all the code split chunks, along
<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-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-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-srcs">srcs</a>, <a href="#esbuild-target">target</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>

Runs the esbuild bundler under Bazel
Expand Down Expand Up @@ -234,9 +234,7 @@ file is named 'foo.js', you should set this to 'foo.css'.

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

(*Boolean*): If true, esbuild produces an output directory containing all the output files from code splitting for multiple entry points

See https://esbuild.github.io/api/#splitting and https://esbuild.github.io/api/#entry-points for more details
(*Boolean*): If true, esbuild produces an output directory containing all output files

Defaults to `False`

Expand Down Expand Up @@ -269,6 +267,14 @@ See https://esbuild.github.io/api/#sources-content for more details

Defaults to `False`

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

(*Boolean*): If true, esbuild produces an output directory containing all the output files from code splitting for multiple entry points

See https://esbuild.github.io/api/#splitting and https://esbuild.github.io/api/#entry-points for more details

Defaults to `False`

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

(*<a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a>*): Source files to be made available to esbuild
Expand Down
30 changes: 21 additions & 9 deletions packages/esbuild/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,21 @@ def _esbuild_impl(ctx):
# disable this unless also minifying
args.update({"treeShaking": "ignore-annotations"})

if ctx.attr.splitting:
if not ctx.attr.output_dir:
fail("output_dir must be set to True when splitting is set to True")
args.update({
"format": "esm",
"splitting": True,
})

if ctx.attr.output_dir:
js_out = ctx.actions.declare_directory("%s" % ctx.attr.name)
outputs.append(js_out)

# disable the log limit and show all logs
args.update({
"format": "esm",
"outdir": js_out.path,
"splitting": True,
})
else:
js_out = ctx.outputs.output
Expand Down Expand Up @@ -277,10 +283,7 @@ file is named 'foo.js', you should set this to 'foo.css'.""",
),
"output_dir": attr.bool(
default = False,
doc = """If true, esbuild produces an output directory containing all the output files from code splitting for multiple entry points
See https://esbuild.github.io/api/#splitting and https://esbuild.github.io/api/#entry-points for more details
""",
doc = """If true, esbuild produces an output directory containing all output files""",
),
"output_map": attr.output(
mandatory = False,
Expand Down Expand Up @@ -310,6 +313,13 @@ See https://esbuild.github.io/api/#sourcemap for more details
See https://esbuild.github.io/api/#sources-content for more details
""",
),
"splitting": attr.bool(
default = False,
doc = """If true, esbuild produces an output directory containing all the output files from code splitting for multiple entry points
See https://esbuild.github.io/api/#splitting and https://esbuild.github.io/api/#entry-points for more details
""",
),
"srcs": attr.label_list(
allow_files = True,
default = [],
Expand All @@ -334,14 +344,15 @@ For further information about esbuild, see https://esbuild.github.io/
""",
)

def esbuild_macro(name, output_dir = False, **kwargs):
def esbuild_macro(name, output_dir = False, splitting = False, **kwargs):
"""esbuild helper macro around the `esbuild_bundle` rule
For a full list of attributes, see the `esbuild_bundle` rule
Args:
name: The name used for this rule and output files
output_dir: If `True`, produce a code split bundle in an output directory
output_dir: If `True`, produce an output directory
splitting: If `True`, produce a code split bundle in the output directory
**kwargs: All other args from `esbuild_bundle`
"""

Expand Down Expand Up @@ -374,10 +385,11 @@ def esbuild_macro(name, output_dir = False, **kwargs):
data = deps + srcs,
)

if output_dir == True or entry_points:
if output_dir == True or entry_points or splitting == True:
esbuild(
name = name,
srcs = srcs,
splitting = splitting,
output_dir = True,
args_file = args_file,
launcher = _launcher,
Expand Down
14 changes: 6 additions & 8 deletions packages/esbuild/helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Utility helper functions for the esbuild rule
load("@build_bazel_rules_nodejs//third_party/github.com/bazelbuild/bazel-skylib:lib/paths.bzl", "paths")

TS_EXTENSIONS = ["ts", "tsx"]
JS_EXTENSIONS = ["js", "mjs"]
JS_EXTENSIONS = ["js", "jsx", "mjs"]
ALLOWED_EXTENSIONS = JS_EXTENSIONS + TS_EXTENSIONS

def strip_ext(f):
"Strips the extension of a file."
return f.short_path[:-len(f.extension) - 1]

def resolve_entry_point(f, inputs, srcs):
"""Find a corresponding javascript entrypoint for a provided file
"""Find a corresponding entrypoint for a provided file
Args:
f: The file where its basename is used to match the entrypoint
Expand All @@ -26,15 +26,13 @@ def resolve_entry_point(f, inputs, srcs):

no_ext = strip_ext(f)

# check for the ts file in srcs
for i in srcs:
if i.extension in TS_EXTENSIONS:
for i in inputs:
if i.extension in ALLOWED_EXTENSIONS:
if strip_ext(i) == no_ext:
return i

# check for a js files everywhere else
for i in inputs:
if i.extension in JS_EXTENSIONS:
for i in srcs:
if i.extension in ALLOWED_EXTENSIONS:
if strip_ext(i) == no_ext:
return i

Expand Down
1 change: 1 addition & 0 deletions packages/esbuild/test/entries/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ esbuild(
"a.ts",
"b.ts",
],
splitting = True,
deps = [":lib"],
)

Expand Down
1 change: 1 addition & 0 deletions packages/esbuild/test/splitting/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ esbuild(
},
entry_point = "main.ts",
output_dir = True,
splitting = True,
deps = [":main"],
)

Expand Down

0 comments on commit e3edb28

Please sign in to comment.