Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: eliminate distinction between srcs and assets #750

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 44 additions & 23 deletions examples/assets/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@ ts_config(
ts_project(
name = "ts",
srcs = [
"src/generated.json",
"src/index.ts",
],
assets = [
"src/styles.css",
"src/generated.json",
],
tsconfig = ":config",
)

ts_project(
name = "ts-out",
srcs = [
"src/generated.json",
"src/index.ts",
],
assets = [
"src/styles.css",
"src/generated.json",
],
extends = ":config",
out_dir = "out",
Expand All @@ -47,8 +43,10 @@ ts_project(

ts_project(
name = "ts-runtime-assets",
srcs = ["src/index-runtime.ts"],
assets = ["src/generated.json"],
srcs = [
"src/generated.json",
"src/index-runtime.ts",
],
tsconfig = ":config",
)

Expand All @@ -60,17 +58,19 @@ js_test(

ts_project(
name = "ts-runtime-outdir-assets",
srcs = ["src/index-runtime.ts"],
assets = ["src/generated.json"],
srcs = [
"src/generated.json",
"src/index-runtime.ts",
],
extends = ":config",
out_dir = "out",
tsconfig = {"compilerOptions": {"outDir": "out"}},
out_dir = "runtime-out",
tsconfig = {"compilerOptions": {"outDir": "runtime-out"}},
)

js_test(
name = "ts-runtime-outdir-assets_test",
data = [":ts-runtime-outdir-assets"],
entry_point = "out/src/index-runtime.js",
entry_point = "runtime-out/src/index-runtime.js",
)

filegroup(
Expand All @@ -85,8 +85,8 @@ ts_project(
name = "ts-out-target",
srcs = [
"src/index.ts",
":assets-filegroup",
],
assets = [":assets-filegroup"],
out_dir = "out-target",
tsconfig = ":config",
)
Expand All @@ -113,8 +113,8 @@ ts_project(
name = "ts-out-copy-target",
srcs = [
"src/index.ts",
":assets-filegroup",
],
assets = [":assets-filegroup"],
out_dir = "out-copy-target",
tsconfig = ":config",
)
Expand All @@ -133,8 +133,6 @@ ts_project(
name = "ts-out-copy-target-abs",
srcs = [
"src/index.ts",
],
assets = [
"//%s:assets-filegroup" % package_name(),
],
out_dir = "out-copy-target-abs",
Expand All @@ -152,13 +150,38 @@ assert_outputs(
)

ts_project(
name = "ts-root",
name = "ts-typings-out",
srcs = [
"src/index.ts",
"types/typings.d.ts",
],
assets = [
"src/styles.css",
out_dir = "typings-out",
declaration_dir = "typings-out-declarations",
tsconfig = ":config",
)

assert_outputs(
name = "ts-typings-out_outputs_test",
actual = ":ts-typings-out",
expected = [
"examples/assets/typings-out/src/index.js",
],
)

assert_outputs(
name = "ts-typings-out-typings_outputs_test",
actual = ":ts-typings-out_types",
expected = [
"examples/assets/typings-out-declarations/types/typings.d.ts",
],
)

ts_project(
name = "ts-root",
srcs = [
"src/generated.json",
"src/index.ts",
"src/styles.css",
],
root_dir = "src",
tsconfig = ":config",
Expand All @@ -167,11 +190,9 @@ ts_project(
ts_project(
name = "ts-root-out",
srcs = [
"src/generated.json",
"src/index.ts",
],
assets = [
"src/styles.css",
"src/generated.json",
],
out_dir = "root-out",
root_dir = "src",
Expand Down
2 changes: 0 additions & 2 deletions examples/json_data/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ ts_project(
srcs = [
"src/index.ts",
"src/src.json",
],
assets = [
"src/asset.txt",
],
data = [
Expand Down
21 changes: 9 additions & 12 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def ts_project(
args = [],
data = [],
deps = [],
assets = [],
extends = None,
allow_js = False,
isolated_typecheck = False,
Expand Down Expand Up @@ -94,14 +93,10 @@ def ts_project(
- If `resolve_json_module` is set, include all JSON files in the package,
but exclude `package.json`, `package-lock.json`, and `tsconfig*.json`.

assets: Files which are needed by a downstream build step such as a bundler.

These files are **not** included as inputs to any actions spawned by `ts_project`.
They are not transpiled, and are not visible to the type-checker.
Instead, these files appear among the *outputs* of this target.

A typical use is when your TypeScript code has an import that TS itself doesn't understand
such as
Non-TypeScript and JavaScript assets may be included; they will be copied directly
to the outputs. As with transpiled outputs, the `out_dir` and `root_dir` attributes
determine where such assets are written. A typical use is when your TypeScript code
has an import that TS itself doesn't understand, such as

`import './my.scss'`

Expand All @@ -115,7 +110,7 @@ def ts_project(

Note that `data` is used for files that are resolved by some binary, including a test
target. Behind the scenes, `data` populates Bazel's Runfiles object in `DefaultInfo`,
while this attribute populates the `transitive_sources` of the `JsInfo`.
while assets populate the `transitive_sources` of the `JsInfo`.

data: Files needed at runtime by binaries or tests that transitively depend on this target.
See https://bazel.build/reference/be/common-definitions#typical-attributes
Expand Down Expand Up @@ -353,9 +348,11 @@ def ts_project(
tsc_js_outs = []
tsc_map_outs = []
if emit_tsc_js:
tsc_js_outs = _lib.calculate_js_outs(srcs, out_dir, root_dir, allow_js, resolve_json_module, preserve_jsx, emit_declaration_only)
tsc_js_outs = _lib.calculate_js_outs(srcs, out_dir, root_dir, allow_js, preserve_jsx, emit_declaration_only)
tsc_map_outs = _lib.calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, emit_declaration_only)

asset_outs = _lib.calculate_asset_outs(srcs, out_dir, typings_out_dir, root_dir, allow_js)

# Custom typing transpiler
if emit_transpiler_dts:
declarations_target_name = "%s_declarations" % name
Expand Down Expand Up @@ -425,7 +422,6 @@ def ts_project(
name = name,
srcs = srcs,
args = args,
assets = assets,
data = data,
deps = tsc_deps,
tsconfig = tsconfig,
Expand All @@ -447,6 +443,7 @@ def ts_project(
map_outs = tsc_map_outs,
typings_outs = tsc_typings_outs,
typing_maps_outs = tsc_typing_maps_outs,
asset_outs = asset_outs,
buildinfo_out = tsbuildinfo_path if composite or incremental else None,
no_emit = no_emit,
emit_declaration_only = emit_declaration_only,
Expand Down
2 changes: 1 addition & 1 deletion ts/private/ts_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _write_tsconfig_rule(ctx):
src_files = []
for f in ctx.files.files:
# Only include typescript source files
if not _lib.is_ts_src(f.basename, ctx.attr.allow_js, ctx.attr.resolve_json_module, True):
if not _lib.is_ts_src(f.basename, ctx.attr.allow_js, True):
continue

if f.short_path.startswith(local_package_prefix):
Expand Down
Loading