pug-site-core 3.0.6 → 3.0.8

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.
package/lib/generate.js CHANGED
@@ -11,6 +11,7 @@ import {
11
11
  pathSymbol,
12
12
  obfuscateJavaScript,
13
13
  addTemplateScopeIsolation,
14
+ getJsonData,
14
15
  } from "./utils.js";
15
16
  import _ from "lodash";
16
17
  import async from "async";
@@ -544,29 +545,24 @@ export async function buildStatic() {
544
545
  * @returns {string} 完整的HTML文件路径
545
546
  */
546
547
  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
- }
548
+ if (fileName) {
549
+ const htmlPath = paths.resolveRoot(
550
+ distOutputPath,
551
+ lang,
552
+ outputPath.replace(
553
+ outputPath
554
+ .split("/")
555
+ .pop()
556
+ .replace(/\.[^.]*$/, ""),
557
+ fileName.replace(/\.[^.]*$/, "")
558
+ )
559
+ );
560
+ console.log(htmlPath);
561
+ return htmlPath;
562
+ } else {
563
+ const htmlPath = paths.resolveRoot(distOutputPath, lang, outputPath);
564
564
  console.log(htmlPath);
565
565
  return htmlPath;
566
- } catch (error) {
567
- throw new Error(
568
- `构建HTML路径失败 [lang: ${lang}, outputPath: ${outputPath}, fileName: ${fileName}]: ${error}`
569
- );
570
566
  }
571
567
  }
572
568
 
@@ -574,21 +570,15 @@ export async function buildStatic() {
574
570
  * 生成HTML内容
575
571
  */
576
572
  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
- );
573
+ if (!pagesPugToFn[funName]) {
574
+ throw new Error(`模板函数 ${funName} 不存在`);
591
575
  }
576
+
577
+ return pagesPugToFn[funName]({
578
+ data,
579
+ _pagePath: pagePath,
580
+ common: commonData,
581
+ });
592
582
  }
593
583
 
594
584
  /**
@@ -614,27 +604,21 @@ export async function buildStatic() {
614
604
  const property = name.slice(1, -1);
615
605
 
616
606
  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
- );
607
+ const fileName = dataItem[property];
608
+ if (!fileName) {
609
+ throw new Error(`数据项索引 ${index} 中缺少属性 ${property} 或值为空`);
637
610
  }
611
+
612
+ const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath, fileName);
613
+ const html = generateHtml(
614
+ pagesPugToFn,
615
+ funName,
616
+ dataItem,
617
+ pagePath,
618
+ commonData
619
+ );
620
+ const compressedHtml = await compressHtml(html, htmlPath);
621
+ await fse.outputFile(htmlPath, compressedHtml);
638
622
  });
639
623
  } else {
640
624
  const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath);
@@ -643,9 +627,10 @@ export async function buildStatic() {
643
627
  await fse.outputFile(htmlPath, compressedHtml);
644
628
  }
645
629
  } catch (error) {
646
- throw new Error(
647
- `处理数组数据失败 [lang: ${lang}, getDataFn: ${obj.getDataFn}]: ${error}`
630
+ console.error(
631
+ `处理数组数据失败 [lang: ${lang}, getDataFn: ${obj.getDataFn}]:`, error
648
632
  );
633
+ throw error;
649
634
  }
650
635
  }
651
636
 
@@ -699,8 +684,14 @@ export async function buildStatic() {
699
684
  ) {
700
685
  throw new Error(`数据获取函数 ${obj.getDataFn} 不存在或不是函数`);
701
686
  }
702
-
703
- const data = await getData[obj.getDataFn](lang);
687
+ let getDataFn = getData[obj.getDataFn].bind({
688
+ getR2Data: async (jsonPath) => {
689
+ jsonPath = paths.join(lang, jsonPath);
690
+ let data = await getJsonData(jsonPath);
691
+ return data;
692
+ },
693
+ });
694
+ const data = await getDataFn(lang);
704
695
 
705
696
  // 处理路径和函数名
706
697
  obj.inputPugPath = obj.inputPugPath.replace(/^\//, "");
@@ -733,11 +724,10 @@ export async function buildStatic() {
733
724
  throw new Error(`数据类型不支持: ${typeof data}`);
734
725
  }
735
726
  } catch (error) {
736
- throw new Error(
737
- `处理自定义HTML构建失败 [lang: ${lang}, config: ${JSON.stringify(obj)}]: ${
738
- error
739
- }`
727
+ console.error(
728
+ `处理自定义HTML构建失败 [lang: ${lang}, config: ${JSON.stringify(obj)}]:`,error
740
729
  );
730
+ throw error;
741
731
  }
742
732
  });
743
733
  }
@@ -758,55 +748,50 @@ export async function buildStatic() {
758
748
  ).filter((fileName) => fileName.endsWith(".json"));
759
749
 
760
750
  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
- }
751
+ const data = await fse.readJSON(
752
+ paths.resolveRoot(langDataPath, jsonFileName)
753
+ );
754
+ const pugTemplate = data._template;
770
755
 
771
- const funName = pugTemplate.split(pathSymbol).join("_").slice(0, -4);
756
+ if (!pugTemplate) {
757
+ return;
758
+ }
772
759
 
773
- if (dealWithEndFunName.has(funName)) {
774
- return;
775
- }
760
+ const funName = pugTemplate.split(pathSymbol).join("_").slice(0, -4);
776
761
 
777
- const html = generateHtml(
778
- pagesPugToFn,
779
- funName,
780
- data,
781
- pugTemplate,
782
- commonData
783
- );
762
+ if (dealWithEndFunName.has(funName)) {
763
+ return;
764
+ }
784
765
 
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
- }
766
+ const html = generateHtml(
767
+ pagesPugToFn,
768
+ funName,
769
+ data,
770
+ pugTemplate,
771
+ commonData
772
+ );
793
773
 
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
- );
774
+ // 构建输出路径
775
+ let finalPugTemplate = pugTemplate;
776
+ if (data.page_name) {
777
+ finalPugTemplate =
778
+ pugTemplate.split(pathSymbol).slice(0, -1).join(pathSymbol) +
779
+ pathSymbol +
780
+ data.page_name;
806
781
  }
782
+
783
+ const htmlPath = paths.resolveRoot(
784
+ distOutputPath,
785
+ lang,
786
+ finalPugTemplate.replace(/\.[^.]*$/, ".html")
787
+ );
788
+ console.log(htmlPath);
789
+ const compressedHtml = await compressHtml(html, htmlPath);
790
+ await fse.outputFile(htmlPath, compressedHtml);
807
791
  });
808
792
  } catch (error) {
809
- throw new Error(`处理页面JSON文件失败 [lang: ${lang}]: ${error.message}`);
793
+ console.error(`处理页面JSON文件失败 [lang: ${lang}]:`, error);
794
+ throw error;
810
795
  }
811
796
  }
812
797
 
@@ -897,7 +882,8 @@ export async function buildStatic() {
897
882
 
898
883
  console.log("处理语言:", lang, "完成");
899
884
  } catch (error) {
900
- throw new Error(`处理语言 ${lang} 失败: ${error.message}`);
885
+ console.error(`处理语言 ${lang} 失败:`, error);
886
+ throw error;
901
887
  }
902
888
  });
903
889
 
@@ -910,7 +896,8 @@ export async function buildStatic() {
910
896
  const costTime = (Date.now() - startTime) / 1000;
911
897
  console.log("混淆js文件耗时:", costTime, "s");
912
898
  } catch (error) {
913
- throw new Error(`混淆JavaScript文件失败: ${error.message}`);
899
+ console.error(`混淆JavaScript文件失败:`, error);
900
+ throw error;
914
901
  }
915
902
  }
916
903
 
package/lib/utils.js CHANGED
@@ -383,7 +383,6 @@ export async function getJsonData(jsonDataPath) {
383
383
  return jsonData;
384
384
  }
385
385
 
386
-
387
386
  /**
388
387
  * 为HTML字符串中的template标签添加作用域隔离(优化版本)
389
388
  * @param {string} htmlString - HTML字符串
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.8",
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": "增加getR2Data方法",
56
56
  "files": [
57
57
  "lib/",
58
58
  "index.js"