diff --git a/examples/global_consumer/BUILD.bazel b/examples/global_consumer/BUILD.bazel new file mode 100644 index 00000000..a8788436 --- /dev/null +++ b/examples/global_consumer/BUILD.bazel @@ -0,0 +1,24 @@ +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files") +load("@aspect_rules_ts//ts:defs.bzl", "ts_project") +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +ts_project( + name = "lib", + srcs = [ + "index.ts", + "//examples/global_dts_generated_tsconfig:lib_types", + ], + tsconfig = {}, +) + +build_test( + name = "test", + targets = [":lib"], +) + +write_source_files( + name = "generated_tsconfig", + files = { + "tsconfig_gen.json.expected": ":_gen_tsconfig_lib", + }, +) diff --git a/examples/global_consumer/index.ts b/examples/global_consumer/index.ts new file mode 100644 index 00000000..1ebe5bf0 --- /dev/null +++ b/examples/global_consumer/index.ts @@ -0,0 +1,3 @@ +if (myGlobalFunction(42) != myGlobalResult) { + console.log('Not equal!?') +} diff --git a/examples/global_consumer/tsconfig_gen.json.expected b/examples/global_consumer/tsconfig_gen.json.expected new file mode 100644 index 00000000..aaf1e17d --- /dev/null +++ b/examples/global_consumer/tsconfig_gen.json.expected @@ -0,0 +1 @@ +{"compilerOptions":{"allowJs":false,"declaration":false,"declarationMap":false,"emitDeclarationOnly":false,"noEmit":false,"resolveJsonModule":null,"sourceMap":false},"files":["./index.ts", "./../../examples/global_dts_generated_tsconfig/index.d.ts", "./../../examples/global_dts_generated_tsconfig/global.d.ts"]} \ No newline at end of file diff --git a/examples/global_dts_generated_tsconfig/BUILD.bazel b/examples/global_dts_generated_tsconfig/BUILD.bazel index 71e079f5..904c1d7d 100644 --- a/examples/global_dts_generated_tsconfig/BUILD.bazel +++ b/examples/global_dts_generated_tsconfig/BUILD.bazel @@ -1,3 +1,4 @@ +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files") load("@aspect_rules_ts//ts:defs.bzl", "ts_project") load("@bazel_skylib//rules:build_test.bzl", "build_test") @@ -9,7 +10,7 @@ ts_project( ], # Intentionally a generated config to catch regressions of # https://github.com/aspect-build/rules_ts/issues/204 - tsconfig = {"declaration": True}, + tsconfig = {"compilerOptions": {"declaration": True}}, visibility = ["//examples:__subpackages__"], ) @@ -17,3 +18,10 @@ build_test( name = "test", targets = [":lib"], ) + +write_source_files( + name = "generated_tsconfig", + files = { + "tsconfig_gen.json.expected": ":_gen_tsconfig_lib", + }, +) diff --git a/examples/global_dts_generated_tsconfig/global.d.ts b/examples/global_dts_generated_tsconfig/global.d.ts index 793acd22..9975c87d 100644 --- a/examples/global_dts_generated_tsconfig/global.d.ts +++ b/examples/global_dts_generated_tsconfig/global.d.ts @@ -1 +1,3 @@ +// A global definition within a .d.ts file + declare function myGlobalFunction(a: number): string diff --git a/examples/global_dts_generated_tsconfig/index.ts b/examples/global_dts_generated_tsconfig/index.ts index 5472e85a..e754b444 100644 --- a/examples/global_dts_generated_tsconfig/index.ts +++ b/examples/global_dts_generated_tsconfig/index.ts @@ -1 +1,11 @@ -myGlobalFunction(42) +;(typeof globalThis !== 'undefined' + ? globalThis + : (window as any) +).myGlobalResult = myGlobalFunction(42) + +export {} + +// A definition within a .ts file that must be compiled. +declare global { + const myGlobalResult: string +} diff --git a/examples/global_dts_generated_tsconfig/tsconfig_gen.json.expected b/examples/global_dts_generated_tsconfig/tsconfig_gen.json.expected new file mode 100644 index 00000000..75fec4c1 --- /dev/null +++ b/examples/global_dts_generated_tsconfig/tsconfig_gen.json.expected @@ -0,0 +1 @@ +{"compilerOptions":{"allowJs":false,"declaration":true,"declarationMap":false,"emitDeclarationOnly":false,"noEmit":false,"resolveJsonModule":null,"sourceMap":false},"files":["./global.d.ts", "./index.ts"]} \ No newline at end of file diff --git a/ts/private/ts_config.bzl b/ts/private/ts_config.bzl index 888d0acf..b37bd470 100644 --- a/ts/private/ts_config.bzl +++ b/ts/private/ts_config.bzl @@ -114,14 +114,6 @@ extended configuration file as well, to pass them both to the TypeScript compile toolchains = COPY_FILE_TO_BIN_TOOLCHAINS, ) -def _filter_input_files(files, allow_js, resolve_json_module): - return [ - f - for f in files - # include typescript, json & declaration sources - if _lib.is_ts_src(f.basename, allow_js, resolve_json_module) or _lib.is_typings_src(f.basename) - ] - def _write_tsconfig_rule(ctx): # TODO: is it useful to expand Make variables in the content? content = ctx.attr.content @@ -135,22 +127,30 @@ def _write_tsconfig_rule(ctx): extends_path = "./" + extends_path content = content.replace("__extends__", extends_path) - filtered_files = _filter_input_files(ctx.files.files, ctx.attr.allow_js, ctx.attr.resolve_json_module) - - # Update file paths to be relative to the tsconfig file, including a ./ prefix - # to ensure paths are all relative to the config file. - package_prefix = ctx.label.package + "/" - - # If the target is inside another workspace, we will need to also add the workspace - # root to the prefix. + # The prefix of source files that are within the same package as the tsconfig file. + local_package_prefix = "%s/" % ctx.label.package if ctx.label.package else "" if (len(ctx.label.repo_name) > 0): - package_prefix = "../{}/{}".format(ctx.label.repo_name, package_prefix) - filtered_files = [ - "./" + f.short_path.removeprefix(package_prefix) - for f in filtered_files - ] - - content = content.replace("\"__files__\"", str(filtered_files)) + # If the target is inside another workspace the prefix also contains the navigation to that workspace. + local_package_prefix = "../{}/{}".format(ctx.label.repo_name, local_package_prefix) + + # The path to navigate to the root of the workspace + path_to_root = "/".join([".."] * (ctx.label.package.count("/") + 1)) + + # Compute the list of source files with paths relative to the generated tsconfig file. + 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) or _lib.is_typings_src(f.basename)): + continue + + if f.short_path.startswith(local_package_prefix): + # Files within this project or subdirs can avoid the ugly ../ prefix + src_files.append("./{}".format(f.short_path.removeprefix(local_package_prefix))) + else: + # Files from parent/sibling projects must navigate up to the workspace root + src_files.append("./{}/{}".format(path_to_root, f.short_path)) + + content = content.replace("\"__files__\"", str(src_files)) ctx.actions.write( output = ctx.outputs.out, content = content,