From fe693f7b7418470970a435727058818e9a027909 Mon Sep 17 00:00:00 2001 From: Joseph Glanville Date: Wed, 16 Nov 2022 16:25:35 +1100 Subject: [PATCH] feature: bundle=false support --- esbuild/private/esbuild.bzl | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/esbuild/private/esbuild.bzl b/esbuild/private/esbuild.bzl index 4061cfc..fba8740 100644 --- a/esbuild/private/esbuild.bzl +++ b/esbuild/private/esbuild.bzl @@ -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, @@ -172,7 +176,7 @@ def _esbuild_impl(ctx): entry_points = desugar_entry_point_names(ctx.file.entry_point, ctx.files.entry_points) args = dict({ - "bundle": True, + "bundle": ctx.attr.bundle, "define": dict([ [ k, @@ -249,6 +253,7 @@ def _esbuild_impl(ctx): args.update({"outfile": js_out.short_path}) + env = { "BAZEL_BINDIR": ctx.bin_dir.path, "ESBUILD_BINARY_PATH": "../../../" + esbuild_toolinfo.target_tool_path, @@ -318,6 +323,18 @@ def _esbuild_impl(ctx): executable = launcher, ) + 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 = [], @@ -327,14 +344,12 @@ def _esbuild_impl(ctx): 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 = [], + # Don't include transitive runfiles when bundling + deps = [] if ctx.attr.bundle else ctx.attr.srcs, ) return [ @@ -347,15 +362,16 @@ 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 + # 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, - 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.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 ), ]