Skip to content

Commit

Permalink
fix: allow targets as ts_project(assets, srcs)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Apr 11, 2024
1 parent 4237138 commit 46a08e9
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 5 deletions.
84 changes: 84 additions & 0 deletions examples/assets/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_outputs")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
load("@bazel_skylib//rules:build_test.bzl", "build_test")

Expand Down Expand Up @@ -42,6 +44,84 @@ ts_project(
tsconfig = {"compilerOptions": {"outDir": "out"}},
)

filegroup(
name = "assets-filegroup",
srcs = [
"src/generated.json",
"src/styles.css",
],
)

ts_project(
name = "ts-out-target",
srcs = [
"src/index.ts",
],
assets = [":assets-filegroup"],
out_dir = "out-target",
tsconfig = ":config",
)

assert_outputs(
name = "ts-out-target_outputs_test",
actual = ":ts-out-target",
expected = [
"examples/assets/out-target/src/generated.json",
"examples/assets/out-target/src/index.js",
"examples/assets/out-target/src/styles.css",
],
)

copy_to_directory(
name = "assets-directory",
srcs = [
"src/generated.json",
"src/styles.css",
],
)

ts_project(
name = "ts-out-copy-target",
srcs = [
"src/index.ts",
],
assets = [":assets-filegroup"],
out_dir = "out-copy-target",
tsconfig = ":config",
)

assert_outputs(
name = "ts-out-copy-target_outputs_test",
actual = ":ts-out-copy-target",
expected = [
"examples/assets/out-copy-target/src/generated.json",
"examples/assets/out-copy-target/src/index.js",
"examples/assets/out-copy-target/src/styles.css",
],
)

ts_project(
name = "ts-out-copy-target-abs",
srcs = [
"src/index.ts",
],
assets = [
"//%s:assets-filegroup" % package_name(),
],
out_dir = "out-copy-target-abs",
tsconfig = ":config",
)

assert_outputs(
name = "ts-out-copy-target-abs_outputs_test",
actual = ":ts-out-copy-target-abs",
expected = [
"examples/assets/out-copy-target-abs/src/generated.json",
"examples/assets/out-copy-target-abs/src/index.js",
"examples/assets/out-copy-target-abs/src/styles.css",
],
)

ts_project(
name = "ts-root",
srcs = [
Expand Down Expand Up @@ -92,5 +172,9 @@ build_test(
":root-out/index.js",
":root-out/styles.css",
":root-out/generated.json",
# asset targets instead of files
":ts-out-target",
":ts-out-copy-target",
":ts-out-copy-target-abs",
],
)
2 changes: 1 addition & 1 deletion examples/project_references/lib_b/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ts_project(
ts_project(
name = "transpile_test",
testonly = True,
srcs = [":index.spec.ts"],
srcs = ["index.spec.ts"],
composite = True,
declaration = True,
extends = "//examples/project_references:tsconfig-base",
Expand Down
3 changes: 2 additions & 1 deletion examples/srcs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ ts_project(
# Demonstrates that you can mix sources with generated files
srcs = [
"a.ts",
"generated.ts",
"generated.ts", # reference to generated file
":code_generation", # reference to target generating same file
],
out_dir = "generated",
)
Expand Down
72 changes: 72 additions & 0 deletions examples/srcs_targets/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Shows different ways to pass source files into ts_project"""

load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

write_file(
name = "gen-a-ts",
out = "a.ts",
content = [
"export const a = 42",
],
)

write_file(
name = "gen-b.ts", # Ensure this name has a ".ts" suffix!
out = "b.ts",
content = [
"export const a = 42",
],
)

ts_project(
name = "out-refs",
srcs = [
"a.ts",
"b.ts",
],
declaration = True,
out_dir = "out-refs",
source_map = True,
)

ts_project(
name = "out-target-refs",
srcs = [
":a.ts",
":b.ts",
],
declaration = True,
out_dir = "out-target-refs",
source_map = True,
)

ts_project(
name = "target-refs",
srcs = [
":gen-a-ts",
":gen-b.ts",
],
declaration = True,
out_dir = "target-refs",
source_map = True,
)

build_test(
name = "test",
targets = [
# pre-declared outputs
"out-refs/a.js",
"out-refs/a.js.map",
"out-refs/a.d.ts",
"out-refs/b.js",
"out-refs/b.js.map",
"out-refs/b.d.ts",

# ts_project targets
":out-refs",
":out-target-refs",
":target-refs",
],
)
6 changes: 6 additions & 0 deletions examples/srcs_targets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"sourceMap": true,
"declaration": true
}
}
21 changes: 18 additions & 3 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,15 @@ def _relative_to_package(path, ctx):
return path

def _is_typings_src(src):
if not _is_src_file(src):
return False

return src.endswith(".d.ts") or src.endswith(".d.mts") or src.endswith(".d.cts")

def _is_js_src(src, allow_js, resolve_json_module):
if not _is_src_file(src):
return False

if src.endswith(".js") or src.endswith(".jsx") or src.endswith(".mjs") or src.endswith(".cjs"):
return allow_js

Expand All @@ -185,11 +191,19 @@ def _is_js_src(src, allow_js, resolve_json_module):
return False

def _is_ts_src(src, allow_js, resolve_json_module):
if not _is_src_file(src):
return False

if (src.endswith(".ts") or src.endswith(".tsx") or src.endswith(".mts") or src.endswith(".cts")):
return not _is_typings_src(src)

return _is_js_src(src, allow_js, resolve_json_module)

def _is_src_file(src):
return not (src.startswith(":") or src.startswith("//"))

_is_asset_src = _is_src_file

def _replace_ext(f, ext_map):
cur_ext = f[f.rindex("."):]
new_ext = ext_map.get(cur_ext)
Expand Down Expand Up @@ -223,9 +237,10 @@ def _to_js_out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, ext
def _calculate_assets_outs(assets, out_dir = ".", root_dir = "."):
outs = []
for a in assets:
out = _to_out_path(a, out_dir, root_dir)
if out != a:
outs.append(out)
if _is_asset_src(a):
out = _to_out_path(a, out_dir, root_dir)
if out != a:
outs.append(out)
return outs

# Quick check to validate path options
Expand Down

0 comments on commit 46a08e9

Please sign in to comment.