Skip to content

Commit ac2097c

Browse files
dgp1130alexeagle
authored andcommitted
feat(typescript): add data attribute
Adds the `data` attribute to `ts_project()`. This functions like most `data` attributes as defined by [common Bazel definitions](https://docs.bazel.build/versions/master/be/common-definitions.html#typical-attributes). All it really does is add the provided files to the `DefaultInfo()` runfiles object, where they will be propagated to any consuming rules.
1 parent b0fddae commit ac2097c

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

packages/typescript/internal/ts_project.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ _DEFAULT_TYPESCRIPT_PACKAGE = (
2323

2424
_ATTRS = {
2525
"args": attr.string_list(),
26+
"data": attr.label_list(default = [], allow_files = True),
2627
"declaration_dir": attr.string(),
2728
"deps": attr.label_list(
2829
providers = [
@@ -223,7 +224,9 @@ def _ts_project_impl(ctx):
223224
DefaultInfo(
224225
files = default_outputs_depset,
225226
runfiles = ctx.runfiles(
226-
transitive_files = default_outputs_depset,
227+
transitive_files = depset(ctx.files.data, transitive = [
228+
default_outputs_depset,
229+
]),
227230
collect_default = True,
228231
),
229232
),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("//packages/jasmine:index.bzl", "jasmine_node_test")
2+
load("//packages/typescript:index.bzl", "ts_project")
3+
4+
ts_project(
5+
name = "data",
6+
testonly = True,
7+
srcs = ["data.spec.ts"],
8+
composite = True,
9+
# Should be readable at runtime under runfiles directory.
10+
data = ["foo.txt"],
11+
extends = "//packages/typescript/test/ts_project:tsconfig-base.json",
12+
tsconfig = "tsconfig.json",
13+
deps = [
14+
"@npm//@types/jasmine",
15+
"@npm//@types/node",
16+
],
17+
)
18+
19+
jasmine_node_test(
20+
name = "test",
21+
srcs = [":data"],
22+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {promises as fs} from 'fs';
2+
3+
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!);
4+
5+
const FOO_PATH =
6+
runfiles.resolve('build_bazel_rules_nodejs/packages/typescript/test/ts_project/data/foo.txt');
7+
8+
describe('data', () => {
9+
it('makes files available at runtime', async () => {
10+
const foo = await fs.readFile(FOO_PATH, {encoding: 'utf8'});
11+
expect(foo).toBe('Foo\n');
12+
});
13+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../tsconfig-base.json",
3+
"compilerOptions": {
4+
"types": ["jasmine", "node"]
5+
},
6+
"references": [
7+
{"path": "./"}
8+
],
9+
"include": ["*.spec.ts"]
10+
}

0 commit comments

Comments
 (0)