From 3c9b1f181007a520ea6c01a2d6107d7b8288f75e Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 6 Dec 2024 16:44:30 -0800 Subject: [PATCH] Elide distinction between srcs and assets --- docs/rules.md | 11 +++--- examples/assets/BUILD.bazel | 67 ++++++++++++++++++++++------------ examples/json_data/BUILD.bazel | 2 - ts/defs.bzl | 21 +++++------ ts/private/ts_config.bzl | 2 +- ts/private/ts_lib.bzl | 58 +++++++++++++++-------------- ts/private/ts_project.bzl | 33 +++++++++++------ ts/test/mock_transpiler.bzl | 4 +- 8 files changed, 114 insertions(+), 84 deletions(-) diff --git a/docs/rules.md b/docs/rules.md index 7dbfa0a9..53bf6620 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -39,7 +39,7 @@ extended configuration file as well, to pass them both to the TypeScript compile
 load("@aspect_rules_ts//ts:defs.bzl", "ts_project_rule")
 
-ts_project_rule(name, deps, srcs, data, allow_js, args, assets, buildinfo_out, composite,
+ts_project_rule(name, deps, srcs, data, allow_js, args, asset_outs, buildinfo_out, composite,
                 declaration, declaration_dir, declaration_map, declaration_transpile,
                 emit_declaration_only, extends, incremental, is_typescript_5_or_greater,
                 isolated_typecheck, js_outs, map_outs, no_emit, out_dir, preserve_jsx,
@@ -61,11 +61,11 @@ for srcs and tsconfig, and pre-declaring output files.
 | :------------- | :------------- | :------------- | :------------- | :------------- |
 | name |  A unique name for this target.   | Name | required |  |
 | deps |  List of targets that produce TypeScript typings (`.d.ts` files)

Follows the same runfiles semantics as `js_library` `deps` attribute. See https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more info. | List of labels | optional | `[]` | -| srcs | TypeScript source files | List of labels | required | | +| srcs | TypeScript source files and assets | List of labels | required | | | data | Runtime dependencies to include in binaries/tests that depend on this target.

Follows the same semantics as `js_library` `data` attribute. See https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#data for more info. | List of labels | optional | `[]` | | allow_js | https://www.typescriptlang.org/tsconfig#allowJs | Boolean | optional | `False` | | args | https://www.typescriptlang.org/docs/handbook/compiler-options.html | List of strings | optional | `[]` | -| assets | Files which are needed by a downstream build step such as a bundler.

See more details on the `assets` parameter of the `ts_project` macro. | List of labels | optional | `[]` | +| asset_outs | Locations in bazel-out where ts_project will write asset files | List of labels | optional | `[]` | | buildinfo_out | Location in bazel-out where tsc will write a `.tsbuildinfo` file | Label | optional | `None` | | composite | https://www.typescriptlang.org/tsconfig#composite | Boolean | optional | `False` | | declaration | https://www.typescriptlang.org/tsconfig#declaration | Boolean | optional | `False` | @@ -128,7 +128,7 @@ along with any transitively referenced tsconfig.json files chained by the
 load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
 
-ts_project(name, tsconfig, srcs, args, data, deps, assets, extends, allow_js, isolated_typecheck,
+ts_project(name, tsconfig, srcs, args, data, deps, extends, allow_js, isolated_typecheck,
            declaration, source_map, declaration_map, resolve_json_module, preserve_jsx, composite,
            incremental, no_emit, emit_declaration_only, transpiler, declaration_transpiler,
            ts_build_info_file, tsc, tsc_worker, validate, validator, declaration_dir, out_dir,
@@ -164,11 +164,10 @@ If you have problems getting your `ts_project` to work correctly, read the dedic
 | :------------- | :------------- | :------------- |
 | name |  a name for this target   |  none |
 | tsconfig |  Label of the tsconfig.json file to use for the compilation. To support "chaining" of more than one extended config, this label could be a target that provides `TsConfigInfo` such as `ts_config`.

By default, if a "tsconfig.json" file is in the same folder with the ts_project rule, it is used.

Instead of a label, you can pass a dictionary matching the JSON schema.

See [docs/tsconfig.md](/docs/tsconfig.md) for detailed information. | `None` | -| srcs | List of labels of TypeScript source files to be provided to the compiler.

If absent, the default is set as follows:

- Include all TypeScript files in the package, recursively. - If `allow_js` is set, include all JavaScript files in the package as well. - If `resolve_json_module` is set, include all JSON files in the package, but exclude `package.json`, `package-lock.json`, and `tsconfig*.json`. | `None` | +| srcs | List of labels of TypeScript source files to be provided to the compiler.

If absent, the default is set as follows:

- Include all TypeScript files in the package, recursively. - If `allow_js` is set, include all JavaScript files in the package as well. - If `resolve_json_module` is set, include all JSON files in the package, but exclude `package.json`, `package-lock.json`, and `tsconfig*.json`.

Non-TypeScript and JavaScript assets may be included; they will be copied directly to the outputs. As with transpiled outputs, the `out_dir` and `root_dir` attributes determine where such assets are written. A typical use is when your TypeScript code has an import that TS itself doesn't understand, such as

`import './my.scss'`

and the type-checker allows this because you have an "ambient" global type declaration like

`declare module '*.scss' { ... }`

A bundler like webpack will expect to be able to resolve the `./my.scss` import to a file and doesn't care about the typing declaration. A bundler runs as a build step, so it does not see files included in the `data` attribute.

Note that `data` is used for files that are resolved by some binary, including a test target. Behind the scenes, `data` populates Bazel's Runfiles object in `DefaultInfo`, while assets populate the `transitive_sources` of the `JsInfo`. | `None` | | args | List of strings of additional command-line arguments to pass to tsc. See https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options Typically useful arguments for debugging are `--listFiles` and `--listEmittedFiles`. | `[]` | | data | Files needed at runtime by binaries or tests that transitively depend on this target. See https://bazel.build/reference/be/common-definitions#typical-attributes | `[]` | | deps | List of targets that produce TypeScript typings (`.d.ts` files)

If this list contains linked npm packages, npm package store targets or other targets that provide `JsInfo`, `NpmPackageStoreInfo` providers are gathered from `JsInfo`. This is done directly from the `npm_package_store_deps` field of these. For linked npm package targets, the underlying `npm_package_store` target(s) that back the links is used. Gathered `NpmPackageStoreInfo` providers are propagated to the direct dependencies of downstream linked `npm_package` targets.

NB: Linked npm package targets that are "dev" dependencies do not forward their underlying `npm_package_store` target(s) through `npm_package_store_deps` and will therefore not be propagated to the direct dependencies of downstream linked `npm_package` targets. npm packages that come in from `npm_translate_lock` are considered "dev" dependencies if they are have `dev: true` set in the pnpm lock file. This should be all packages that are only listed as "devDependencies" in all `package.json` files within the pnpm workspace. This behavior is intentional to mimic how `devDependencies` work in published npm packages. | `[]` | -| assets | Files which are needed by a downstream build step such as a bundler.

These files are **not** included as inputs to any actions spawned by `ts_project`. They are not transpiled, and are not visible to the type-checker. Instead, these files appear among the *outputs* of this target.

A typical use is when your TypeScript code has an import that TS itself doesn't understand such as

`import './my.scss'`

and the type-checker allows this because you have an "ambient" global type declaration like

`declare module '*.scss' { ... }`

A bundler like webpack will expect to be able to resolve the `./my.scss` import to a file and doesn't care about the typing declaration. A bundler runs as a build step, so it does not see files included in the `data` attribute.

Note that `data` is used for files that are resolved by some binary, including a test target. Behind the scenes, `data` populates Bazel's Runfiles object in `DefaultInfo`, while this attribute populates the `transitive_sources` of the `JsInfo`. | `[]` | | extends | Label of the tsconfig file referenced in the `extends` section of tsconfig To support "chaining" of more than one extended config, this label could be a target that provdes `TsConfigInfo` such as `ts_config`. | `None` | | allow_js | Whether TypeScript will read .js and .jsx files. When used with `declaration`, TypeScript will generate `.d.ts` files from `.js` files. | `False` | | isolated_typecheck | Whether to type-check asynchronously as a separate bazel action. Requires https://devblogs.microsoft.com/typescript/announcing-typescript-5-6/#the---nocheck-option6 Requires https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html#isolated-declarations | `False` | diff --git a/examples/assets/BUILD.bazel b/examples/assets/BUILD.bazel index c1996867..552de852 100644 --- a/examples/assets/BUILD.bazel +++ b/examples/assets/BUILD.bazel @@ -22,11 +22,9 @@ ts_config( ts_project( name = "ts", srcs = [ + "src/generated.json", "src/index.ts", - ], - assets = [ "src/styles.css", - "src/generated.json", ], tsconfig = ":config", ) @@ -34,11 +32,9 @@ ts_project( ts_project( name = "ts-out", srcs = [ + "src/generated.json", "src/index.ts", - ], - assets = [ "src/styles.css", - "src/generated.json", ], extends = ":config", out_dir = "out", @@ -47,8 +43,10 @@ ts_project( ts_project( name = "ts-runtime-assets", - srcs = ["src/index-runtime.ts"], - assets = ["src/generated.json"], + srcs = [ + "src/generated.json", + "src/index-runtime.ts", + ], tsconfig = ":config", ) @@ -60,17 +58,19 @@ js_test( ts_project( name = "ts-runtime-outdir-assets", - srcs = ["src/index-runtime.ts"], - assets = ["src/generated.json"], + srcs = [ + "src/generated.json", + "src/index-runtime.ts", + ], extends = ":config", - out_dir = "out", - tsconfig = {"compilerOptions": {"outDir": "out"}}, + out_dir = "runtime-out", + tsconfig = {"compilerOptions": {"outDir": "runtime-out"}}, ) js_test( name = "ts-runtime-outdir-assets_test", data = [":ts-runtime-outdir-assets"], - entry_point = "out/src/index-runtime.js", + entry_point = "runtime-out/src/index-runtime.js", ) filegroup( @@ -85,8 +85,8 @@ ts_project( name = "ts-out-target", srcs = [ "src/index.ts", + ":assets-filegroup", ], - assets = [":assets-filegroup"], out_dir = "out-target", tsconfig = ":config", ) @@ -113,8 +113,8 @@ ts_project( name = "ts-out-copy-target", srcs = [ "src/index.ts", + ":assets-filegroup", ], - assets = [":assets-filegroup"], out_dir = "out-copy-target", tsconfig = ":config", ) @@ -133,8 +133,6 @@ ts_project( name = "ts-out-copy-target-abs", srcs = [ "src/index.ts", - ], - assets = [ "//%s:assets-filegroup" % package_name(), ], out_dir = "out-copy-target-abs", @@ -152,13 +150,38 @@ assert_outputs( ) ts_project( - name = "ts-root", + name = "ts-typings-out", srcs = [ "src/index.ts", + "types/typings.d.ts", ], - assets = [ - "src/styles.css", + out_dir = "typings-out", + declaration_dir = "typings-out-declarations", + tsconfig = ":config", +) + +assert_outputs( + name = "ts-typings-out_outputs_test", + actual = ":ts-typings-out", + expected = [ + "examples/assets/typings-out/src/index.js", + ], +) + +assert_outputs( + name = "ts-typings-out-typings_outputs_test", + actual = ":ts-typings-out_types", + expected = [ + "examples/assets/typings-out-declarations/types/typings.d.ts", + ], +) + +ts_project( + name = "ts-root", + srcs = [ "src/generated.json", + "src/index.ts", + "src/styles.css", ], root_dir = "src", tsconfig = ":config", @@ -167,11 +190,9 @@ ts_project( ts_project( name = "ts-root-out", srcs = [ + "src/generated.json", "src/index.ts", - ], - assets = [ "src/styles.css", - "src/generated.json", ], out_dir = "root-out", root_dir = "src", diff --git a/examples/json_data/BUILD.bazel b/examples/json_data/BUILD.bazel index a5f77e16..d19f310c 100644 --- a/examples/json_data/BUILD.bazel +++ b/examples/json_data/BUILD.bazel @@ -7,8 +7,6 @@ ts_project( srcs = [ "src/index.ts", "src/src.json", - ], - assets = [ "src/asset.txt", ], data = [ diff --git a/ts/defs.bzl b/ts/defs.bzl index 9ff154d6..2ad8b0c8 100644 --- a/ts/defs.bzl +++ b/ts/defs.bzl @@ -36,7 +36,6 @@ def ts_project( args = [], data = [], deps = [], - assets = [], extends = None, allow_js = False, isolated_typecheck = False, @@ -94,14 +93,10 @@ def ts_project( - If `resolve_json_module` is set, include all JSON files in the package, but exclude `package.json`, `package-lock.json`, and `tsconfig*.json`. - assets: Files which are needed by a downstream build step such as a bundler. - - These files are **not** included as inputs to any actions spawned by `ts_project`. - They are not transpiled, and are not visible to the type-checker. - Instead, these files appear among the *outputs* of this target. - - A typical use is when your TypeScript code has an import that TS itself doesn't understand - such as + Non-TypeScript and JavaScript assets may be included; they will be copied directly + to the outputs. As with transpiled outputs, the `out_dir` and `root_dir` attributes + determine where such assets are written. A typical use is when your TypeScript code + has an import that TS itself doesn't understand, such as `import './my.scss'` @@ -115,7 +110,7 @@ def ts_project( Note that `data` is used for files that are resolved by some binary, including a test target. Behind the scenes, `data` populates Bazel's Runfiles object in `DefaultInfo`, - while this attribute populates the `transitive_sources` of the `JsInfo`. + while assets populate the `transitive_sources` of the `JsInfo`. data: Files needed at runtime by binaries or tests that transitively depend on this target. See https://bazel.build/reference/be/common-definitions#typical-attributes @@ -353,9 +348,11 @@ def ts_project( tsc_js_outs = [] tsc_map_outs = [] if emit_tsc_js: - tsc_js_outs = _lib.calculate_js_outs(srcs, out_dir, root_dir, allow_js, resolve_json_module, preserve_jsx, emit_declaration_only) + tsc_js_outs = _lib.calculate_js_outs(srcs, out_dir, root_dir, allow_js, preserve_jsx, emit_declaration_only) tsc_map_outs = _lib.calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, emit_declaration_only) + asset_outs = _lib.calculate_asset_outs(srcs, out_dir, typings_out_dir, root_dir, allow_js) + # Custom typing transpiler if emit_transpiler_dts: declarations_target_name = "%s_declarations" % name @@ -425,7 +422,6 @@ def ts_project( name = name, srcs = srcs, args = args, - assets = assets, data = data, deps = tsc_deps, tsconfig = tsconfig, @@ -447,6 +443,7 @@ def ts_project( map_outs = tsc_map_outs, typings_outs = tsc_typings_outs, typing_maps_outs = tsc_typing_maps_outs, + asset_outs = asset_outs, buildinfo_out = tsbuildinfo_path if composite or incremental else None, no_emit = no_emit, emit_declaration_only = emit_declaration_only, diff --git a/ts/private/ts_config.bzl b/ts/private/ts_config.bzl index 7ff34400..c8f8c92f 100644 --- a/ts/private/ts_config.bzl +++ b/ts/private/ts_config.bzl @@ -140,7 +140,7 @@ def _write_tsconfig_rule(ctx): src_files = [] for f in ctx.files.files: # Only include typescript source files - if not _lib.is_ts_src(f.basename, ctx.attr.allow_js, ctx.attr.resolve_json_module, True): + if not _lib.is_ts_src(f.basename, ctx.attr.allow_js, True): continue if f.short_path.startswith(local_package_prefix): diff --git a/ts/private/ts_lib.bzl b/ts/private/ts_lib.bzl index 206df138..0c3dd9dc 100644 --- a/ts/private/ts_lib.bzl +++ b/ts/private/ts_lib.bzl @@ -2,13 +2,6 @@ # Attributes common to all TypeScript rules STD_ATTRS = { - "assets": attr.label_list( - doc = """Files which are needed by a downstream build step such as a bundler. - -See more details on the `assets` parameter of the `ts_project` macro. -""", - allow_files = True, - ), "args": attr.string_list( doc = "https://www.typescriptlang.org/docs/handbook/compiler-options.html", ), @@ -36,12 +29,8 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more "root_dir": attr.string( doc = "https://www.typescriptlang.org/tsconfig#rootDir", ), - # NB: no restriction on extensions here, because tsc sometimes adds type-check support - # for more file kinds (like require('some.json')) and also - # if you swap out the `compiler` attribute (like with ngtsc) - # that compiler might allow more sources than tsc does. "srcs": attr.label_list( - doc = "TypeScript source files", + doc = "TypeScript source files and assets", allow_files = True, mandatory = True, ), @@ -183,6 +172,9 @@ OUTPUT_ATTRS = { "typings_outs": attr.output_list( doc = "Locations in bazel-out where tsc will write `.d.ts` files", ), + "asset_outs": attr.output_list( + doc = "Locations in bazel-out where ts_project will write asset files", + ), } def _join(*elements): @@ -202,20 +194,16 @@ def _relative_to_package(path, ctx): def _is_typings_src(src): return src.endswith(".d.ts") or src.endswith(".d.mts") or src.endswith(".d.cts") -def _is_js_src(src, allow_js, resolve_json_module): +def _is_js_src(src, allow_js): if allow_js and (src.endswith(".js") or src.endswith(".jsx") or src.endswith(".mjs") or src.endswith(".cjs")): return True - - if resolve_json_module and src.endswith(".json"): - return True - return False -def _is_ts_src(src, allow_js, resolve_json_module, include_typings): +def _is_ts_src(src, allow_js, include_typings): if src.endswith(".ts") or src.endswith(".tsx") or src.endswith(".mts") or src.endswith(".cts"): return include_typings or not _is_typings_src(src) - return _is_js_src(src, allow_js, resolve_json_module) + return _is_js_src(src, allow_js) def _to_out_path(f, out_dir, root_dir): f = f[f.find(":") + 1:] @@ -225,10 +213,10 @@ def _to_out_path(f, out_dir, root_dir): f = out_dir + "/" + f return f -def _to_js_out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, ext_map, default_ext): +def _to_js_out_paths(srcs, out_dir, root_dir, allow_js, ext_map, default_ext): outs = [] for f in srcs: - if _is_ts_src(f, allow_js, resolve_json_module, False): + if _is_ts_src(f, allow_js, False): out = _to_out_path(f, out_dir, root_dir) ext_idx = out.rindex(".") out = out[:ext_idx] + ext_map.get(out[ext_idx:], default_ext) @@ -239,6 +227,22 @@ def _to_js_out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, ext outs.append(out) return outs +# Macros can't reliably distinguish between labels and paths, but we can make a guess. +def _is_likely_label(f): + return f.find(":") != -1 or f.startswith("//") or f.startswith("@") + +def _calculate_asset_outs(srcs, out_dir, typings_out_dir, root_dir, allow_js): + outs = [] + for f in srcs: + # We must avoid predeclaring asset outputs for labels, because the label name is + # not guaranteed to bear any relation to the actual names of the output assets. + if not _is_ts_src(f, allow_js, False) and not _is_likely_label(f): + out = _to_out_path(f, typings_out_dir if _is_typings_src(f) else out_dir, root_dir) + # Don't declare outputs that collide with inputs + if out != f: + outs.append(out) + return outs + # Quick check to validate path options # One usecase: https://github.com/aspect-build/rules_ts/issues/551 def _validate_tsconfig_dirs(root_dir, out_dir, typings_out_dir): @@ -251,7 +255,7 @@ def _validate_tsconfig_dirs(root_dir, out_dir, typings_out_dir): if typings_out_dir and typings_out_dir.find("../") != -1: fail("typings_out_dir cannot output to parent directory") -def _calculate_js_outs(srcs, out_dir, root_dir, allow_js, resolve_json_module, preserve_jsx, emit_declaration_only): +def _calculate_js_outs(srcs, out_dir, root_dir, allow_js, preserve_jsx, emit_declaration_only): if emit_declaration_only: return [] @@ -260,14 +264,13 @@ def _calculate_js_outs(srcs, out_dir, root_dir, allow_js, resolve_json_module, p ".mjs": ".mjs", ".cjs": ".cjs", ".cts": ".cjs", - ".json": ".json", } if preserve_jsx: exts[".jsx"] = ".jsx" exts[".tsx"] = ".jsx" - return _to_js_out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, exts, ".js") + return _to_js_out_paths(srcs, out_dir, root_dir, allow_js, exts, ".js") def _calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, emit_declaration_only): if not source_map or emit_declaration_only: @@ -282,7 +285,7 @@ def _calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, emit_ if preserve_jsx: exts[".tsx"] = ".jsx.map" - return _to_js_out_paths(srcs, out_dir, root_dir, False, False, exts, ".js.map") + return _to_js_out_paths(srcs, out_dir, root_dir, False, exts, ".js.map") def _calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, composite, allow_js): if not (declaration or composite): @@ -295,7 +298,7 @@ def _calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, compos ".cjs": ".d.cts", } - return _to_js_out_paths(srcs, typings_out_dir, root_dir, allow_js, False, exts, ".d.ts") + return _to_js_out_paths(srcs, typings_out_dir, root_dir, allow_js, exts, ".d.ts") def _calculate_typing_maps_outs(srcs, typings_out_dir, root_dir, declaration_map, allow_js): if not declaration_map: @@ -308,7 +311,7 @@ def _calculate_typing_maps_outs(srcs, typings_out_dir, root_dir, declaration_map ".cjs": ".d.cts.map", } - return _to_js_out_paths(srcs, typings_out_dir, root_dir, allow_js, False, exts, ".d.ts.map") + return _to_js_out_paths(srcs, typings_out_dir, root_dir, allow_js, exts, ".d.ts.map") def _calculate_root_dir(ctx): return _join( @@ -337,5 +340,6 @@ lib = struct( calculate_map_outs = _calculate_map_outs, calculate_typings_outs = _calculate_typings_outs, calculate_typing_maps_outs = _calculate_typing_maps_outs, + calculate_asset_outs = _calculate_asset_outs, calculate_root_dir = _calculate_root_dir, ) diff --git a/ts/private/ts_project.bzl b/ts/private/ts_project.bzl index 702aa552..26179885 100644 --- a/ts/private/ts_project.bzl +++ b/ts/private/ts_project.bzl @@ -67,7 +67,7 @@ def _ts_project_impl(ctx): js_outs = [] map_outs = [] if not ctx.attr.no_emit and ctx.attr.transpile != 0: - js_outs = _lib.declare_outputs(ctx, _lib.calculate_js_outs(srcs, ctx.attr.out_dir, ctx.attr.root_dir, ctx.attr.allow_js, ctx.attr.resolve_json_module, ctx.attr.preserve_jsx, ctx.attr.emit_declaration_only)) + js_outs = _lib.declare_outputs(ctx, _lib.calculate_js_outs(srcs, ctx.attr.out_dir, ctx.attr.root_dir, ctx.attr.allow_js, ctx.attr.preserve_jsx, ctx.attr.emit_declaration_only)) map_outs = _lib.declare_outputs(ctx, _lib.calculate_map_outs(srcs, ctx.attr.out_dir, ctx.attr.root_dir, ctx.attr.source_map, ctx.attr.preserve_jsx, ctx.attr.emit_declaration_only)) # dts+map file outputs @@ -160,15 +160,26 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details. transitive_inputs.extend(tsconfig_transitive_deps) assets_outs = [] - for a in ctx.files.assets: + typing_assets_outs = [] + for a in ctx.files.srcs: a_path = _lib.relative_to_package(a.short_path, ctx) - a_out = _lib.to_out_path(a_path, ctx.attr.out_dir, ctx.attr.root_dir) - if a.is_source or a_path != a_out: - asset = ctx.actions.declare_file(a_out) - copy_file_action(ctx, a, asset) - assets_outs.append(asset) - else: - assets_outs.append(a) + if not _lib.is_ts_src(a_path, ctx.attr.allow_js, False): + if _lib.is_typings_src(a_path): + a_out = _lib.to_out_path(a_path, typings_out_dir, ctx.attr.root_dir) + if a.is_source or a_path != a_out: + asset = ctx.actions.declare_file(a_out) + copy_file_action(ctx, a, asset) + typing_assets_outs.append(asset) + else: + typing_assets_outs.append(a) + else: + a_out = _lib.to_out_path(a_path, ctx.attr.out_dir, ctx.attr.root_dir) + if a.is_source or a_path != a_out: + asset = ctx.actions.declare_file(a_out) + copy_file_action(ctx, a, asset) + assets_outs.append(asset) + else: + assets_outs.append(a) outputs = js_outs + map_outs + typings_outs + typing_maps_outs if ctx.outputs.buildinfo_out: @@ -176,7 +187,7 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details. outputs.append(ctx.outputs.buildinfo_out) output_sources = js_outs + map_outs + assets_outs + ctx.files.pretranspiled_js - output_types = typings_outs + typing_maps_outs + ctx.files.pretranspiled_dts + output_types = typings_outs + typing_maps_outs + typing_assets_outs + ctx.files.pretranspiled_dts # Add JS inputs that collide with outputs (see #250). # @@ -189,7 +200,7 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details. # (so it also works in the macro), but we need Files here. if ctx.attr.out_dir == ctx.attr.root_dir: for s in srcs_inputs: - if _lib.is_js_src(s.path, ctx.attr.allow_js, ctx.attr.resolve_json_module): + if _lib.is_js_src(s.path, ctx.attr.allow_js): output_sources.append(s) if _lib.is_typings_src(s.path): output_types.append(s) diff --git a/ts/test/mock_transpiler.bzl b/ts/test/mock_transpiler.bzl index 5f141558..86c4d720 100644 --- a/ts/test/mock_transpiler.bzl +++ b/ts/test/mock_transpiler.bzl @@ -53,7 +53,7 @@ def mock(name, srcs, source_map = False, **kwargs): srcs = srcs, # Calculate pre-declared outputs so they can be referenced as targets. # This is an optional transpiler feature aligning with the default tsc transpiler. - js_outs = lib.calculate_js_outs(srcs, ".", ".", False, False, False, False), - map_outs = lib.calculate_map_outs(srcs, ".", ".", True, False, False) if source_map else [], + js_outs = lib.calculate_js_outs(srcs, ".", ".", False, False, False), + map_outs = lib.calculate_map_outs(srcs, ".", ".", True, False) if source_map else [], **kwargs )