modularize
Restructures monolithic files, God classes/components, and tightly coupled projects into high-cohesion, loosely coupled modules with clean folder/file structures and explicit architectural boundaries. Enforces Single Responsibility Principle (SRP), Dependency Inversion (DIP), Acyclic Dependencies (no circular imports), and Deep Module design. Use when the user invokes /modularize, asks to split large files, decouple modules, reorganize project folder structure, or refactor a monolith into clean modular boundaries.
- License 许可
- MIT
- 最近更新
- 2026-10-23
- Stars
- ★ 5
- Forks
- 0
01 前言 Front Matter
- name
- modularize
- description
- Restructures monolithic files, God classes/components, and tightly coupled projects into high-cohesion, loosely coupled modules with clean folder/file structures and explicit architectural boundaries. Enforces Single Responsibility Principle (SRP), Dependency Inversion (DIP), Acyclic Dependencies (no circular imports), and Deep Module design. Use when the user invokes /modularize, asks to split large files, decouple modules, reorganize project folder structure, or refactor a monolith into clean modular boundaries.
- allowed-tools
- Bash Read Grep Glob Edit Write
02 资源文件 3
- 参考文档references/architectural-partitioning.md15.7 KB
- 参考文档references/decoupling-and-refactoring-patterns.md11.1 KB
- 参考文档references/folder-and-file-structure.md12.6 KB
03 SKILL.md 正文
Modularize & Decouple Architecture
Transform monolithic files, tangled "God" modules, and flat or chaotic directory trees into high-cohesion, loosely coupled modules with explicit public contracts and enforced architectural boundaries.
True modularization is not chopping one 1,000-line file into ten 100-line files that all import each other in a circle. Good modularity means each module hides a meaningful piece of complexity behind a small, stable interface (John Ousterhout's Deep Modules), dependencies flow in one direction (DAG), and changing one feature or infrastructure adapter does not ripple across unrelated files.
Reference Modules
Load the appropriate reference guide before restructuring files, folders, or architectural layers:
| Concern | Reference Module | What It Covers |
|---|---|---|
| Folder & File Organization | references/folder-and-file-structure.md | Package-by-Feature (Vertical Slice) vs. Package-by-Layer, Frontend Feature-Sliced Design (FSD), Backend/Service layouts across TS/Python/Go/Rust, Co-location rules, and Public API Facades vs. Barrel (index.ts) traps |
| Architectural Partitioning & Boundaries | references/architectural-partitioning.md | Modular Monolith design, Hexagonal (Ports & Adapters) & Clean Architecture, Functional Core / Imperative Shell, Deep vs. Shallow modules, Acyclic Dependencies Principle (ADP), and automated boundary enforcement |
| Decoupling & Safe Extraction Patterns | references/decoupling-and-refactoring-patterns.md | Coupling & Cohesion taxonomy, Dependency Inversion (DIP), Event/Command decoupling, UI Component decomposition (Container/Hook/View), and the step-by-step Strangler workflow for splitting God files safely |
Core Principles of Modularity
- High Cohesion (Things That Change Together Stay Together):
Group code by business capability / domain feature first, and by technical role second. A change to
billingshould stay insidesrc/features/billing/(orsrc/modules/billing/), not require edits across 8 top-levelcontrollers/,services/,helpers/, andutils/directories. - Loose Coupling & Narrow Interfaces: Modules communicate exclusively through minimal, explicitly typed public interfaces. Internal data structures, database queries, and private helpers stay unexported.
- Strict Dependency Direction (No Cycles):
The module dependency graph must form a Directed Acyclic Graph (DAG):
Entry/UI/HTTP → Feature/Application → Domain/Core ← Infrastructure Adapters(via Dependency Inversion), andFeature Modules → Shared/Core(neverShared → Feature). - Deep Modules Over Pass-Through Slop: Do not create 5 layers of 1-line wrapper classes that just forward arguments unchanged. Extract a module when it encapsulates distinct state, policy, I/O boundary, or reusable algorithm behind a simpler signature.
Execution Workflow
Phase 1 — Reconnaissance & Coupling Diagnosis
Before moving or splitting any file, map the current dependency graph and identify the coupling hotspots:
# 1. Find the largest files (God files / monolith candidates)
wc -l $(git ls-files '*.ts' '*.tsx' '*.py' '*.go' '*.rs' '*.java' '*.php' 2>/dev/null) 2>/dev/null | sort -rn | head -20
# 2. Inspect who imports the target file (blast radius of callers)
grep -rn "from ['\"].*<target-module>" src/ app/ tests/ 2>/dev/null
# 3. Inspect what the target file imports (fan-out / mixed responsibilities)
grep -nE "^(import |from .* import |require\()" <path/to/target-file>
Identify which axes of change are tangled inside the target:
- Pure Domain / Business Policy (calculations, validations, state machines)
- State / Orchestration (use-case flow, hooks, stores, transaction boundaries)
- External I/O & Infrastructure (SQL/ORM queries,
fetch/SDK calls, filesystem, Redis) - Presentation / Transport (JSX/HTML rendering, CLI flag parsing, HTTP request/response formatting)
Phase 2 — Design the Target Module Structure
Select the appropriate structure from references/folder-and-file-structure.md and references/architectural-partitioning.md:
- Splitting a Single Monolithic File / Component:
Decompose along responsibility seams into cohesive sibling files:
*.types.ts/models.py— Domain types, schemas, contracts*.logic.ts/policy.py— Pure, zero-I/O business transformations & validations*.repository.ts/*.client.ts— External I/O and data-access adapters*.service.ts/use-*.ts— Orchestration / stateful hook wiring pure logic + I/O*.view.tsx/*.controller.ts— Thin UI rendering or HTTP/CLI transport handler
- Restructuring a Whole Project / Directory Tree:
Migrate from flat or technical-layer sprawl (
utils/,helpers/,misc/) to Feature/Domain Modules (modules/<feature>/orfeatures/<feature>/) + a strictly pureshared/(orpkg//internal/) foundation.
Phase 3 — Incremental, Caller-Safe Extraction
Follow the safe extraction choreography in references/decoupling-and-refactoring-patterns.md:
- Extract Pure Functions & Types First (Zero Dependencies): Move pure calculations, validators, constants, and types into dedicated modules first — they depend on nothing and are trivial to unit-test.
- Invert Hardcoded I/O Dependencies:
Replace inline database calls,
fetchrequests, or third-party SDK instantiations with narrow Port interfaces or injected dependencies. - Preserve Public Entry Points During Transition:
If external callers import
from './orders', keeporders/index.ts(or the original file path) re-exporting the public API while you update call sites, then remove dead shims once all callers are updated. - Eliminate Circular Dependencies:
If Module A and Module B import each other, either:
- Extract the shared contract/type into a lower-level module
C(A → C ← B), or - Invert one direction using an interface or Domain Event.
- Extract the shared contract/type into a lower-level module
Phase 4 — Verify Build, Tests & Dependency Graph
After modularizing:
- Run the compiler / typechecker (
tsc --noEmit,mypy,pyright,cargo check,go build ./...). - Run the full test suite to confirm zero behavioral regressions.
- Verify zero circular dependencies (
npx madge --circular src/or language equivalent) and zero broken imports.
Verification Checklist
- [ ] Single Responsibility: Every new file/module has one clear, nameable responsibility and one primary axis of change.
- [ ] No "Junk Drawer" Files: Zero new
utils.ts,helpers.ts,common.ts, ormisc.pyfiles created — every helper is named after its domain/technical capability (currency-format.ts,retry-backoff.ts). - [ ] Acyclic Dependency Graph: Zero circular imports (
A ↔ B); dependencies strictly flowPresentation → Application/Feature → Domain ← InfrastructureandFeatures → Shared. - [ ] Pure Core Separated from I/O: Business calculations and validations can be imported and tested without booting a database, HTTP server, or UI renderer.
- [ ] All Call Sites & Tests Green: Every import path across source and test files is updated and verified by passing build and test runs.
04 同仓库更多 Skills
writing-guidelines
Review docs/prose for Writing Guidelines compliance. Use when asked to "review my docs", "check writing style", "audit prose", "review docs voice and tone", or "check this page against the writing handbook".
writing-for-agents
Writing documents for agents. Use when creating or editing skills, or modifying AGENTS.md or CLAUDE.md.
wrangler
Cloudflare Workers CLI for deploying, developing, and managing Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines, and Secrets Store. Load before running wrangler commands to ensure correct syntax and best practices. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.
workers-best-practices
Reviews and authors Cloudflare Workers code against production best practices. Load when writing new Workers, reviewing Worker code, configuring wrangler.jsonc, or checking for common Workers anti-patterns (streaming, floating promises, global state, secrets, bindings, observability). Biases towards retrieval from Cloudflare docs over pre-trained knowledge.
web-perf
Analyzes web performance using Chrome DevTools MCP. Measures Core Web Vitals (LCP, INP, CLS) and supplementary metrics (FCP, TBT, Speed Index), identifies render-blocking resources, network dependency chains, layout shifts, caching issues, and accessibility gaps. Use when asked to audit, profile, debug, or optimize page load performance, Lighthouse scores, or site speed. Biases towards retrieval from current documentation over pre-trained knowledge.
using-agent-skills
Discovers and invokes agent skills. Use when starting a session, or when you need to decide which skill or workflow applies to the piece of work at hand. This is the meta-skill that governs how all other skills are discovered and invoked.