setup-ts-deep-modules
配置 TypeScript 深模块
Section titled “配置 TypeScript 深模块”让仓库里的每个 package 都成为一个深模块:大量行为藏在小接口后面。package 的公共表面是它的入口文件(位于 package 根目录的文件),子文件夹里的内容都隐藏起来。这个 skill 会安装 dependency-cruiser 并配置相应规则,让外部只能通过入口文件访问 package,最后验证规则确实生效。
关于深模块、接口、接缝和深度这些术语,请调用 Skill 工具「codebase-design」,并在全文使用其中的表述。
这套规则要求的结构
Section titled “这套规则要求的结构”src/packages/ <name>/ index.ts ← 入口文件(公共) client.ts ← 另一个入口文件。package 可以公开多个入口 lib/ ← 实现代码:对外部隐藏,package 内部可以互相导入 tests/ ← 同目录测试和 fixture(子文件夹,因此是私有的)公共表面是 package 的所有根文件,不是某个指定的 index.ts。按惯例,实现代码放在 lib/,测试放在 tests/,这样每个 package 都有相同的双文件夹结构。不过规则本身是通用的:任何子文件夹中的内容都是私有的,所以不必为了新增文件夹去扩展配置。
共四条规则,全部设为 error:
- 入口边界:package 外部的代码(应用代码或其他 package)只能导入该 package 的入口文件(根文件),不能导入它子文件夹中的内容。
- package 内部可自由导入:package 自己的文件可以互相导入。
- 测试通过入口访问:
<pkg>/tests/下的文件可以导入任何 package 的入口文件,以及自己tests/里的 fixture,但不能导入任何 package 子文件夹中的内部文件,包括自己的。跨 package 的集成测试没问题,但不能深度导入。 - 禁止循环依赖:依赖之间不能形成循环。
入口文件,不是汇总文件。 package 的公共表面是所有根文件,因此可以公开多个小入口文件(index.ts、client.ts、server.ts),不必把所有内容都塞进一个庞大的 index.ts。不建议用重新导出整个子树的汇总文件;入口文件应保持精简,把实现藏在子文件夹中。
分层规则(哪些 package 可以依赖哪些 package)属于另一项工作,因此配置里留了注释占位,供仓库自行补充。
1. 检查环境
Section titled “1. 检查环境”- package 管理器:有
pnpm-lock.yaml就用 pnpm,有yarn.lock就用 yarn,有bun.lockb就用 bun,否则用 npm。下面所有命令都用检测到的管理器(pnpm/yarn/npm run/bunx)。 - packages 根目录:如果存在
src/,就用src/packages,否则用packages。如果仓库已有明显不同的惯例,请先向用户确认选择。 - 现有配置:检查是否有
.dependency-cruiser.*文件。如果有,不要覆盖:把四条规则和相关选项合并进去,并告诉用户添加了什么。
**完成条件:**已确认 package 管理器、packages 根目录和现有配置状态。
2. 安装 dependency-cruiser
Section titled “2. 安装 dependency-cruiser”用检测到的 package 管理器把 dependency-cruiser 安装为开发依赖。
完成条件:dependency-cruiser 已加入 devDependencies。
3. 编写配置
Section titled “3. 编写配置”把 dependency-cruiser.config.cjs 复制到仓库根目录,命名为 .dependency-cruiser.cjs。将 PACKAGES_ROOT 设为第 1 步检测到的根目录。规则按路径深度工作,且与扩展名无关,无需再调整其他内容。
完成条件:.dependency-cruiser.cjs 已存在,PACKAGES_ROOT 正确,并包含四条禁止规则。
4. 接入检查命令
Section titled “4. 接入检查命令”- 添加
lint:boundaries脚本:depcruise <packages-root>(或depcruise src)。 - 把它并入仓库的总检查命令,即当前已运行 typecheck 的命令(例如
check/ci/validate)。不要改动tsconfig,也不要添加路径别名。 - 如果没有总检查脚本,就添加
lint:boundaries,并告诉用户将它加入 CI。
完成条件:lint:boundaries 已添加,并与 typecheck 在同一命令中运行。
5. 搭建示例 package
Section titled “5. 搭建示例 package”创建一个会提交到仓库的 <packages-root>/example/,作为可复制的模板:
index.ts是入口文件。导出一个委托给内部文件的函数,让这个 package 明显是深模块,而非简单转发。lib/impl.ts:位于子文件夹中的内部文件,由index.ts导入,外部无法访问。tests/example.test.ts只导入../index(入口文件),并对公共函数进行断言。
告诉用户这是一个起步模板,可以复制或删除。
**完成条件:**示例 package 已创建,通过根目录入口公开行为,并把 impl 隐藏在子文件夹中。
6. 验证规则确实生效
Section titled “6. 验证规则确实生效”这是整个 skill 的完成标准:违反规则却不会报错的配置毫无用处。
- 运行
lint:boundaries。干净的示例必须通过。 - 临时在
tests/example.test.ts中添加深度导入(例如import { thing } from "../lib/impl")。再次运行lint:boundaries;它必须因tests-through-entrypoints失败。 - 撤销深度导入,再运行一次;这次必须通过。
**完成条件:**确认依次通过、深度导入时报错、撤销后再次通过。若第 2 步没有失败,说明规则没有正确接入,必须修好后再结束。
7. 记录约定
Section titled “7. 记录约定”在 packages 文件夹中编写 README.md(<packages-root>/README.md,与它管理的 packages 同级),说明:
src/packages/<name>/的布局(根目录放入口文件,lib/放实现,tests/放测试);- 「只通过 package 的入口文件(根文件)导入」;
- 如何运行
lint:boundaries。
还要明确不建议使用汇总文件:公开多个小入口文件,而不是通过一个 index 重新导出整个子树。内容只需包括可复制的代码片段,以及每段一条说明四条规则的段落。
然后从仓库的代理指令文件中添加一个上下文指针(如果有 CLAUDE.md 就用它,否则用 AGENTS.md;两者都没有就新建 AGENTS.md)。一行就够,例如:Packages are deep modules: see [src/packages/README.md](./src/packages/README.md) before adding or importing one. 这样代理在触犯边界规则之前就能发现它。
完成条件:<packages-root>/README.md 已创建并明确不建议使用汇总文件,且仓库的 CLAUDE.md/AGENTS.md 链接到了它。
- 配置里的
$1反向引用(dependency-cruiser 的 group 匹配)允许 package 访问自身内部文件,同时阻止外部访问。不要将它们拆成按 package 单独配置的规则。 - 公有与私有由深度决定:package 根目录的文件是入口,子文件夹里的任何内容都是私有的。常见子文件夹是
lib/(实现)和tests/,但规则并不限定名称:任何子文件夹都是私有的,因此新增文件夹不需要修改配置。添加入口文件只需在根目录增加文件,不需要汇总文件。 - Packages 是扁平的:根目录下只有一层直接子项。package 的内部可以任意嵌套,但 package 里不能再包含另一个 package。
- 使用
.cjs(而不是.js),这样即使仓库设置了"type": "module",配置里的module.exports也能正常工作。
原文链接和 commit
Section titled “原文链接和 commit”读者要自己输入 /setup-ts-deep-modules 才会用到它。它不在 Claude Code 插件里。
为 TypeScript 仓库配置 dependency-cruiser,让每个 package 通过根目录入口文件公开功能,并将实现隐藏在子文件夹中。还会添加示例 package、记录约定,并验证边界规则确实生效。
Wire dependency-cruiser into a TypeScript repo so each package is a deep module, with implementation hidden in subfolders and reachable only through its entry-point files. User-invoked.- 仓库 mattpocock/skills
- 文件
skills/in-progress/setup-ts-deep-modules/SKILL.md - 提交
d81f3a1 - 原仓库里这一则没有单独的说明页。
- 同目录文件
skills/in-progress/setup-ts-deep-modules/agents/openai.yaml - 同目录文件
skills/in-progress/setup-ts-deep-modules/dependency-cruiser.config.cjs
英文 SKILL.md
---name: setup-ts-deep-modulesdescription: Wire dependency-cruiser into a TypeScript repo so each package is a deep module, with implementation hidden in subfolders and reachable only through its entry-point files. User-invoked.disable-model-invocation: true---
# Setup TS Deep Modules
Make every package in this repo a **deep module**: a lot of behaviour behind a small interface. A package's public surface is its **entry points** (the files at the package root), and everything in its subfolders is hidden. This skill installs [dependency-cruiser](https://github.com/sverweij/dependency-cruiser) and the rules that make the entry points the only way in, then proves the rules bite.
For the vocabulary (deep module, interface, seam, depth), call the Skill tool with "codebase-design" and use its language throughout.
## The shape this enforces
```src/packages/ <name>/ index.ts ← an entry point (public). Import this from outside. client.ts ← another entry point. Packages may expose SEVERAL. lib/ ← implementation: hidden from outside, free to import each other. tests/ ← co-located tests + fixtures (a subfolder, so private).```
The public surface is the package's **root files**, not one designated `index.ts`. By convention implementation lives in `lib/` and tests in `tests/`, giving every package the same two-folder shape. The rule itself is general, though: *anything* in *any* subfolder is private, so you never extend the config to add a folder.
Four rules, all `error`:
1. **Entry-point boundary**: code outside a package (app code or another package) may import only that package's entry points (its root files), never anything in its subfolders.2. **Intra-package freedom**: a package's own files import each other freely.3. **Tests through the entry points**: files under `<pkg>/tests/` may import any package's entry points and their own `tests/` fixtures, but never any package's subfolder internals (not even their own). Integration tests across packages are fine; deep imports are not.4. **No cycles**: no dependency cycles.
**Entry points, not a barrel.** Because the public surface is *every* root file, a package can expose several small entry points (`index.ts`, `client.ts`, `server.ts`) instead of funnelling everything through one giant `index.ts`. Barrel files that re-export a whole subtree are discouraged; keep entry points small and hide implementation in subfolders.
Layering (which packages may depend on which) is a *different* concern and is left as a commented stub in the config for this repo to fill in.
## Steps
### 1. Detect the environment
- **Package manager**: `pnpm-lock.yaml` → pnpm, `yarn.lock` → yarn, `bun.lockb` → bun, else npm. Use it for every command below (`pnpm`/`yarn`/`npm run`/`bunx`).- **Packages root**: if `src/` exists use `src/packages`, else `packages`. Confirm the choice with the user if the repo already has a different obvious convention.- **Existing config**: check for a `.dependency-cruiser.*` file. If one exists, do **not** overwrite it: merge the four rules and the options in, and tell the user what you added.
**Done when:** package manager, packages root, and existing-config status are all known.
### 2. Install dependency-cruiser
Install `dependency-cruiser` as a devDependency with the detected package manager.
**Done when:** `dependency-cruiser` is in `devDependencies`.
### 3. Write the config
Copy [`dependency-cruiser.config.cjs`](./dependency-cruiser.config.cjs) to the repo root as `.dependency-cruiser.cjs`. Set `PACKAGES_ROOT` to the root detected in step 1. The rules are path-depth based and extension-agnostic, so nothing else needs adapting.
**Done when:** `.dependency-cruiser.cjs` exists with the correct `PACKAGES_ROOT`, and the four forbidden rules are present.
### 4. Wire it into the checks
- Add a `lint:boundaries` script: `depcruise <packages-root>` (or `depcruise src`).- Fold it into the repo's umbrella check command, the one that already runs typecheck (e.g. a `check` / `ci` / `validate` script). Do **not** touch `tsconfig` or add path aliases.- If there is no umbrella script, add `lint:boundaries` and tell the user to include it in CI.
**Done when:** `lint:boundaries` exists and runs as part of the same command as typecheck.
### 5. Scaffold the example package
Create a committed `<packages-root>/example/` as a copy-me template:
- `index.ts` is an entry point. Export one function that delegates to an internal file (so the package is visibly *deep*, not a pass-through).- `lib/impl.ts`: an internal file in a **subfolder**, imported by `index.ts`, not reachable from outside.- `tests/example.test.ts` imports **only** `../index` (an entry point) and asserts against the public function.
Tell the user this is a starter template to copy or delete.
**Done when:** the example package exists, exposes its behaviour through a root entry point, and hides `impl` in a subfolder.
### 6. Prove the rules bite
This is the completion criterion for the whole skill: a config that doesn't fail on a violation is worthless.
1. Run `lint:boundaries`. It must **pass** on the clean example.2. Temporarily add a deep import to `tests/example.test.ts` (e.g. `import { thing } from "../lib/impl"`). Run `lint:boundaries` again; it must **fail** with `tests-through-entrypoints`.3. Revert the deep import. Run once more, and it must **pass**.
**Done when:** you have observed a pass, then a fail on the deep import, then a pass again. If step 2 does not fail, the rules are not wired correctly, so fix before finishing.
### 7. Document the convention
Write a `README.md` **in the packages folder** (`<packages-root>/README.md`, next to the packages it governs) covering: the `src/packages/<name>/` layout (entry points at the root, `lib/` for implementation, `tests/` for tests), "import only through a package's entry points (its root files)", and how to run `lint:boundaries`. **Discourage barrel files** explicitly: expose several small entry points instead of re-exporting a whole subtree through one index. Keep it to the copy-me snippet plus the four rules in one paragraph each.
Then add a **context pointer** to it from the repo's agent-instructions file (`CLAUDE.md` if present, else `AGENTS.md`, creating `AGENTS.md` if neither exists). One line is enough, e.g. `Packages are deep modules: see [src/packages/README.md](./src/packages/README.md) before adding or importing one.` This is what makes an agent discover the boundary rule instead of tripping over it.
**Done when:** `<packages-root>/README.md` exists and discourages barrels, and the repo's `CLAUDE.md`/`AGENTS.md` links to it.
## Notes
- The config's `$1` back-references (dependency-cruiser's group matching) are what let a package reach its own internals while outsiders can't. Don't flatten them into separate per-package rules.- Public vs private is decided by **depth**: a package's root files are entry points; anything in a subfolder is private. The conventional subfolders are `lib/` (implementation) and `tests/`, but the rule doesn't hardcode them: any subfolder is private, so a new folder never needs a config change. Adding an entry point is just adding a root file (no barrel).- Packages are **flat**: one tier of immediate children under the root. A package's internals may nest as deep as you like; a package may not contain another package.- Use `.cjs` (not `.js`) so the config's `module.exports` works even in `"type": "module"` repos.非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。
技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock