From 01915eea5345c247162a12b8184fc925a383e3b0 Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Tue, 15 Apr 2025 15:37:35 -0700 Subject: [PATCH] fix(bazel): handle `*.mts` / `*.cts` extensions in strict deps test --- bazel/ts_project/strict_deps/index.mts | 2 +- .../import_from_mts_cts_extensions/BUILD.bazel | 18 ++++++++++++++++++ .../common_extension.cts | 1 + .../import_from_mts_cts_extensions/index.ts | 5 +++++ .../module_extension.mts | 1 + .../tsconfig.json | 7 +++++++ 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/BUILD.bazel create mode 100644 bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/common_extension.cts create mode 100644 bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/index.ts create mode 100644 bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/module_extension.mts create mode 100644 bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/tsconfig.json diff --git a/bazel/ts_project/strict_deps/index.mts b/bazel/ts_project/strict_deps/index.mts index 7364b6fc6..448bbaad0 100644 --- a/bazel/ts_project/strict_deps/index.mts +++ b/bazel/ts_project/strict_deps/index.mts @@ -14,7 +14,7 @@ const manifest: StrictDepsManifest = JSON.parse(await fs.readFile(manifestExecPa * Regex matcher to extract a npm package name, potentially with scope from a subpackage import path. */ const moduleSpeciferMatcher = /^(@[\w\d-_]+\/)?([\w\d-_]+)/; -const extensionRemoveRegex = /\.[mc]?(js|(d\.)?ts)$/; +const extensionRemoveRegex = /\.[mc]?(js|(d\.)?[mc]?ts)$/; const allowedModuleNames = new Set(manifest.allowedModuleNames); const allowedSources = new Set( manifest.allowedSources.map((s) => s.replace(extensionRemoveRegex, '')), diff --git a/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/BUILD.bazel b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/BUILD.bazel new file mode 100644 index 000000000..6e4ec953e --- /dev/null +++ b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/BUILD.bazel @@ -0,0 +1,18 @@ +load("@aspect_rules_ts//ts:defs.bzl", "ts_project") +load("//bazel/ts_project/strict_deps:index.bzl", "strict_deps_test") + +strict_deps_test( + name = "import_from_mts_cts_extensions", + srcs = ["index.ts"], + deps = [":mts_cts_extensions"], +) + +ts_project( + name = "mts_cts_extensions", + srcs = [ + "common_extension.cts", + "module_extension.mts", + ], + declaration = True, + deps = ["//bazel:node_modules/@types/node"], +) diff --git a/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/common_extension.cts b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/common_extension.cts new file mode 100644 index 000000000..93fb8c83f --- /dev/null +++ b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/common_extension.cts @@ -0,0 +1 @@ +export const commonValue = 42; diff --git a/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/index.ts b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/index.ts new file mode 100644 index 000000000..3e85a7795 --- /dev/null +++ b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/index.ts @@ -0,0 +1,5 @@ +export {commonValue} from './common_extension.cjs'; + +// This file compiles to CommonJS, so it needs a dynamic import for TS +// to allow a dependency on an ESM file. +export const mod = import('./module_extension.mjs'); diff --git a/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/module_extension.mts b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/module_extension.mts new file mode 100644 index 000000000..d9ae53971 --- /dev/null +++ b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/module_extension.mts @@ -0,0 +1 @@ +export const moduleValue = 42; diff --git a/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/tsconfig.json b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/tsconfig.json new file mode 100644 index 000000000..94205b059 --- /dev/null +++ b/bazel/ts_project/strict_deps/test/import_from_mts_cts_extensions/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "strict": true, + "lib": ["esnext", "DOM"], + "declaration": true + } +}