pug-site-core 3.0.6 → 3.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/generate.js +84 -104
  2. package/package.json +2 -2
package/lib/generate.js CHANGED
@@ -544,29 +544,24 @@ export async function buildStatic() {
544
544
  * @returns {string} 完整的HTML文件路径
545
545
  */
546
546
  function buildHtmlPath(lang, outputPath, fileName = null) {
547
- let htmlPath;
548
- try {
549
- if (fileName) {
550
- htmlPath = paths.resolveRoot(
551
- distOutputPath,
552
- lang,
553
- outputPath.replace(
554
- outputPath
555
- .split("/")
556
- .pop()
557
- .replace(/\.[^.]*$/, ""),
558
- fileName.replace(/\.[^.]*$/, "")
559
- )
560
- );
561
- } else {
562
- htmlPath = paths.resolveRoot(distOutputPath, lang, outputPath);
563
- }
547
+ if (fileName) {
548
+ const htmlPath = paths.resolveRoot(
549
+ distOutputPath,
550
+ lang,
551
+ outputPath.replace(
552
+ outputPath
553
+ .split("/")
554
+ .pop()
555
+ .replace(/\.[^.]*$/, ""),
556
+ fileName.replace(/\.[^.]*$/, "")
557
+ )
558
+ );
559
+ console.log(htmlPath);
560
+ return htmlPath;
561
+ } else {
562
+ const htmlPath = paths.resolveRoot(distOutputPath, lang, outputPath);
564
563
  console.log(htmlPath);
565
564
  return htmlPath;
566
- } catch (error) {
567
- throw new Error(
568
- `构建HTML路径失败 [lang: ${lang}, outputPath: ${outputPath}, fileName: ${fileName}]: ${error}`
569
- );
570
565
  }
571
566
  }
572
567
 
@@ -574,21 +569,15 @@ export async function buildStatic() {
574
569
  * 生成HTML内容
575
570
  */
576
571
  function generateHtml(pagesPugToFn, funName, data, pagePath, commonData) {
577
- try {
578
- if (!pagesPugToFn[funName]) {
579
- throw new Error(`模板函数 ${funName} 不存在`);
580
- }
581
-
582
- return pagesPugToFn[funName]({
583
- data,
584
- _pagePath: pagePath,
585
- common: commonData,
586
- });
587
- } catch (error) {
588
- throw new Error(
589
- `生成HTML失败 [funName: ${funName}, pagePath: ${pagePath}]: ${error}`
590
- );
572
+ if (!pagesPugToFn[funName]) {
573
+ throw new Error(`模板函数 ${funName} 不存在`);
591
574
  }
575
+
576
+ return pagesPugToFn[funName]({
577
+ data,
578
+ _pagePath: pagePath,
579
+ common: commonData,
580
+ });
592
581
  }
593
582
 
594
583
  /**
@@ -614,27 +603,21 @@ export async function buildStatic() {
614
603
  const property = name.slice(1, -1);
615
604
 
616
605
  await async.eachLimit(data, 128, async (dataItem, index) => {
617
- try {
618
- const fileName = dataItem[property];
619
- if (!fileName) {
620
- throw new Error(`数据项索引 ${index} 中缺少属性 ${property} 或值为空`);
621
- }
622
-
623
- const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath, fileName);
624
- const html = generateHtml(
625
- pagesPugToFn,
626
- funName,
627
- dataItem,
628
- pagePath,
629
- commonData
630
- );
631
- const compressedHtml = await compressHtml(html, htmlPath);
632
- await fse.outputFile(htmlPath, compressedHtml);
633
- } catch (error) {
634
- throw new Error(
635
- `处理数组数据项失败 [index: ${index}, property: ${property}]: ${error}`
636
- );
606
+ const fileName = dataItem[property];
607
+ if (!fileName) {
608
+ throw new Error(`数据项索引 ${index} 中缺少属性 ${property} 或值为空`);
637
609
  }
610
+
611
+ const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath, fileName);
612
+ const html = generateHtml(
613
+ pagesPugToFn,
614
+ funName,
615
+ dataItem,
616
+ pagePath,
617
+ commonData
618
+ );
619
+ const compressedHtml = await compressHtml(html, htmlPath);
620
+ await fse.outputFile(htmlPath, compressedHtml);
638
621
  });
639
622
  } else {
640
623
  const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath);
@@ -643,9 +626,10 @@ export async function buildStatic() {
643
626
  await fse.outputFile(htmlPath, compressedHtml);
644
627
  }
645
628
  } catch (error) {
646
- throw new Error(
647
- `处理数组数据失败 [lang: ${lang}, getDataFn: ${obj.getDataFn}]: ${error}`
629
+ console.error(
630
+ `处理数组数据失败 [lang: ${lang}, getDataFn: ${obj.getDataFn}]:`, error
648
631
  );
632
+ throw error;
649
633
  }
650
634
  }
651
635
 
@@ -733,11 +717,10 @@ export async function buildStatic() {
733
717
  throw new Error(`数据类型不支持: ${typeof data}`);
734
718
  }
735
719
  } catch (error) {
736
- throw new Error(
737
- `处理自定义HTML构建失败 [lang: ${lang}, config: ${JSON.stringify(obj)}]: ${
738
- error
739
- }`
720
+ console.error(
721
+ `处理自定义HTML构建失败 [lang: ${lang}, config: ${JSON.stringify(obj)}]:`,error
740
722
  );
723
+ throw error;
741
724
  }
742
725
  });
743
726
  }
@@ -758,55 +741,50 @@ export async function buildStatic() {
758
741
  ).filter((fileName) => fileName.endsWith(".json"));
759
742
 
760
743
  await async.eachLimit(pagesAllJsonFileName, 128, async (jsonFileName) => {
761
- try {
762
- const data = await fse.readJSON(
763
- paths.resolveRoot(langDataPath, jsonFileName)
764
- );
765
- const pugTemplate = data._template;
766
-
767
- if (!pugTemplate) {
768
- return;
769
- }
744
+ const data = await fse.readJSON(
745
+ paths.resolveRoot(langDataPath, jsonFileName)
746
+ );
747
+ const pugTemplate = data._template;
770
748
 
771
- const funName = pugTemplate.split(pathSymbol).join("_").slice(0, -4);
749
+ if (!pugTemplate) {
750
+ return;
751
+ }
772
752
 
773
- if (dealWithEndFunName.has(funName)) {
774
- return;
775
- }
753
+ const funName = pugTemplate.split(pathSymbol).join("_").slice(0, -4);
776
754
 
777
- const html = generateHtml(
778
- pagesPugToFn,
779
- funName,
780
- data,
781
- pugTemplate,
782
- commonData
783
- );
755
+ if (dealWithEndFunName.has(funName)) {
756
+ return;
757
+ }
784
758
 
785
- // 构建输出路径
786
- let finalPugTemplate = pugTemplate;
787
- if (data.page_name) {
788
- finalPugTemplate =
789
- pugTemplate.split(pathSymbol).slice(0, -1).join(pathSymbol) +
790
- pathSymbol +
791
- data.page_name;
792
- }
759
+ const html = generateHtml(
760
+ pagesPugToFn,
761
+ funName,
762
+ data,
763
+ pugTemplate,
764
+ commonData
765
+ );
793
766
 
794
- const htmlPath = paths.resolveRoot(
795
- distOutputPath,
796
- lang,
797
- finalPugTemplate.replace(/\.[^.]*$/, ".html")
798
- );
799
- console.log(htmlPath);
800
- const compressedHtml = await compressHtml(html, htmlPath);
801
- await fse.outputFile(htmlPath, compressedHtml);
802
- } catch (error) {
803
- throw new Error(
804
- `处理JSON文件失败 [file: ${jsonFileName}]: ${error.message}`
805
- );
767
+ // 构建输出路径
768
+ let finalPugTemplate = pugTemplate;
769
+ if (data.page_name) {
770
+ finalPugTemplate =
771
+ pugTemplate.split(pathSymbol).slice(0, -1).join(pathSymbol) +
772
+ pathSymbol +
773
+ data.page_name;
806
774
  }
775
+
776
+ const htmlPath = paths.resolveRoot(
777
+ distOutputPath,
778
+ lang,
779
+ finalPugTemplate.replace(/\.[^.]*$/, ".html")
780
+ );
781
+ console.log(htmlPath);
782
+ const compressedHtml = await compressHtml(html, htmlPath);
783
+ await fse.outputFile(htmlPath, compressedHtml);
807
784
  });
808
785
  } catch (error) {
809
- throw new Error(`处理页面JSON文件失败 [lang: ${lang}]: ${error.message}`);
786
+ console.error(`处理页面JSON文件失败 [lang: ${lang}]:`, error);
787
+ throw error;
810
788
  }
811
789
  }
812
790
 
@@ -897,7 +875,8 @@ export async function buildStatic() {
897
875
 
898
876
  console.log("处理语言:", lang, "完成");
899
877
  } catch (error) {
900
- throw new Error(`处理语言 ${lang} 失败: ${error.message}`);
878
+ console.error(`处理语言 ${lang} 失败:`, error);
879
+ throw error;
901
880
  }
902
881
  });
903
882
 
@@ -910,7 +889,8 @@ export async function buildStatic() {
910
889
  const costTime = (Date.now() - startTime) / 1000;
911
890
  console.log("混淆js文件耗时:", costTime, "s");
912
891
  } catch (error) {
913
- throw new Error(`混淆JavaScript文件失败: ${error.message}`);
892
+ console.error(`混淆JavaScript文件失败:`, error);
893
+ throw error;
914
894
  }
915
895
  }
916
896
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pug-site-core",
3
- "version": "3.0.6",
3
+ "version": "3.0.7",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -52,7 +52,7 @@
52
52
  "ws": "^8.18.0"
53
53
  },
54
54
  "license": "ISC",
55
- "description": "静态打包增加初始化函数的调用",
55
+ "description": "增加错误堆栈信息的处理",
56
56
  "files": [
57
57
  "lib/",
58
58
  "index.js"