pug-site-core 3.0.12 → 3.0.14
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/devServer.js +3 -3
- package/lib/generate.js +4 -4
- package/lib/utils.js +39 -0
- package/package.json +2 -2
package/lib/devServer.js
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
pathSymbol,
|
|
12
12
|
getJsonData,
|
|
13
13
|
addTemplateScopeIsolation,
|
|
14
|
+
getFilePathByFunctionName,
|
|
14
15
|
} from "./utils.js";
|
|
15
16
|
import http from "http";
|
|
16
17
|
import WebSocket, { WebSocketServer } from "ws";
|
|
@@ -172,7 +173,7 @@ function setupRoutes(app, wss) {
|
|
|
172
173
|
}
|
|
173
174
|
pugPath = paths.resolveRoot(pagsTemplatePath, lastPath) + ".pug";
|
|
174
175
|
} else {
|
|
175
|
-
pugPath = paths.resolveRoot(pagsTemplatePath, pageInfoObj.pugPath)
|
|
176
|
+
pugPath = paths.resolveRoot(pagsTemplatePath, pageInfoObj.pugPath);
|
|
176
177
|
data = pageInfoObj.data;
|
|
177
178
|
jsonDataPath = "自定义路由数据";
|
|
178
179
|
}
|
|
@@ -258,8 +259,7 @@ async function matchRouter(url, language, device) {
|
|
|
258
259
|
const obj = router[index];
|
|
259
260
|
if (obj.matchFn.call(params)) {
|
|
260
261
|
let data = await obj.getData.call(params);
|
|
261
|
-
let pugPath = obj.getPagesFnName.call(params);
|
|
262
|
-
pugPath = pugPath.split("_").join(pathSymbol);
|
|
262
|
+
let pugPath = await getFilePathByFunctionName(obj.getPagesFnName.call(params));
|
|
263
263
|
return {
|
|
264
264
|
pugPath,
|
|
265
265
|
data,
|
package/lib/generate.js
CHANGED
|
@@ -62,7 +62,7 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
62
62
|
.split(pathSymbol)
|
|
63
63
|
.join("_")
|
|
64
64
|
.slice(0, -4)
|
|
65
|
-
.
|
|
65
|
+
.replaceAll(/[-]/g, "_");
|
|
66
66
|
|
|
67
67
|
// 读取并编译pug文件
|
|
68
68
|
const pugValue = await fse.readFile(filePath, "utf8");
|
|
@@ -153,7 +153,7 @@ export async function generateGetDataFn() {
|
|
|
153
153
|
await async.each(pagesPugFilePathArr, async (fileName) => {
|
|
154
154
|
const funName =
|
|
155
155
|
"get_" +
|
|
156
|
-
fileName.split(pathSymbol).join("_").slice(0, -4).
|
|
156
|
+
fileName.split(pathSymbol).join("_").slice(0, -4).replaceAll(/[-]/g, "_") +
|
|
157
157
|
"_data";
|
|
158
158
|
|
|
159
159
|
// 使用正则表达式检查特定的数据获取函数是否存在
|
|
@@ -346,7 +346,7 @@ export async function fetchDataToJsonFile(args) {
|
|
|
346
346
|
for (const fileName of pugFilePathList) {
|
|
347
347
|
let funName =
|
|
348
348
|
"get_" +
|
|
349
|
-
fileName.split(pathSymbol).join("_").slice(0, -4).
|
|
349
|
+
fileName.split(pathSymbol).join("_").slice(0, -4).replaceAll(/[-]/g, "_") +
|
|
350
350
|
"_data";
|
|
351
351
|
|
|
352
352
|
let jsonFilePath = fileName.slice(0, -4).split(pathSymbol);
|
|
@@ -790,7 +790,7 @@ export async function buildStatic() {
|
|
|
790
790
|
pugTemplate,
|
|
791
791
|
commonData
|
|
792
792
|
);
|
|
793
|
-
|
|
793
|
+
|
|
794
794
|
const compressedHtml = await compressHtml(html, htmlPath);
|
|
795
795
|
await fse.outputFile(htmlPath, compressedHtml);
|
|
796
796
|
});
|
package/lib/utils.js
CHANGED
|
@@ -642,3 +642,42 @@ export function addTemplateScopeIsolation(htmlString, scopePrefix = "xy", option
|
|
|
642
642
|
return htmlString; // 出错时返回原始内容
|
|
643
643
|
}
|
|
644
644
|
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* 根据函数名查找对应的pug文件路径
|
|
648
|
+
* @param {string} functionName - 函数名(可以是模板函数名或数据获取函数名)
|
|
649
|
+
* @returns {Promise<string|null>} 返回对应的pug文件路径,如果未找到则返回null
|
|
650
|
+
*/
|
|
651
|
+
export async function getFilePathByFunctionName(functionName) {
|
|
652
|
+
try {
|
|
653
|
+
// 获取所有pug文件路径
|
|
654
|
+
const pagesPugFilePathArr = await getPagesPugFilePathArr();
|
|
655
|
+
|
|
656
|
+
// 清理函数名,移除get_前缀和_data后缀(如果存在)
|
|
657
|
+
let cleanFunctionName = functionName;
|
|
658
|
+
if (functionName.startsWith("get_") && functionName.endsWith("_data")) {
|
|
659
|
+
// 数据获取函数名格式:get_xxx_data
|
|
660
|
+
cleanFunctionName = functionName.slice(4, -5); // 移除 "get_" 和 "_data"
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// 遍历所有pug文件,找到匹配的文件
|
|
664
|
+
for (const fileName of pagesPugFilePathArr) {
|
|
665
|
+
// 根据fileName生成函数名(与generate.js中的逻辑保持一致)
|
|
666
|
+
const generatedFunctionName = fileName
|
|
667
|
+
.split(pathSymbol)
|
|
668
|
+
.join("_")
|
|
669
|
+
.slice(0, -4) // 移除.pug扩展名
|
|
670
|
+
.replaceAll(/[-]/g, "_");
|
|
671
|
+
|
|
672
|
+
// 检查是否匹配
|
|
673
|
+
if (generatedFunctionName === cleanFunctionName || generatedFunctionName === functionName) {
|
|
674
|
+
return fileName;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
return null;
|
|
679
|
+
} catch (error) {
|
|
680
|
+
console.error("根据函数名查找文件路径失败:", error);
|
|
681
|
+
return null;
|
|
682
|
+
}
|
|
683
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pug-site-core",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.14",
|
|
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": "优自定义路由的pug文件路径获取以及路径bug修复",
|
|
56
56
|
"files": [
|
|
57
57
|
"lib/",
|
|
58
58
|
"index.js"
|