Skip to content

Commit

Permalink
chore: move @types deps to the ts_config target
Browse files Browse the repository at this point in the history
Source code doesn't have to include any indication these 'ambient' types are required.
However we can force users to be explicit in the tsconfig of which `@types` packages to include in compilation.
This follows the locality principle, since the types weren't referenced in the sources, the deps dont belong on the ts_project.
  • Loading branch information
alexeagle committed Jun 11, 2024
1 parent cf28458 commit 593ed6a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
6 changes: 6 additions & 0 deletions frontend/next.js/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ ts_config(
name = "tsconfig",
src = "tsconfig.json",
visibility = ["//visibility:public"],
deps = [
":node_modules/@types/is-even",
":node_modules/@types/jest",
":node_modules/@types/react",
":node_modules/@types/react-dom",
],
)

next(
Expand Down
4 changes: 0 additions & 4 deletions frontend/next.js/pages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ ts_project(
visibility = ["//next.js:__subpackages__"],
deps = [
"//next.js:node_modules/@bazel-example/one",
"//next.js:node_modules/@types/is-even",
"//next.js:node_modules/@types/react",
"//next.js:node_modules/@types/react-dom",
"//next.js:node_modules/next",
"//next.js/pages/api",
],
Expand All @@ -38,7 +35,6 @@ ts_project(
deps = [
"//next.js:node_modules/@testing-library/jest-dom",
"//next.js:node_modules/@testing-library/react",
"//next.js:node_modules/@types/jest",
"//next.js/pages",
],
)
Expand Down
8 changes: 7 additions & 1 deletion frontend/next.js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx"
"jsx": "react-jsx",
"types": [
"is-even",
"jest",
"react",
"react-dom"
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
Expand Down
18 changes: 9 additions & 9 deletions frontend/packages/one/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [":node_modules/@types/is-odd"],
)

ts_project(
name = "one_ts",
srcs = ["src/main.ts"],
# runtime direct dependencies of the linked npm package
data = [
":node_modules/is-odd",
],
data = [":node_modules/is-odd"],
declaration = True,
transpiler = "tsc",
# transpile time direct dependencies
deps = [
":node_modules/@types/is-odd",
"//next.js:node_modules/@types/node",
],
tsconfig = ":tsconfig",
)

# make this library available via node_modules
Expand Down
4 changes: 3 additions & 1 deletion frontend/packages/one/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,

"types": [
"is-odd"
]
}
}
1 change: 1 addition & 0 deletions frontend/react/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ts_config(
name = "tsconfig",
src = "tsconfig.json",
visibility = ["//visibility:public"],
deps = [":node_modules/@types/react"],
)

tsconfig_to_swcconfig.t2s(
Expand Down
3 changes: 1 addition & 2 deletions frontend/react/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ ts_project(
tsconfig = "//react:tsconfig",
visibility = ["//react:__subpackages__"],
deps = [
"//react:node_modules/@types/react",
"//react:node_modules/react",
"//react:node_modules/react-dom",
"//react:node_modules/vite-plugin-svgr",
Expand All @@ -43,7 +42,7 @@ ts_project(
"//:node_modules/vitest",
"//react:node_modules/@testing-library/jest-dom",
"//react:node_modules/@testing-library/react",
"//react:node_modules/@types/jest",
"//react:node_modules/@types",
],
)

Expand Down
5 changes: 4 additions & 1 deletion frontend/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"resolveJsonModule": true,
"isolatedModules": true,
"declaration": true,
"jsx": "react-jsx"
"jsx": "react-jsx",
"types": [
"react"
]
},
"include": ["src"]
}

0 comments on commit 593ed6a

Please sign in to comment.