From a3465fe4a11ab527f64245dcd9fdd50a7acc7f5f Mon Sep 17 00:00:00 2001 From: rbetree Date: Wed, 7 Jan 2026 17:48:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A8=A1=E6=9D=BF/=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E6=98=BE=E5=BC=8F=E6=8E=92=E5=BA=8F=E4=BB=A5?= =?UTF-8?q?=E4=BF=9D=E8=AF=81=E6=9E=84=E5=BB=BA=E7=A1=AE=E5=AE=9A=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - loadHandlebarsTemplates:对 templates/layouts 与 templates/components 的 .hbs 列表先 filter+sort 再注册 partial - 避免 fs.readdirSync 在不同文件系统/平台下返回顺序不一致导致的潜在产物漂移 --- src/generator.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/generator.js b/src/generator.js index 781a68d..dd8c748 100644 --- a/src/generator.js +++ b/src/generator.js @@ -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.');