Skip to content

Commit

Permalink
feat: add bundle=false support (#98)
Browse files Browse the repository at this point in the history
* feature: bundle=false support

* cleanup test cases & providers when not bundling

* update docs

---------

Co-authored-by: Greg Magolan <[email protected]>
  • Loading branch information
josephglanville and gregmagolan authored Oct 22, 2023
1 parent e2c8172 commit 044face
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 32 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jobs:
- 'e2e/npm-links'
- 'e2e/sourcemaps'
- 'e2e/tsconfig'
- 'e2e/bundle'
exclude:
# Don't test macos with Bazel 5 to minimize macOS minutes (billed at 10X)
# https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions#included-storage-and-minutes
Expand Down
8 changes: 5 additions & 3 deletions docs/esbuild.md

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

2 changes: 2 additions & 0 deletions e2e/bundle/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build --enable_runfiles
common:bzlmod --enable_bzlmod
38 changes: 38 additions & 0 deletions e2e/bundle/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")

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

ENTRY = "main.js"

esbuild(
name = "bundle-true",
testonly = 1,
srcs = SRCS,
entry_point = ENTRY,
output = "bundle-true.js",
)

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

esbuild(
name = "bundle-false",
testonly = 1,
srcs = SRCS,
bundle = False,
entry_point = ENTRY,
output = "bundle-false.js",
)

assert_contains(
name = "bundle-false_test",
actual = ":bundle-false.js",
expected = "from \"./lib\"",
)
9 changes: 9 additions & 0 deletions e2e/bundle/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"Bazel dependencies"

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",
path = "../..",
)
34 changes: 34 additions & 0 deletions e2e/bundle/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Override http_archive for local testing
local_repository(
name = "aspect_rules_esbuild",
path = "../..",
)

######################
# rules_esbuild setup #
######################

# Fetches the rules_esbuild dependencies.
# If you want to have a different version of some dependency,
# you should fetch it *before* calling this.
# Alternatively, you can skip calling this function, so long as you've
# already fetched all the dependencies.
load("@aspect_rules_esbuild//esbuild:dependencies.bzl", "rules_esbuild_dependencies")

rules_esbuild_dependencies()

# If you didn't already register a toolchain providing nodejs, do that:
load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "node",
node_version = DEFAULT_NODE_VERSION,
)

# Register a toolchain containing esbuild npm package and native bindings
load("@aspect_rules_esbuild//esbuild:repositories.bzl", "LATEST_ESBUILD_VERSION", "esbuild_register_toolchains")

esbuild_register_toolchains(
name = "esbuild",
esbuild_version = LATEST_ESBUILD_VERSION,
)
Empty file added e2e/bundle/WORKSPACE.bzlmod
Empty file.
1 change: 1 addition & 0 deletions e2e/bundle/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ANSWER = 42;
3 changes: 3 additions & 0 deletions e2e/bundle/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ANSWER } from './lib';

console.log(ANSWER);
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
87 changes: 59 additions & 28 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ edge16, node10, esnext). Default es2015.
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""",
),
"config": attr.label(
mandatory = False,
allow_single_file = True,
Expand Down Expand Up @@ -192,7 +196,7 @@ def _esbuild_impl(ctx):
tsconfig_bin_copy = copy_file_to_bin_action(ctx, ctx.file.tsconfig)

args = dict({
"bundle": True,
"bundle": ctx.attr.bundle,
"define": dict([
[
k,
Expand Down Expand Up @@ -342,26 +346,58 @@ def _esbuild_impl(ctx):
executable = launcher,
)

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,
)

output_sources_depset = depset(output_sources)

runfiles = js_lib_helpers.gather_runfiles(
ctx = ctx,
sources = output_sources_depset,
data = ctx.attr.data,
# Since we're bundling, we don't propogate any transitive runfiles from dependencies
deps = [],
)
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 @@ -373,15 +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,
# Since 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,
transitive_npm_linked_packages = npm_linked_packages.direct,
# Since we're bundling, we don't propogate any transitive sources from dependencies
transitive_sources = output_sources_depset,
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 044face

Please sign in to comment.