# Design Document ## Overview The Vite HTML to PHP Template plugin is a post-build transformation tool that converts standard HTML files into PHP template format. The plugin integrates with Vite's build lifecycle, executing after the build process completes to perform secondary transformations on generated HTML files. It uses HTML parsing to extract specific content sections and reorganizes them into a structured PHP template format compatible with template engines like ThinkPHP. ## Architecture ### Plugin Architecture The plugin follows Vite's plugin architecture pattern and implements the following hooks: - **closeBundle**: Executes after the build is complete and all files are written - **buildEnd**: Fallback hook for error handling and cleanup ### Core Components ``` vite-plugin-html-to-php-template/ ├── src/ │ ├── index.ts # Main plugin entry point │ ├── parser.ts # HTML parsing and content extraction │ ├── template-generator.ts # PHP template generation │ ├── file-processor.ts # File system operations │ └── types.ts # TypeScript type definitions ├── package.json ├── tsconfig.json └── README.md ``` ## Components and Interfaces ### Plugin Interface ```typescript interface PluginOptions { // Input directory containing HTML files (default: 'dist') inputDir?: string; // Output directory for PHP templates (default: same as inputDir) outputDir?: string; // Glob pattern for HTML files to process (default: '**/*.html') include?: string | string[]; // Glob pattern for files to exclude (default: []) exclude?: string | string[]; // Template configuration template?: { extend?: string; // Default: 'main/newMain' blocks?: BlockConfig; // Block name mappings footer?: string; // Default: 'footer2' }; // Processing options preserveOriginal?: boolean; // Keep original HTML files (default: false) encoding?: string; // File encoding (default: 'utf-8') } interface BlockConfig { title?: string; // Default: 'title' keywords?: string; // Default: 'keywords' description?: string; // Default: 'description' csslink?: string; // Default: 'csslink' main?: string; // Default: 'main' script?: string; // Default: 'script' } ``` ### HTML Parser Interface ```typescript interface ParsedContent { title: string; keywords: string; description: string; stylesheets: string[]; scripts: string[]; bodyContent: string; } interface ParserOptions { preserveComments?: boolean; minifyContent?: boolean; } ``` ### Template Generator Interface ```typescript interface TemplateConfig { extend: string; blocks: BlockConfig; footer: string; } interface GeneratedTemplate { content: string; blocks: { [key: string]: string; }; } ``` ## Data Models ### HTML Content Extraction Model The parser extracts content using the following mapping: 1. **Title**: `