Skip to content

Commit

Permalink
feat(typescript): add data attribute
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgp1130 authored and alexeagle committed Feb 17, 2021
1 parent b0fddae commit ac2097c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _DEFAULT_TYPESCRIPT_PACKAGE = (

_ATTRS = {
"args": attr.string_list(),
"data": attr.label_list(default = [], allow_files = True),
"declaration_dir": attr.string(),
"deps": attr.label_list(
providers = [
Expand Down Expand Up @@ -223,7 +224,9 @@ def _ts_project_impl(ctx):
DefaultInfo(
files = default_outputs_depset,
runfiles = ctx.runfiles(
transitive_files = default_outputs_depset,
transitive_files = depset(ctx.files.data, transitive = [
default_outputs_depset,
]),
collect_default = True,
),
),
Expand Down
22 changes: 22 additions & 0 deletions packages/typescript/test/ts_project/data/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//packages/jasmine:index.bzl", "jasmine_node_test")
load("//packages/typescript:index.bzl", "ts_project")

ts_project(
name = "data",
testonly = True,
srcs = ["data.spec.ts"],
composite = True,
# Should be readable at runtime under runfiles directory.
data = ["foo.txt"],
extends = "//packages/typescript/test/ts_project:tsconfig-base.json",
tsconfig = "tsconfig.json",
deps = [
"@npm//@types/jasmine",
"@npm//@types/node",
],
)

jasmine_node_test(
name = "test",
srcs = [":data"],
)
13 changes: 13 additions & 0 deletions packages/typescript/test/ts_project/data/data.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {promises as fs} from 'fs';

const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!);

const FOO_PATH =
runfiles.resolve('build_bazel_rules_nodejs/packages/typescript/test/ts_project/data/foo.txt');

describe('data', () => {
it('makes files available at runtime', async () => {
const foo = await fs.readFile(FOO_PATH, {encoding: 'utf8'});
expect(foo).toBe('Foo\n');
});
});
1 change: 1 addition & 0 deletions packages/typescript/test/ts_project/data/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
10 changes: 10 additions & 0 deletions packages/typescript/test/ts_project/data/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"types": ["jasmine", "node"]
},
"references": [
{"path": "./"}
],
"include": ["*.spec.ts"]
}

0 comments on commit ac2097c

Please sign in to comment.