Skip to content

Commit

Permalink
feat: set explicit and configurable tsconfig
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 committed Jun 9, 2023
1 parent 196eb6c commit 861a5ae
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 4 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: 1 addition & 1 deletion e2e/smoke/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")

esbuild(
name = "lib",
srcs = ["main.js"],
srcs = ["main.js", "tsconfig.json"],
entry_point = "main.js",
)

Expand Down
1 change: 1 addition & 0 deletions e2e/smoke/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
with invalid syntax, esbuild shouldn't use this
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
46 changes: 46 additions & 0 deletions e2e/tsconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
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 = ":copy-a",
output = "a.js",
)

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

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

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


# Must use tsconfig files in the bindir due to esbuild escaping the sandbox
# See https://github.com/aspect-build/rules_esbuild/issues/58
copy_to_bin(
name = "copy-a",
srcs = ["tsconfig-a.json"]
)
copy_to_bin(
name = "copy-b",
srcs = ["tsconfig-b.json"]
)
7 changes: 7 additions & 0 deletions e2e/tsconfig/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"Bazel dependencies"
bazel_dep(name = "aspect_rules_esbuild", dev_dependency = True, version = "0.0.0")

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

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

######################
# 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/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"]
}
}
}
2 changes: 1 addition & 1 deletion esbuild/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports_files(
)

exports_files(
["launcher.js"],
["launcher.js", "empty.json"],
visibility = ["//visibility:public"],
)

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: 12 additions & 0 deletions esbuild/private/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ 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 = False,
allow_single_file = True,
default = Label("@aspect_rules_esbuild//esbuild/private:empty.json"),
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 Down Expand Up @@ -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, ctx.file.tsconfig),
"metafile": ctx.attr.metafile,
"platform": ctx.attr.platform,
# Don't preserve symlinks since doing so breaks node_modules resolution
Expand Down Expand Up @@ -281,6 +291,8 @@ def _esbuild_impl(ctx):
other_inputs.append(args_file)
launcher_args.add("--esbuild_args=%s" % _bin_relative_path(ctx, args_file))

other_inputs.append(ctx.file.tsconfig)

if ctx.attr.metafile:
# add metafile
meta_file = ctx.actions.declare_file("%s_metadata.json" % ctx.attr.name)
Expand Down
6 changes: 5 additions & 1 deletion esbuild/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def _esbuild_repo_impl(repository_ctx):
Label("@aspect_rules_esbuild//esbuild/private:launcher.js"),
"launcher.js",
)
repository_ctx.symlink(
Label("@aspect_rules_esbuild//esbuild/private:empty.json"),
"empty.json",
)
build_content = """#Generated by esbuild/repositories.bzl
load("@aspect_rules_esbuild//esbuild:toolchain.bzl", "esbuild_toolchain")
load("@aspect_rules_js//js:defs.bzl", "js_binary")
Expand All @@ -52,7 +56,7 @@ npm_link_package(
js_binary(
name = "launcher",
entry_point = "launcher.js",
data = [":node_modules/esbuild"],
data = [":node_modules/esbuild", ":empty.json"],
)
esbuild_toolchain(
Expand Down

0 comments on commit 861a5ae

Please sign in to comment.