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 94aa0ea
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 58 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\"",
)
5 changes: 3 additions & 2 deletions e2e/bundle/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"Bazel dependencies"
bazel_dep(name = "aspect_rules_esbuild", dev_dependency = True, version = "0.0.0")
bazel_dep(name = "bazel_skylib", dev_dependency = True, version = "1.4.1")

bazel_dep(name = "aspect_rules_esbuild", version = "0.0.0", dev_dependency = True)
bazel_dep(name = "aspect_bazel_lib", version = "1.36.0", dev_dependency = True)

local_path_override(
module_name = "aspect_rules_esbuild",
Expand Down
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
95 changes: 55 additions & 40 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,56 @@ 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
# downstream linked `npm_package` targets instead of the common `data` and `deps` pattern.
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,
)
if ctx.attr.bundle:
# If we're bundling we don't propogate any transitive sources or declarations since sources
# are typically bundled into the output. 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.
transitive_sources = output_sources_depset
transitive_declarations = depset()
npm_linked_packages = js_lib_helpers.gather_npm_linked_packages(
srcs = ctx.attr.srcs,
deps = [],
)
npm_linked_packages = struct(
direct_files = npm_linked_packages.direct_files,
direct = npm_linked_packages.direct,
transitive_files = npm_linked_packages.direct_files,
transitive = npm_linked_packages.direct,
)
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,
deps = [],
)
else:
# If we're not bundling then include all transitive files
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 @@ -388,16 +409,10 @@ def _esbuild_impl(ctx):
npm_linked_packages = npm_linked_packages.direct,
npm_package_store_deps = npm_package_store_deps,
sources = output_sources_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.
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_npm_linked_package_files = npm_linked_packages.transitive_files,
transitive_npm_linked_packages = npm_linked_packages.transitive,
transitive_sources = transitive_sources,
transitive_declarations = transitive_declarations,
),
]

Expand Down

0 comments on commit 94aa0ea

Please sign in to comment.