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

test: add case for filegroup of srcs #211

Merged
merged 2 commits into from
Oct 31, 2022
Merged
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
57 changes: 23 additions & 34 deletions examples/simple/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
"""Simplest example of using ts_project to transpile and type-check a file"""

# Dependencies for all targets in this package
_DEPS = [
"//examples:node_modules/@myorg/js_lib",
"//examples:node_modules/@myorg/dts_lib",
"//examples:node_modules/@types/node",
"//examples:node_modules/date-fns",
]
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

# Type-checks, and emits output to
# bazel-bin/examples/simple/foo.js
Expand All @@ -21,33 +15,28 @@ ts_project(
out_dir = ".",
# Note, the tsconfig attribute defaults to the tsconfig.json file in this directory.
# tsconfig = "<default>",
deps = _DEPS,
deps = [
# Two dependencies are linked from first-party code in this repo
# by the npm_link_package calls in /examples/BUILD.bazel
"//examples:node_modules/@myorg/js_lib",
"//examples:node_modules/@myorg/dts_lib",
# Two dependencies are linked from third-party npm packages
# by the npm_link_all_packages call in /examples/BUILD.bazel
"//examples:node_modules/@types/node",
"//examples:node_modules/date-fns",
],
)

# Code generation tools can produce .ts outputs.
# This example just writes one directly to bazel-bin.
write_file(
name = "code_generation",
out = "generated.ts",
content = [
"export const data: string[] = []",
],
# Assert that the output looks like what we expected.
# Demonstrates that we can refer directly to "foo.js" as a pre-declared output.
assert_contains(
name = "test_js",
actual = "foo.js",
expected = """var js_lib_1 = require("@myorg/js_lib")""",
)

# Writes output to bazel-bin/examples/simple/build/foo.js
ts_project(
name = "outdir",
# Demonstrates that you can mix sources with generated files
srcs = [
"foo.ts",
"generated.ts",
],
extends = "tsconfig.json",
# A tsconfig.json file will be generated with this content
tsconfig = {
"compilerOptions": {
"outDir": "build",
},
},
deps = _DEPS,
assert_contains(
name = "test_dts",
actual = "foo.d.ts",
expected = "export declare const b: string",
)
82 changes: 82 additions & 0 deletions examples/srcs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""Shows different ways to pass source files into ts_project"""

load("@aspect_bazel_lib//lib:params_file.bzl", "params_file")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

# By default, the macro collects all .ts files in this directory.
# See the docs on srcs for exactly which files are included.
ts_project(
name = "srcs-auto",
out_dir = "auto",
)

# The sources can come from the default output of some other target.
# In this case, the ts_project macro won't be able to enumerate individual files
# so it cannot pre-declare the corresponding .js outputs.
filegroup(
name = "srcs",
srcs = glob(["*.ts"]),
)

ts_project(
name = "srcs-filegroup",
srcs = [":srcs"],
out_dir = "filegroup",
)

# Tools can also generate .ts source files, so the sources are actually in the bazel-out tree.
# This example just writes one directly but this could instead be some tool.
write_file(
name = "code_generation",
out = "generated.ts",
content = [
"export const data: string[] = []",
],
)

# Writes output to bazel-bin/examples/simple/build/foo.js
ts_project(
name = "srcs-generated",
# Demonstrates that you can mix sources with generated files
srcs = [
"a.ts",
"generated.ts",
],
out_dir = "generated",
)

# Testing what outputs are actually produced
[
params_file(
name = "srcs-{}_mf".format(case),
out = "srcs-{}.mf".format(case),
args = ["$(rootpaths :srcs-{})".format(case)],
data = [":srcs-" + case],
)
for case in [
"auto",
"filegroup",
"generated",
]
]

[
assert_contains(
name = "test_" + case,
actual = "srcs-{}.mf".format(case),
expected = "examples/srcs/{}/a.js".format(case),
)
for case in [
"auto",
"filegroup",
"generated",
]
]

assert_contains(
name = "test_gen",
actual = "generated/generated.js",
expected = "exports.data = []",
)
1 change: 1 addition & 0 deletions examples/srcs/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: string = 'a'
1 change: 1 addition & 0 deletions examples/srcs/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b: string = 'b'
1 change: 1 addition & 0 deletions examples/srcs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}