fix: 模板/组件扫描显式排序以保证构建确定性

- loadHandlebarsTemplates:对 templates/layouts 与 templates/components 的 .hbs 列表先 filter+sort 再注册 partial
- 避免 fs.readdirSync 在不同文件系统/平台下返回顺序不一致导致的潜在产物漂移
This commit is contained in:
rbetree
2026-01-07 17:48:57 +08:00
parent efd1683e2b
commit a3465fe4a1

View File

@@ -23,13 +23,14 @@ function loadHandlebarsTemplates() {
// 加载布局模板 // 加载布局模板
const layoutsDir = path.join(templatesDir, 'layouts'); const layoutsDir = path.join(templatesDir, 'layouts');
if (fs.existsSync(layoutsDir)) { if (fs.existsSync(layoutsDir)) {
fs.readdirSync(layoutsDir).forEach((file) => { fs.readdirSync(layoutsDir)
if (file.endsWith('.hbs')) { .filter((file) => file.endsWith('.hbs'))
.sort()
.forEach((file) => {
const layoutName = path.basename(file, '.hbs'); const layoutName = path.basename(file, '.hbs');
const layoutPath = path.join(layoutsDir, file); const layoutPath = path.join(layoutsDir, file);
const layoutContent = fs.readFileSync(layoutPath, 'utf8'); const layoutContent = fs.readFileSync(layoutPath, 'utf8');
handlebars.registerPartial(layoutName, layoutContent); handlebars.registerPartial(layoutName, layoutContent);
}
}); });
} else { } else {
throw new Error('Layouts directory not found. Cannot proceed without layout templates.'); throw new Error('Layouts directory not found. Cannot proceed without layout templates.');
@@ -38,13 +39,14 @@ function loadHandlebarsTemplates() {
// 加载组件模板 // 加载组件模板
const componentsDir = path.join(templatesDir, 'components'); const componentsDir = path.join(templatesDir, 'components');
if (fs.existsSync(componentsDir)) { if (fs.existsSync(componentsDir)) {
fs.readdirSync(componentsDir).forEach((file) => { fs.readdirSync(componentsDir)
if (file.endsWith('.hbs')) { .filter((file) => file.endsWith('.hbs'))
.sort()
.forEach((file) => {
const componentName = path.basename(file, '.hbs'); const componentName = path.basename(file, '.hbs');
const componentPath = path.join(componentsDir, file); const componentPath = path.join(componentsDir, file);
const componentContent = fs.readFileSync(componentPath, 'utf8'); const componentContent = fs.readFileSync(componentPath, 'utf8');
handlebars.registerPartial(componentName, componentContent); handlebars.registerPartial(componentName, componentContent);
}
}); });
} else { } else {
throw new Error('Components directory not found. Cannot proceed without component templates.'); throw new Error('Components directory not found. Cannot proceed without component templates.');