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 13, 2023
1 parent 196eb6c commit 741ac7f
Show file tree
Hide file tree
Showing 18 changed files with 129 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
34 changes: 34 additions & 0 deletions e2e/tsconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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",
)

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

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

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", version = "1.29.2")

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"]
}
}
}
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 @@
{}
13 changes: 12 additions & 1 deletion 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 All @@ -181,6 +190,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 +209,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 +322,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
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 741ac7f

Please sign in to comment.