Skip to content

Commit 7da17be

Browse files
committed
优化修改文件重新编译效率
1 parent 4aa1878 commit 7da17be

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

lib/dev-server.js

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,81 @@ class DevServer {
6767

6868
async handleFileChange(filepath) {
6969
try {
70-
if (filepath.endsWith('pageforge.yaml')) {
71-
console.log('📝 配置文件已更新,重新加载配置并重新编译...');
72-
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();
7391
}
92+
// 其他文件 - 全量编译
7493
else {
75-
console.log(`📄 文件 ${filepath} 已更新,正在重新编译...`);
94+
console.log(`📄 文件已更新: ${filepath},全量重新编译...`);
95+
await this.generator.generate();
7696
}
7797

78-
const startTime = Date.now();
79-
await this.generator.generate();
8098
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
81-
console.log(`✓ 项目重新编译完成,耗时: ${duration}s`);
99+
console.log(`✓ 编译完成,耗时: ${duration}s`);
82100
}
83101
catch (error) {
84-
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+
);
85145
}
86146
}
87147
}

0 commit comments

Comments
 (0)