pug-site-core 3.0.16 → 3.0.18

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,7 +11,7 @@ import {
11
11
  pathSymbol,
12
12
  obfuscateJavaScript,
13
13
  addTemplateScopeIsolation,
14
- getJsonData,
14
+ getJsonData
15
15
  } from "./utils.js";
16
16
  import _ from "lodash";
17
17
  import async from "async";
@@ -73,7 +73,7 @@ export async function compilePagesPugToFn(pugPath) {
73
73
  basedir: paths.template.root,
74
74
  compileDebug: true,
75
75
  name: funName,
76
- filters: getCompilePugFilter(),
76
+ filters: getCompilePugFilter()
77
77
  });
78
78
 
79
79
  // 提取函数定义部分
@@ -154,7 +154,11 @@ export async function generateGetDataFn() {
154
154
  await async.each(pagesPugFilePathArr, async (fileName) => {
155
155
  const funName =
156
156
  "get_" +
157
- fileName.split(pathSymbol).join("_").slice(0, -4).replaceAll(/[-]/g, "_") +
157
+ fileName
158
+ .split(pathSymbol)
159
+ .join("_")
160
+ .slice(0, -4)
161
+ .replaceAll(/[-]/g, "_") +
158
162
  "_data";
159
163
 
160
164
  // 使用正则表达式检查特定的数据获取函数是否存在
@@ -163,7 +167,10 @@ export async function generateGetDataFn() {
163
167
  );
164
168
  if (!dataFnRegex.test(getDataFile)) {
165
169
  const templateFn = config.getDataFnTemplate.toString();
166
- const dataFn = `export async ${templateFn.replace("template", funName)}`;
170
+ const dataFn = `export async ${templateFn.replace(
171
+ "template",
172
+ funName
173
+ )}`;
167
174
  await fse.appendFile(getDataPath, dataFn + "\n");
168
175
  }
169
176
  });
@@ -212,15 +219,23 @@ export async function fetchDataToJsonFile(args) {
212
219
  let checkData = (data, language, funName) => {
213
220
  if (data === null || typeof data !== "object") {
214
221
  return Promise.reject(
215
- new Error(`${language} ${funName} 期望返回数组、对象类型返回: ${data}`)
222
+ new Error(
223
+ `${language} ${funName} 期望返回数组、对象类型返回: ${data}`
224
+ )
216
225
  );
217
226
  }
218
227
  if (Array.isArray(data)) {
219
228
  if (data.length === 0) {
220
- return Promise.reject(new Error(`${language} ${funName} 数据为空数组`));
229
+ return Promise.reject(
230
+ new Error(`${language} ${funName} 数据为空数组`)
231
+ );
221
232
  }
222
233
  data.forEach((item, index) => {
223
- if (item === null || typeof item !== "object" || Array.isArray(item)) {
234
+ if (
235
+ item === null ||
236
+ typeof item !== "object" ||
237
+ Array.isArray(item)
238
+ ) {
224
239
  return Promise.reject(
225
240
  new Error(
226
241
  `${language} ${funName} 返回的数据不为对象数组类型返回: ${item} 下标为${index}`
@@ -277,7 +292,9 @@ export async function fetchDataToJsonFile(args) {
277
292
  allTasks.push(async () => {
278
293
  console.log(language, commonFuncName, "开始写入json文件");
279
294
  const commonData = await getData[commonFuncName](language);
280
- let languageData = (await import(paths.languageData)).default[language];
295
+ let languageData = (await import(paths.languageData)).default[
296
+ language
297
+ ];
281
298
  commonData.lang = _.merge(commonData.lang, languageData);
282
299
  await fse.outputJSON(
283
300
  paths.resolveRoot("jsonData", language, "_common.json"),
@@ -299,7 +316,9 @@ export async function fetchDataToJsonFile(args) {
299
316
 
300
317
  let dataFn = getData[obj.getDataFn];
301
318
  if (!dataFn || typeof dataFn !== "function") {
302
- return Promise.reject(new Error(obj.getDataFn + "获取数据函数不存在"));
319
+ return Promise.reject(
320
+ new Error(obj.getDataFn + "获取数据函数不存在")
321
+ );
303
322
  }
304
323
 
305
324
  if (!isFillFun(obj.getDataFn)) {
@@ -347,7 +366,11 @@ export async function fetchDataToJsonFile(args) {
347
366
  for (const fileName of pugFilePathList) {
348
367
  let funName =
349
368
  "get_" +
350
- fileName.split(pathSymbol).join("_").slice(0, -4).replaceAll(/[-]/g, "_") +
369
+ fileName
370
+ .split(pathSymbol)
371
+ .join("_")
372
+ .slice(0, -4)
373
+ .replaceAll(/[-]/g, "_") +
351
374
  "_data";
352
375
 
353
376
  let jsonFilePath = fileName.slice(0, -4).split(pathSymbol);
@@ -373,7 +396,11 @@ export async function fetchDataToJsonFile(args) {
373
396
  item.page_name
374
397
  );
375
398
  } else {
376
- console.warn("下标:", index, "无page_name属性,使用index作为文件名");
399
+ console.warn(
400
+ "下标:",
401
+ index,
402
+ "无page_name属性,使用index作为文件名"
403
+ );
377
404
  lastJsonFilePath =
378
405
  paths.join(language, ...jsonFilePath) +
379
406
  "_" +
@@ -406,7 +433,11 @@ export async function fetchDataToJsonFile(args) {
406
433
  // 等待所有任务完成
407
434
  return new Promise((resolve, reject) => {
408
435
  queue.drain(() => {
409
- console.log("获取数据并写入完成花费:", (Date.now() - starTime) / 1000, "s");
436
+ console.log(
437
+ "获取数据并写入完成花费:",
438
+ (Date.now() - starTime) / 1000,
439
+ "s"
440
+ );
410
441
  resolve();
411
442
  });
412
443
 
@@ -450,14 +481,22 @@ export async function buildFn() {
450
481
  totalCommonData.langCommon = config.commonData;
451
482
  let languageData = (await import(paths.languageData)).default;
452
483
  await async.each(config.languageList, async (lang) => {
453
- let commonData = await (await import(paths.getData))["get_common_data"](lang);
484
+ let commonData = await (
485
+ await import(paths.getData)
486
+ )["get_common_data"](lang);
454
487
  commonData.lang = _.merge(commonData.lang, languageData[lang]);
455
488
  totalCommonData[lang] = commonData;
456
489
  });
457
490
 
458
- const ABTestInfo = await fetchABTestInfo(config.siteConfig.siteAbbr);
459
- if (ABTestInfo) {
460
- totalCommonData.langCommon.ABTestInfo = ABTestInfo;
491
+ if (config.siteConfig?.siteAbbr?.length > 0) {
492
+ const ABTestInfo = await fetchABTestInfo(config.siteConfig.siteAbbr);
493
+ if (ABTestInfo) {
494
+ totalCommonData.langCommon.ABTestInfo = ABTestInfo;
495
+ }
496
+ } else {
497
+ console.warn(
498
+ "config.siteConfig.siteAbbr 配置不存在,无法获取ABTestInfo 将不会开启abtest"
499
+ );
461
500
  }
462
501
 
463
502
  // await fse.copy(jsonDataPath, paths.resolveRoot(outputPath, "data"), {
@@ -478,11 +517,13 @@ export async function buildFn() {
478
517
  }
479
518
  if (config.buildStaticDirArr && config.buildStaticDirArr.length > 0) {
480
519
  return !!config.buildStaticDirArr.find((item) => {
481
- return src.startsWith(paths.resolveRoot(paths.template.static, item));
520
+ return src.startsWith(
521
+ paths.resolveRoot(paths.template.static, item)
522
+ );
482
523
  });
483
524
  }
484
525
  return true;
485
- },
526
+ }
486
527
  }
487
528
  );
488
529
 
@@ -522,7 +563,7 @@ export async function buildStatic() {
522
563
  useShortDoctype: true,
523
564
  removeEmptyAttributes: true,
524
565
  removeOptionalTags: true,
525
- caseSensitive: false,
566
+ caseSensitive: false
526
567
  };
527
568
 
528
569
  /**
@@ -585,7 +626,7 @@ export async function buildStatic() {
585
626
  return pagesPugToFn[funName]({
586
627
  data,
587
628
  _pagePath: pagePath,
588
- common: commonData,
629
+ common: commonData
589
630
  });
590
631
  }
591
632
 
@@ -614,7 +655,9 @@ export async function buildStatic() {
614
655
  await async.eachLimit(data, 128, async (dataItem, index) => {
615
656
  const fileName = dataItem[property];
616
657
  if (!fileName) {
617
- throw new Error(`数据项索引 ${index} 中缺少属性 ${property} 或值为空`);
658
+ throw new Error(
659
+ `数据项索引 ${index} 中缺少属性 ${property} 或值为空`
660
+ );
618
661
  }
619
662
 
620
663
  const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath, fileName);
@@ -630,7 +673,13 @@ export async function buildStatic() {
630
673
  });
631
674
  } else {
632
675
  const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath);
633
- const html = generateHtml(pagesPugToFn, funName, data, pagePath, commonData);
676
+ const html = generateHtml(
677
+ pagesPugToFn,
678
+ funName,
679
+ data,
680
+ pagePath,
681
+ commonData
682
+ );
634
683
  const compressedHtml = await compressHtml(html, htmlPath);
635
684
  await fse.outputFile(htmlPath, compressedHtml);
636
685
  }
@@ -657,7 +706,13 @@ export async function buildStatic() {
657
706
  ) {
658
707
  try {
659
708
  const htmlPath = buildHtmlPath(lang, obj.outPutHtmlPath);
660
- const html = generateHtml(pagesPugToFn, funName, data, pagePath, commonData);
709
+ const html = generateHtml(
710
+ pagesPugToFn,
711
+ funName,
712
+ data,
713
+ pagePath,
714
+ commonData
715
+ );
661
716
  const compressedHtml = await compressHtml(html, htmlPath);
662
717
  await fse.outputFile(htmlPath, compressedHtml);
663
718
  } catch (error) {
@@ -698,7 +753,7 @@ export async function buildStatic() {
698
753
  jsonPath = paths.join(lang, jsonPath);
699
754
  let data = await getJsonData(jsonPath);
700
755
  return data;
701
- },
756
+ }
702
757
  });
703
758
  const data = await getDataFn(lang);
704
759
 
@@ -734,7 +789,9 @@ export async function buildStatic() {
734
789
  }
735
790
  } catch (error) {
736
791
  console.error(
737
- `处理自定义HTML构建失败 [lang: ${lang}, config: ${JSON.stringify(obj)}]:`,
792
+ `处理自定义HTML构建失败 [lang: ${lang}, config: ${JSON.stringify(
793
+ obj
794
+ )}]:`,
738
795
  error
739
796
  );
740
797
  throw error;
@@ -826,11 +883,13 @@ export async function buildStatic() {
826
883
  }
827
884
  if (config.buildStaticDirArr && config.buildStaticDirArr.length > 0) {
828
885
  return !!config.buildStaticDirArr.find((item) => {
829
- return src.startsWith(paths.resolveRoot(paths.template.static, item));
886
+ return src.startsWith(
887
+ paths.resolveRoot(paths.template.static, item)
888
+ );
830
889
  });
831
890
  }
832
891
  return true;
833
- },
892
+ }
834
893
  }
835
894
  );
836
895
 
@@ -992,7 +1051,7 @@ export async function createDebugTemplate() {
992
1051
  /[+\-*/%]/,
993
1052
  /\brandom\b|\bfloor\b|\bceil\b|\bround\b/,
994
1053
  /\.\w+\(/, // 方法调用
995
- /\$\{[^}]*[+\-*/%.][^}]*\}/, // 模板字符串中的计算
1054
+ /\$\{[^}]*[+\-*/%.][^}]*\}/ // 模板字符串中的计算
996
1055
  ];
997
1056
 
998
1057
  return dynamicPatterns.some((pattern) => pattern.test(code));
@@ -1017,7 +1076,9 @@ export async function createDebugTemplate() {
1017
1076
  }
1018
1077
 
1019
1078
  // 匹配带反引号的模板字符串 `${common.lang.xxx}`
1020
- const backquoteLangMatch = code.match(/`.*\$\{common\.lang\.([^}]+)\}.*`/);
1079
+ const backquoteLangMatch = code.match(
1080
+ /`.*\$\{common\.lang\.([^}]+)\}.*`/
1081
+ );
1021
1082
  if (backquoteLangMatch) {
1022
1083
  return backquoteLangMatch[1];
1023
1084
  }
@@ -1048,7 +1109,7 @@ export async function createDebugTemplate() {
1048
1109
  textContent: "",
1049
1110
  langPath: "",
1050
1111
  hasStaticText: false,
1051
- hasLangText: false,
1112
+ hasLangText: false
1052
1113
  };
1053
1114
 
1054
1115
  // 分析标签的子节点来确定文本内容
@@ -1144,7 +1205,7 @@ export async function createDebugTemplate() {
1144
1205
  "script",
1145
1206
  "base",
1146
1207
  "noscript",
1147
- "template",
1208
+ "template"
1148
1209
  ]);
1149
1210
 
1150
1211
  // 处理每一行
@@ -1175,7 +1236,7 @@ export async function createDebugTemplate() {
1175
1236
  // 构建调试属性
1176
1237
  const debugAttrs = [
1177
1238
  `data-debug-file="${relativePath}"`,
1178
- `data-debug-line="${lineNumber}"`,
1239
+ `data-debug-line="${lineNumber}"`
1179
1240
  ];
1180
1241
 
1181
1242
  // 只有可编辑的元素才添加 data-debug-editable 属性
@@ -1217,8 +1278,12 @@ export async function createDebugTemplate() {
1217
1278
  const lastParenIndex = afterFirstParen.lastIndexOf(")");
1218
1279
 
1219
1280
  if (lastParenIndex !== -1) {
1220
- const existingAttrs = afterFirstParen.substring(0, lastParenIndex);
1221
- const afterLastParen = afterFirstParen.substring(lastParenIndex);
1281
+ const existingAttrs = afterFirstParen.substring(
1282
+ 0,
1283
+ lastParenIndex
1284
+ );
1285
+ const afterLastParen =
1286
+ afterFirstParen.substring(lastParenIndex);
1222
1287
 
1223
1288
  const separator = existingAttrs.trim() ? ", " : "";
1224
1289
  processedLine = `${indent}${beforeParen}${existingAttrs}${separator}${debugAttrsString}${afterLastParen}`;
@@ -1244,7 +1309,10 @@ export async function createDebugTemplate() {
1244
1309
  const parenEnd = findMatchingParen(trimmedLine, tagEndIndex);
1245
1310
 
1246
1311
  if (parenEnd !== -1) {
1247
- const tagWithModifiers = trimmedLine.substring(0, tagEndIndex);
1312
+ const tagWithModifiers = trimmedLine.substring(
1313
+ 0,
1314
+ tagEndIndex
1315
+ );
1248
1316
  const existingAttrs = trimmedLine.substring(
1249
1317
  tagEndIndex + 1,
1250
1318
  parenEnd
package/lib/utils.js CHANGED
@@ -690,7 +690,7 @@ export async function getFilePathByFunctionName(functionName) {
690
690
  */
691
691
  export async function fetchABTestInfo(siteAbbr) {
692
692
  try {
693
- const res = await axios.get("https://new.sp.com/open-api/abtest-info", {
693
+ const res = await axios.get("http://new.sp.com/open-api/abtest-info", {
694
694
  params: { site: siteAbbr },
695
695
  });
696
696
  return (res && res.data && res.data.result) ? res.data.result : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pug-site-core",
3
- "version": "3.0.16",
3
+ "version": "3.0.18",
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": "增加ABTest数据获取的接口",
55
+ "description": "siteAbbr可选配置,添加会在common.langCommon.ABTestInfo中添加ABTestInfo数据开启abtest,否则不会添加",
56
56
  "files": [
57
57
  "lib/",
58
58
  "index.js"
@@ -60,4 +60,4 @@
60
60
  "exports": {
61
61
  ".": "./index.js"
62
62
  }
63
- }
63
+ }