ln-721-frontend-restructure

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ln-721-frontend-restructure

ln-721-前端结构重构

Type: L3 Worker Category: 7XX Project Bootstrap Parent: ln-720-structure-migrator
Frontend structure worker with two modes: SCAFFOLD (generate minimal React project from template) or RESTRUCTURE (migrate monolith to component-based architecture).

类型: L3 处理工具 分类: 7XX 项目初始化 父级工具: ln-720-结构迁移工具
这款前端结构处理工具包含两种模式:SCAFFOLD(从模板生成极简React项目)和RESTRUCTURE(将单体应用迁移为组件化架构)。

Mode Selection

模式选择

ModeWhenInputOutput
SCAFFOLDCREATE pipeline — no existing frontendTarget stack config from ln-720Minimal React + Vite project (~7 files)
RESTRUCTURETRANSFORM pipeline — existing frontend foundMonolithic React sourceComponent-based architecture

模式适用场景输入输出
SCAFFOLD创建流水线场景——无现有前端项目来自ln-720的目标技术栈配置极简React + Vite项目(约7个文件)
RESTRUCTURE转换流水线场景——存在现有前端项目单体式React源码组件化架构

Purpose & Scope

目标与范围

AspectDescription
InputTarget stack config (SCAFFOLD) or monolithic React source (RESTRUCTURE)
OutputMinimal project (SCAFFOLD) or component-based architecture (RESTRUCTURE)
FrameworkReact + TypeScript + Vite
Scope boundaries:
  • SCAFFOLD: generates minimal starter files, no business logic
  • RESTRUCTURE: restructures existing code, does not add new functionality
  • Works with React + TypeScript projects
  • Applies transformation rules from reference files

维度说明
输入目标技术栈配置(SCAFFOLD模式)或单体式React源码(RESTRUCTURE模式)
输出极简项目(SCAFFOLD模式)或组件化架构(RESTRUCTURE模式)
技术栈React + TypeScript + Vite
范围边界:
  • SCAFFOLD:仅生成极简启动文件,不包含业务逻辑
  • RESTRUCTURE:仅重构现有代码,不添加新功能
  • 仅支持React + TypeScript项目
  • 基于参考文件中的转换规则执行操作

Workflow

工作流程

SCAFFOLD Mode (CREATE pipeline)

SCAFFOLD模式(创建流水线)

PhaseNameActionsOutput
S1GenerateCreate minimal React + Vite + TypeScript project files~7 starter files
S2VerifyCheck file structure, validate configsValid project skeleton
阶段名称操作输出
S1生成创建极简React + Vite + TypeScript项目文件约7个启动文件
S2验证检查文件结构,验证配置文件有效的项目骨架

RESTRUCTURE Mode (TRANSFORM pipeline)

RESTRUCTURE模式(转换流水线)

PhaseNameActionsOutput
1AnalyzeScan source, detect component types, measure complexityFile inventory, complexity metrics
2PlanApply split thresholds, calculate file moves, detect conflictsMigration plan
3ExecuteCreate folders, extract content, update importsRestructured files
4VerifyRun build, check imports, validate structureBuild success report

阶段名称操作输出
1分析扫描源码,检测组件类型,评估复杂度文件清单、复杂度指标
2规划应用拆分阈值,计算文件迁移路径,检测冲突迁移计划
3执行创建文件夹,提取内容,更新导入路径重构后的文件
4验证执行构建,检查导入路径,验证结构构建成功报告

SCAFFOLD Mode Phases

SCAFFOLD模式阶段

Phase S1: Generate Starter Files

阶段S1:生成启动文件

Create minimal React + Vite + TypeScript project.
FilePurpose
package.json
Dependencies: react, react-dom, typescript, vite, @vitejs/plugin-react
vite.config.ts
Vite config with React plugin, port, proxy settings
tsconfig.json
Strict TypeScript config with path aliases
index.html
Entry HTML with root div
src/main.tsx
React entry point with StrictMode
src/App.tsx
Root App component with router placeholder
src/index.css
Base styles (reset, variables, layout)
创建极简React + Vite + TypeScript项目。
文件用途
package.json
依赖项:react, react-dom, typescript, vite, @vitejs/plugin-react
vite.config.ts
Vite配置,包含React插件、端口、代理设置
tsconfig.json
严格模式TypeScript配置,包含路径别名
index.html
入口HTML文件,包含根div节点
src/main.tsx
React入口文件,启用StrictMode
src/App.tsx
根App组件,包含路由占位符
src/index.css
基础样式(重置样式、变量、布局)

Phase S2: Verify Scaffold

阶段S2:验证搭建结果

CheckMethodExpected
All files createdFile existence check7 files present
package.json validJSON parseNo syntax errors
tsconfig.json validJSON parseNo syntax errors
No hardcoded valuesContent scanProject name from config, not hardcoded

检查项检测方法预期结果
所有文件已创建文件存在性检查7个文件全部存在
package.json有效JSON解析无语法错误
tsconfig.json有效JSON解析无语法错误
无硬编码值内容扫描项目名称来自配置,而非硬编码

RESTRUCTURE Mode Phases

RESTRUCTURE模式阶段

Phase 1: Analyze

阶段1:分析

Scan current frontend structure and classify components.
StepActionReference
1.1Scan all
.tsx
and
.ts
files in source
1.2Measure file complexity (lines, hooks, types)
transformation_rules.md
1.3Classify components by category
component_patterns.md
1.4Build import dependency graph
import_strategy.md
Output: Component inventory with classifications and metrics.

扫描当前前端结构并对组件进行分类。
步骤操作参考文件
1.1扫描源码中所有
.tsx
.ts
文件
1.2评估文件复杂度(行数、hooks、类型)
transformation_rules.md
1.3按类别对组件进行分类
component_patterns.md
1.4构建导入依赖关系图
import_strategy.md
输出: 包含分类信息和指标的组件清单。

Phase 2: Plan

阶段2:规划

Generate migration plan based on analysis.
StepActionReference
2.1Apply split thresholds to identify files to restructure
transformation_rules.md
2.2Calculate target paths for each file
react_folder_structure.md
2.3Identify import updates needed
import_strategy.md
2.4Detect potential conflicts (name collisions, circular deps)
Output: Migration plan with Before/After mapping.

基于分析结果生成迁移计划。
步骤操作参考文件
2.1应用拆分阈值,确定需要重构的文件
transformation_rules.md
2.2计算每个文件的目标路径
react_folder_structure.md
2.3识别需要更新的导入路径
import_strategy.md
2.4检测潜在冲突(名称重复、循环依赖)
输出: 包含前后映射关系的迁移计划。

Phase 3: Execute

阶段3:执行

Apply transformations in correct order.
StepActionNotes
3.1Create directory structureAll target folders
3.2Extract types to
types.ts
Types have no dependencies
3.3Extract constants to
constants.ts
Constants depend only on types
3.4Extract hooks to
hooks.ts
Hooks depend on types, constants
3.5Extract sub-componentsComponents use all above
3.6Create barrel exports (
index.ts
)
For clean imports
3.7Update all import pathsFix references
Order is critical: Execute in sequence to avoid broken imports.

按正确顺序应用转换操作。
步骤操作注意事项
3.1创建目录结构生成所有目标文件夹
3.2将类型提取至
types.ts
类型无依赖项
3.3将常量提取至
constants.ts
常量仅依赖类型
3.4将hooks提取至
hooks.ts
hooks依赖类型和常量
3.5提取子组件组件依赖上述所有内容
3.6创建桶导出文件(
index.ts
实现简洁的导入方式
3.7更新所有导入路径修复引用关系
执行顺序至关重要:需按顺序执行,避免导入关系断裂。

Phase 4: Verify

阶段4:验证

Validate restructured project.
CheckCommandExpected
TypeScript compilation
npx tsc --noEmit
No errors
Build
npm run build
Success
No orphan filesManual checkSource location empty
Imports resolveBuild successNo module not found errors

验证重构后的项目。
检查项命令预期结果
TypeScript编译
npx tsc --noEmit
无错误
构建
npm run build
执行成功
无孤立文件手动检查原源码目录为空
导入路径可解析构建成功无“模块未找到”错误

Transformation Summary

转换总结

TransformationBefore StateAfter State
Component SplitSingle file >300 linesFeature folder with co-located files
Type ExtractionInline interfacesSeparate
types.ts
Constant ExtractionInline magic valuesSeparate
constants.ts
Hook ExtractionInline useState/useEffectSeparate
hooks.ts
or shared hooks
UI Component MoveScattered in featuresCentralized in
components/ui/
Layout Component MoveMixed with featuresCentralized in
components/layout/

转换操作转换前状态转换后状态
组件拆分单文件超过300行功能文件夹,包含同域相关文件
类型提取内联接口独立的
types.ts
文件
常量提取内联魔法值独立的
constants.ts
文件
Hook提取内联useState/useEffect独立的
hooks.ts
或共享hooks文件
UI组件迁移分散在功能模块中集中至
components/ui/
目录
布局组件迁移与功能模块混合集中至
components/layout/
目录

Critical Rules

核心规则

  • Mode Awareness: SCAFFOLD creates from template; RESTRUCTURE transforms existing — never mix
  • Single Responsibility: Handle only frontend structure, no backend changes
  • Idempotent: Can re-run without duplicate files or corruption
  • Build Verification: Must verify build passes after changes (RESTRUCTURE:
    npm run build
    )
  • Preserve Functionality: No behavioral changes, only structural (RESTRUCTURE mode)
  • Backup Strategy: Do not delete source files until verification passes (RESTRUCTURE mode)
  • Import Consistency: Use path aliases for shared, relative for co-located

  • 模式感知:SCAFFOLD模式从模板创建项目;RESTRUCTURE模式转换现有项目——切勿混合使用
  • 单一职责:仅处理前端结构,不修改后端内容
  • 幂等性:可重复执行,不会产生重复文件或损坏项目
  • 构建验证:修改后必须验证构建是否通过(RESTRUCTURE模式:执行
    npm run build
  • 保留功能:仅修改结构,不改变行为(RESTRUCTURE模式)
  • 备份策略:验证通过前,请勿删除原源码文件(RESTRUCTURE模式)
  • 导入一致性:共享模块使用路径别名,同域文件使用相对路径

Definition of Done

完成标准

SCAFFOLD mode:
  • All 7 starter files generated
  • package.json and tsconfig.json valid
  • No hardcoded project names (uses config values)
RESTRUCTURE mode:
  • All source files analyzed and classified
  • Migration plan generated with Before/After mapping
  • Directory structure created per template
  • All extractions completed (types, constants, hooks, components)
  • Import paths updated throughout project
  • npm run build
    passes successfully
  • No orphan imports or missing files
  • Barrel exports created for shared folders

SCAFFOLD模式:
  • 已生成全部7个启动文件
  • package.json和tsconfig.json配置有效
  • 无硬编码项目名称(使用配置值)
RESTRUCTURE模式:
  • 所有源码文件已完成分析与分类
  • 已生成包含前后映射关系的迁移计划
  • 已按模板创建目录结构
  • 已完成所有提取操作(类型、常量、hooks、组件)
  • 已更新项目中所有导入路径
  • npm run build
    执行成功
  • 无孤立导入或缺失文件
  • 已为共享文件夹创建桶导出文件

Risk Mitigation

风险缓解

RiskDetectionMitigation
Build failure after restructure
npm run build
fails
Rollback: restore from source, investigate specific error
Missing importsModule not found errorsScan all imports before/after, update missed paths
Circular dependenciesBuild warning or runtime errorAnalyze dependency graph, break cycles by extracting shared code
Lost functionalityTests fail or UI brokenRun existing tests before/after transformation
Name collisionsDuplicate export namesRename with feature prefix before moving

风险检测方式缓解措施
重构后构建失败
npm run build
执行失败
回滚:从原源码恢复,排查具体错误
缺失导入“模块未找到”错误扫描重构前后的所有导入路径,更新遗漏的路径
循环依赖构建警告或运行时错误分析依赖关系图,通过提取共享代码打破循环
功能丢失测试失败或UI异常转换前后运行现有测试用例
名称冲突导出名称重复迁移前添加功能前缀重命名

Reference Files

参考文件

FilePurpose
references/transformation_rules.md
Split thresholds, extraction rules, transformation order
references/component_patterns.md
Component classification by category
references/import_strategy.md
Import update rules, path aliases, barrel exports
references/react_folder_structure.md
Target directory structure template

Version: 3.0.0 Last Updated: 2026-02-07
文件用途
references/transformation_rules.md
拆分阈值、提取规则、转换顺序
references/component_patterns.md
组件分类规则
references/import_strategy.md
导入更新规则、路径别名、桶导出
references/react_folder_structure.md
目标目录结构模板

版本: 3.0.0 最后更新时间: 2026-02-07