@@ -13,8 +13,10 @@ class DevServer {
1313
1414 async start ( ) {
1515 // 首先构建一次
16+ const startTime = Date . now ( ) ;
1617 await this . generator . generate ( ) ;
17- console . log ( '🎉 项目编译完成' ) ;
18+ const duration = ( ( Date . now ( ) - startTime ) / 1000 ) . toFixed ( 2 ) ;
19+ console . log ( `🎉 项目编译完成,耗时: ${ duration } s` ) ;
1820
1921 const home = this . config ?. version ? this . config . outputPath . replace ( this . config . version , '' ) : this . config . outputPath ;
2022 // 设置静态文件服务
@@ -65,19 +67,81 @@ class DevServer {
6567
6668 async handleFileChange ( filepath ) {
6769 try {
68- if ( filepath . endsWith ( 'pageforge.yaml' ) ) {
69- console . log ( '📝 配置文件已更新,重新加载配置并重新编译...' ) ;
70- await this . generator . reloadConfig ( ) ;
70+ const startTime = Date . now ( ) ;
71+
72+ // 配置文件或模板文件变化 - 全量编译
73+ if ( filepath . endsWith ( 'pageforge.yaml' ) ||
74+ filepath . includes ( '/templates/' ) ||
75+ filepath . includes ( '\\templates\\' ) ) {
76+ console . log ( '📝 配置/模板文件已更新,全量重新编译...' ) ;
77+ if ( filepath . endsWith ( 'pageforge.yaml' ) ) {
78+ await this . generator . reloadConfig ( ) ;
79+ }
80+ await this . generator . generate ( ) ;
81+ }
82+ // Markdown 文件变化 - 增量编译
83+ else if ( filepath . endsWith ( '.md' ) ) {
84+ console . log ( `📄 Markdown 文件已更新: ${ filepath } ` ) ;
85+ await this . compileSingleMarkdown ( filepath ) ;
86+ }
87+ // 资源文件变化 - 复制资源
88+ else if ( this . config . assetsPath && filepath . startsWith ( this . config . assetsPath ) ) {
89+ console . log ( `📦 资源文件已更新: ${ filepath } ` ) ;
90+ await this . generator . copyAssets ( ) ;
7191 }
92+ // 其他文件 - 全量编译
7293 else {
73- console . log ( `📄 文件 ${ filepath } 已更新,正在重新编译...` ) ;
94+ console . log ( `📄 文件已更新: ${ filepath } ,全量重新编译...` ) ;
95+ await this . generator . generate ( ) ;
7496 }
7597
76- await this . generator . generate ( ) ;
77- console . log ( '✓ 项目重新编译完成' ) ;
98+ const duration = ( ( Date . now ( ) - startTime ) / 1000 ) . toFixed ( 2 ) ;
99+ console . log ( `✓ 编译完成,耗时: ${ duration } s` ) ;
78100 }
79101 catch ( error ) {
80- console . error ( '🤯 项目文件重新编译失败 ' , error ) ;
102+ console . error ( '🤯 编译失败:' , error ) ;
103+ }
104+ }
105+
106+ async compileSingleMarkdown ( filepath ) {
107+ const fs = require ( 'fs' ) ;
108+
109+ // 检查文件是否在源目录中
110+ if ( ! filepath . startsWith ( this . config . sourcePath ) ) {
111+ console . log ( '文件不在源目录中,跳过编译' ) ;
112+ return ;
113+ }
114+
115+ // 计算相对路径
116+ const relativePath = path . relative ( this . config . sourcePath , filepath ) ;
117+ const sourceDir = path . dirname ( filepath ) ;
118+ const filename = path . basename ( filepath ) ;
119+
120+ // 判断是否启用国际化
121+ if ( this . config . feature ?. i18n ?. enable ) {
122+ // 多语言模式 - 为每个语言编译
123+ const locales = Object . keys ( this . config . i18n || { } ) . filter ( key => key !== 'default' ) ;
124+ for ( const locale of locales ) {
125+ const relativeDir = path . relative ( this . config . sourcePath , sourceDir ) ;
126+ const baseDir = path . join ( locale , relativeDir ) ;
127+ await this . generator . directoryProcessor . fileProcessor . processMarkdownFile (
128+ sourceDir ,
129+ baseDir ,
130+ filename ,
131+ locale ,
132+ this . config . sourcePath
133+ ) ;
134+ }
135+ } else {
136+ // 单语言模式
137+ const relativeDir = path . relative ( this . config . sourcePath , sourceDir ) ;
138+ await this . generator . directoryProcessor . fileProcessor . processMarkdownFile (
139+ sourceDir ,
140+ relativeDir ,
141+ filename ,
142+ '' ,
143+ this . config . sourcePath
144+ ) ;
81145 }
82146 }
83147}
0 commit comments