Skip to content

Commit

Permalink
cleanup test cases & providers when not bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Oct 22, 2023
1 parent 848a344 commit 3ba39b9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 45 deletions.
34 changes: 21 additions & 13 deletions e2e/bundle/BUILD
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("@bazel_skylib//rules:build_test.bzl", "build_test")

SRCS = ["main.js", "lib.js"]
SRCS = [
"main.js",
"lib.js",
]

ENTRY = "main.js"

esbuild(
name = "bundle-true",
testonly = 1,
srcs = SRCS,
entry_point = ENTRY,
output = "sm-default.js",
testonly = 1,
output = "bundle-true.js",
)
build_test(
name = "true",
targets = [":bundle-true"],

assert_contains(
name = "bundle-true_test",
actual = ":bundle-true.js",
expected = "ANSWER = 42",
)

esbuild(
name = "bundle-false",
testonly = 1,
srcs = SRCS,
entry_point = ENTRY,
bundle = False,
output_dir = ".",
testonly = 1,
entry_point = ENTRY,
output = "bundle-false.js",
)
build_test(
name = "false",
targets = [":bundle-false"],

assert_contains(
name = "bundle-false_test",
actual = ":bundle-false.js",
expected = "from \"./lib\"",
)
2 changes: 0 additions & 2 deletions e2e/bundle/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ local_repository(
path = "../..",
)

#---SNIP--- Below here is re-used in the workspace snippet published on releases

######################
# rules_esbuild setup #
######################
Expand Down
7 changes: 6 additions & 1 deletion esbuild/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def esbuild(name, output_dir = False, splitting = False, config = None, **kwargs
)
config = config_file

if output_dir == True or entry_points or splitting == True:
if type(output_dir) != "bool":
fail("output_dir expected to be a bool")
if type(splitting) != "bool":
fail("splitting expected to be a bool")

if output_dir or entry_points or splitting:
_esbuild(
name = name,
config = config,
Expand Down
78 changes: 49 additions & 29 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ See https://esbuild.github.io/api/#target for more details
),
"bundle": attr.bool(
default = True,
doc = """If true, esbuild will bundle the input files, inlining their dependencies recursively"""
doc = """If true, esbuild will bundle the input files, inlining their dependencies recursively""",
),
"config": attr.label(
mandatory = False,
Expand Down Expand Up @@ -275,7 +275,6 @@ def _esbuild_impl(ctx):

args.update({"outfile": _bin_relative_path(ctx, js_out)})


env = {
"BAZEL_BINDIR": ctx.bin_dir.path,
"ESBUILD_BINARY_PATH": "../../../" + esbuild_toolinfo.target_tool_path,
Expand Down Expand Up @@ -349,34 +348,55 @@ def _esbuild_impl(ctx):

output_sources_depset = depset(output_sources)

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(
# Since we're bundling, only propagate `data` npm packages to the direct dependencies of
if ctx.attr.bundle:
transitive_sources = output_sources_depset
transitive_declarations = depset()

# If we're bundling, we don't propogate linked npm packages from dependencies since
# they are bundled and the dependencies are dropped. If a subset of linked npm
# dependencies are not bundled it is up the the user to re-specify these in `data` if
# they are runtime dependencies to progagate to binary rules or `srcs` if they are to be
# propagated to downstream build targets.
npm_linked_packages = js_lib_helpers.gather_npm_linked_packages(
srcs = ctx.attr.srcs,
deps = [],
)

# If we're bundling, only propagate `data` npm packages to the direct dependencies of
# downstream linked `npm_package` targets instead of the common `data` and `deps` pattern.
targets = ctx.attr.data,
)
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 = output_sources_depset,
data = ctx.attr.data,
# Don't include transitive runfiles when bundling
deps = [] if ctx.attr.bundle else ctx.attr.srcs,
)
runfiles = js_lib_helpers.gather_runfiles(
ctx = ctx,
sources = output_sources_depset,
data = ctx.attr.data,
deps = [],
)
else:
transitive_sources = js_lib_helpers.gather_transitive_sources(
sources = output_sources_depset,
targets = ctx.attr.srcs + ctx.attr.deps,
)
transitive_declarations = js_lib_helpers.gather_transitive_declarations(
declarations = [],
targets = ctx.attr.srcs + ctx.attr.deps,
)
npm_linked_packages = js_lib_helpers.gather_npm_linked_packages(
srcs = ctx.attr.srcs,
deps = ctx.attr.deps,
)
npm_package_store_deps = js_lib_helpers.gather_npm_package_store_deps(
targets = ctx.attr.data + ctx.attr.deps,
)
runfiles = js_lib_helpers.gather_runfiles(
ctx = ctx,
sources = transitive_sources,
data = ctx.attr.data,
deps = ctx.attr.srcs + ctx.attr.deps,
)

return [
DefaultInfo(
Expand All @@ -396,8 +416,8 @@ def _esbuild_impl(ctx):
transitive_npm_linked_package_files = npm_linked_packages.direct_files if ctx.attr.bundle else npm_linked_packages.transitive_files,
transitive_npm_linked_packages = npm_linked_packages.direct if ctx.attr.bundle else npm_linked_packages.transitive,
# If we're bundling, we don't propogate any transitive sources from dependencies
transitive_sources = output_sources_depset if ctx.attr.bundle else transitive_sources,
transitive_declarations = depset() if ctx.attr.bundle else transitive_declarations
transitive_sources = transitive_sources,
transitive_declarations = transitive_declarations,
),
]

Expand Down

0 comments on commit 3ba39b9

Please sign in to comment.