From 86f5c5b6a3991e9a4ed377c1004cd60fcd25b1a5 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 31 Oct 2022 13:17:33 -0700 Subject: [PATCH 1/2] test: add case for filegroup of srcs Part of #23 --- examples/srcs/BUILD.bazel | 52 +++++++++++++++++++++++++++++++++++++ examples/srcs/a.ts | 1 + examples/srcs/b.ts | 1 + examples/srcs/tsconfig.json | 1 + 4 files changed, 55 insertions(+) create mode 100644 examples/srcs/BUILD.bazel create mode 100644 examples/srcs/a.ts create mode 100644 examples/srcs/b.ts create mode 100644 examples/srcs/tsconfig.json diff --git a/examples/srcs/BUILD.bazel b/examples/srcs/BUILD.bazel new file mode 100644 index 00000000..99938561 --- /dev/null +++ b/examples/srcs/BUILD.bazel @@ -0,0 +1,52 @@ +"""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") + +# 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", +) + +# 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", + ] +] + +[ + assert_contains( + name = "test_" + case, + actual = "srcs-{}.mf".format(case), + expected = "examples/srcs/{}/a.js".format(case), + ) + for case in [ + "auto", + "filegroup", + ] +] diff --git a/examples/srcs/a.ts b/examples/srcs/a.ts new file mode 100644 index 00000000..2d0a38f9 --- /dev/null +++ b/examples/srcs/a.ts @@ -0,0 +1 @@ +export const a: string = 'a' diff --git a/examples/srcs/b.ts b/examples/srcs/b.ts new file mode 100644 index 00000000..ea337fd5 --- /dev/null +++ b/examples/srcs/b.ts @@ -0,0 +1 @@ +export const b: string = 'b' diff --git a/examples/srcs/tsconfig.json b/examples/srcs/tsconfig.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/examples/srcs/tsconfig.json @@ -0,0 +1 @@ +{} From f370caecd61234e5f477a058cf2caa800a4117f7 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 31 Oct 2022 13:52:54 -0700 Subject: [PATCH 2/2] code review comments --- examples/simple/BUILD.bazel | 57 +++++++++++++++---------------------- examples/srcs/BUILD.bazel | 30 +++++++++++++++++++ 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/examples/simple/BUILD.bazel b/examples/simple/BUILD.bazel index 8817debd..f2ec86f4 100644 --- a/examples/simple/BUILD.bazel +++ b/examples/simple/BUILD.bazel @@ -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 @@ -21,33 +15,28 @@ ts_project( out_dir = ".", # Note, the tsconfig attribute defaults to the tsconfig.json file in this directory. # tsconfig = "", - 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", ) diff --git a/examples/srcs/BUILD.bazel b/examples/srcs/BUILD.bazel index 99938561..a902f228 100644 --- a/examples/srcs/BUILD.bazel +++ b/examples/srcs/BUILD.bazel @@ -3,6 +3,7 @@ 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. @@ -25,6 +26,27 @@ ts_project( 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( @@ -36,6 +58,7 @@ ts_project( for case in [ "auto", "filegroup", + "generated", ] ] @@ -48,5 +71,12 @@ ts_project( for case in [ "auto", "filegroup", + "generated", ] ] + +assert_contains( + name = "test_gen", + actual = "generated/generated.js", + expected = "exports.data = []", +)