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