Skip to content

Commit

Permalink
fix: align ts_project(data) runfiles with js_library(data)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Oct 21, 2024
1 parent a058043 commit 41a2678
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/js_lib/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "number": 1 }
2 changes: 1 addition & 1 deletion examples/js_lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = 1
module.exports = require('./data.json').number
55 changes: 55 additions & 0 deletions examples/json_data/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:build_test.bzl", "build_test")

ts_project(
name = "tsc",
srcs = [
"src/index.ts",
"src/src.json",
],
assets = [
"src/asset.txt",
],
data = [
"src/tsdata.json",
"src/tsdata.txt",
":fg",
],
resolve_json_module = True,
tsconfig = {
"compilerOptions": {
"resolveJsonModule": True,
},
},
deps = [
"//examples:node_modules/@types/node",
],
)

filegroup(
name = "fg",
srcs = [
"src/fg.json",
"src/fg.txt",
],
)

js_binary(
name = "bin1",
data = [":tsc"],
entry_point = "src/index.js",
)

js_run_binary(
name = "run1",
srcs = [],
outs = ["src/all.txt"],
args = ["%s/src" % package_name()],
tool = ":bin1",
)

build_test(
name = "test",
targets = ["src/all.txt"],
)
1 change: 1 addition & 0 deletions examples/json_data/src/asset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"asset": 42}
1 change: 1 addition & 0 deletions examples/json_data/src/fg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "fg": 42 }
1 change: 1 addition & 0 deletions examples/json_data/src/fg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"fg": 42}
16 changes: 16 additions & 0 deletions examples/json_data/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fs = require('fs')
const path = require('path')

const dataFiles = [
'./asset.txt',
'./fg.json',
'./fg.txt',
'./src.json',
'./tsdata.json',
'./tsdata.txt',
]

fs.writeFileSync(
path.join(process.argv[2], 'all.txt'),
dataFiles.map((f) => fs.readFileSync(path.join(__dirname, f))).join('')
)
1 change: 1 addition & 0 deletions examples/json_data/src/src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "asset": 42 }
1 change: 1 addition & 0 deletions examples/json_data/src/tsdata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "data": 42 }
1 change: 1 addition & 0 deletions examples/json_data/src/tsdata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data": 42}
4 changes: 3 additions & 1 deletion examples/proto_grpc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ ts_proto_library(
ts_project(
name = "proto_grpc",
srcs = ["main.ts"],
data = [
"package.json", # for type=module
],
deps = [
":logger_ts_proto",
":status_ts_proto",
Expand All @@ -59,7 +62,6 @@ ts_project(
js_test(
name = "main",
data = [
"package.json", # for type=module
"proto_grpc",
],
entry_point = "main.js",
Expand Down
10 changes: 10 additions & 0 deletions ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,21 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
output_types_depset = depset(output_types)
output_sources_depset = depset(output_sources)

# Align runfiles config with rules_js js_library() to align behaviour.
# See https://github.com/aspect-build/rules_js/blob/v2.1.0/js/private/js_library.bzl#L241-L254
runfiles = js_lib_helpers.gather_runfiles(
ctx = ctx,
sources = output_sources_depset,
data = ctx.attr.data,
deps = srcs_tsconfig_deps,
data_files = ctx.files.data,
copy_data_files_to_bin = True, # NOTE: configurable (default true) in js_library()
no_copy_to_bin = [], # NOTE: configurable (default []) in js_library()
include_sources = True,
include_types = False,
include_transitive_sources = True,
include_transitive_types = False,
include_npm_sources = True,
)

providers = [
Expand Down

0 comments on commit 41a2678

Please sign in to comment.