Skip to content

Commit

Permalink
feat: set explicit and configurable tsconfig (#151)
Browse files Browse the repository at this point in the history
By default esbuild uses any local tsconfig.json file. With sandboxing issues (#58)
this may unexpectedly cause `esbuild_bundle` to pickup any local tsconfig.json.

A custom tsconfig can now be set using `esbuild_bundle(tsconfig)`. By default an empty tsconfig is used to prevent unexpected sandboxing issues.
  • Loading branch information
jbedard authored Jun 13, 2023
1 parent 196eb6c commit 2a5609c
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
- 'e2e/smoke'
- 'e2e/npm-links'
- 'e2e/sourcemaps'
- 'e2e/tsconfig'
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
3 changes: 2 additions & 1 deletion 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/tsconfig/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build --enable_runfiles
common:bzlmod --enable_bzlmod
48 changes: 48 additions & 0 deletions e2e/tsconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")

SRCS = ["main.js", "libs/a.js", "libs/b.js"]
ENTRY = "main.js"

esbuild(
name = "target-a",
srcs = SRCS,
entry_point = ENTRY,
tsconfig = "tsconfig-a.json",
output = "a.js",
)

esbuild(
name = "target-b",
srcs = SRCS,
entry_point = ENTRY,
tsconfig = "tsconfig-b.json",
output = "b.js",
)

esbuild(
name = "target-none",
# include the tsconfig.json with bad syntax and ensure it is not used
srcs = [ENTRY, "tsconfig.json"],
entry_point = ENTRY,
output = "none.js",
external = ["var-lib"],
)

assert_contains(
name = "config-a",
actual = "a.js",
expected = "library: A"
)

assert_contains(
name = "config-b",
actual = "b.js",
expected = "library: B"
)

assert_contains(
name = "config-none",
actual = "none.js",
expected = "var-lib"
)
8 changes: 8 additions & 0 deletions e2e/tsconfig/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"Bazel dependencies"
bazel_dep(name = "aspect_rules_esbuild", dev_dependency = True, version = "0.0.0")
bazel_dep(name = "aspect_bazel_lib", dev_dependency = True, version = "1.29.2")

local_path_override(
module_name = "aspect_rules_esbuild",
path = "../..",
)
22 changes: 22 additions & 0 deletions e2e/tsconfig/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local_repository(
name = "aspect_rules_esbuild",
path = "../..",
)

load("@aspect_rules_esbuild//esbuild:dependencies.bzl", "rules_esbuild_dependencies")

rules_esbuild_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

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

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/tsconfig/WORKSPACE.bzlmod
Empty file.
1 change: 1 addition & 0 deletions e2e/tsconfig/libs/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ANSWER = "library: A"
1 change: 1 addition & 0 deletions e2e/tsconfig/libs/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ANSWER = "library: B"
3 changes: 3 additions & 0 deletions e2e/tsconfig/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ANSWER } from "var-lib"

console.log(ANSWER)
7 changes: 7 additions & 0 deletions e2e/tsconfig/tsconfig-a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"var-lib": ["./libs/a.js"]
}
}
}
7 changes: 7 additions & 0 deletions e2e/tsconfig/tsconfig-b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"var-lib": ["./libs/b.js"]
}
}
}
1 change: 1 addition & 0 deletions e2e/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
with invalid syntax, esbuild shouldn't use this
3 changes: 3 additions & 0 deletions esbuild/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def esbuild(name, output_dir = False, splitting = False, config = None, **kwargs
srcs = kwargs.pop("srcs", [])
deps = kwargs.pop("deps", [])
entry_points = kwargs.get("entry_points", None)
tsconfig = kwargs.pop("tsconfig", Label("@aspect_rules_esbuild//esbuild/private:empty-json"))

if types.is_dict(config):
config_file = "_%s_config.mjs" % name
Expand All @@ -37,6 +38,7 @@ def esbuild(name, output_dir = False, splitting = False, config = None, **kwargs
_esbuild(
name = name,
config = config,
tsconfig = tsconfig,
srcs = srcs,
splitting = splitting,
output_dir = True,
Expand All @@ -61,6 +63,7 @@ def esbuild(name, output_dir = False, splitting = False, config = None, **kwargs
name = name,
srcs = srcs,
config = config,
tsconfig = tsconfig,
output = output,
output_map = output_map,
deps = deps,
Expand Down
7 changes: 7 additions & 0 deletions esbuild/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

exports_files(
Expand All @@ -10,6 +11,12 @@ exports_files(
visibility = ["//visibility:public"],
)

copy_to_bin(
name = "empty-json",
srcs = ["empty.json"],
visibility = ["//visibility:public"],
)

bzl_library(
name = "esbuild",
srcs = ["esbuild.bzl"],
Expand Down
1 change: 1 addition & 0 deletions esbuild/private/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
12 changes: 11 additions & 1 deletion esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ See https://esbuild.github.io/api/#target for more details
TODO: show how to write a config file that depends on plugins, similar to the esbuild_config macro in rules_nodejs.
""",
),
"tsconfig": attr.label(
mandatory = True,
allow_single_file = True,
doc = """TypeScript configuration file used by esbuild. Default to an empty file with no configuration.
See https://esbuild.github.io/api/#tsconfig for more details
"""
)
}

def _bin_relative_path(ctx, file):
Expand All @@ -181,6 +189,7 @@ def _esbuild_impl(ctx):

entry_points = desugar_entry_point_names(ctx.file.entry_point, ctx.files.entry_points)
entry_points_bin_copy = copy_files_to_bin_actions(ctx, entry_points)
tsconfig_bin_copy = copy_file_to_bin_action(ctx, ctx.file.tsconfig)

args = dict({
"bundle": True,
Expand All @@ -199,6 +208,7 @@ def _esbuild_impl(ctx):
# Also disable the log limit and show all logs
"logLevel": "warning",
"logLimit": 0,
"tsconfig": _bin_relative_path(ctx, tsconfig_bin_copy),
"metafile": ctx.attr.metafile,
"platform": ctx.attr.platform,
# Don't preserve symlinks since doing so breaks node_modules resolution
Expand Down Expand Up @@ -311,7 +321,7 @@ def _esbuild_impl(ctx):
file
for file in ctx.files.srcs
if not (file.path.endswith(".d.ts") or file.path.endswith(".tsbuildinfo"))
]) + entry_points_bin_copy + other_inputs + node_toolinfo.tool_files + esbuild_toolinfo.tool_files,
]) + entry_points_bin_copy + [tsconfig_bin_copy] + other_inputs + node_toolinfo.tool_files + esbuild_toolinfo.tool_files,
transitive = [js_lib_helpers.gather_files_from_js_providers(
targets = ctx.attr.srcs + ctx.attr.deps,
include_transitive_sources = True,
Expand Down

0 comments on commit 2a5609c

Please sign in to comment.