pug-site-core 1.0.1
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/index.js +4 -0
- package/lib/devServer.js +232 -0
- package/lib/generate.js +674 -0
- package/lib/pugRuntime.js +1 -0
- package/lib/translate.js +98 -0
- package/lib/utils.js +374 -0
- package/lib/watchFile.js +32 -0
- package/package.json +34 -0
package/index.js
ADDED
package/lib/devServer.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import useragent from "express-useragent";
|
|
3
|
+
import ip from "ip";
|
|
4
|
+
import fse from "fs-extra";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import _ from "lodash";
|
|
7
|
+
import {
|
|
8
|
+
getCompilePugFilter,
|
|
9
|
+
pagesPathFilter,
|
|
10
|
+
getIdleProt,
|
|
11
|
+
matchESI,
|
|
12
|
+
pathSymbol
|
|
13
|
+
} from "./utils.js";
|
|
14
|
+
import http from "http";
|
|
15
|
+
import WebSocket, { WebSocketServer } from "ws";
|
|
16
|
+
import { pathToFileURL } from "url";
|
|
17
|
+
|
|
18
|
+
const projectRoot = process.cwd();
|
|
19
|
+
const configPath = pathToFileURL(path.resolve(projectRoot, "config.js")).href;
|
|
20
|
+
const { config } = await import(configPath);
|
|
21
|
+
const pagsTemplatePath = path.join(projectRoot, "/template/pages");
|
|
22
|
+
const localIp = ip.address();
|
|
23
|
+
const port = await getIdleProt(config.devServer.port, localIp);
|
|
24
|
+
process.env._port = port;
|
|
25
|
+
process.env._localIp = localIp;
|
|
26
|
+
|
|
27
|
+
function createServer() {
|
|
28
|
+
const app = express();
|
|
29
|
+
const server = http.createServer(app);
|
|
30
|
+
const wss = new WebSocketServer({ server });
|
|
31
|
+
|
|
32
|
+
setupWebSocket(wss);
|
|
33
|
+
setupMiddleware(app);
|
|
34
|
+
setupRoutes(app, wss);
|
|
35
|
+
|
|
36
|
+
return server;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function setupWebSocket(wss) {
|
|
40
|
+
wss.on("connection", function connection(ws) {
|
|
41
|
+
console.log("刷新网页");
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function setupMiddleware(app) {
|
|
46
|
+
app.use(useragent.express());
|
|
47
|
+
app.set("views", config.templatePath);
|
|
48
|
+
app.set("view engine", "pug");
|
|
49
|
+
app.use(
|
|
50
|
+
"/static",
|
|
51
|
+
express.static(path.join(projectRoot, "/template/static"))
|
|
52
|
+
);
|
|
53
|
+
app.use(express.static(path.join(projectRoot, "public")));
|
|
54
|
+
app.locals.basedir = path.join(projectRoot, "/template");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function setupRoutes(app, wss) {
|
|
58
|
+
app.get("/_refresh", (req, res) => {
|
|
59
|
+
wss.clients.forEach(function each(client) {
|
|
60
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
61
|
+
client.send("refresh");
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
res.send("刷新成功");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
app.get("*", async (req, res) => {
|
|
68
|
+
try {
|
|
69
|
+
let useragent = req.useragent;
|
|
70
|
+
let device;
|
|
71
|
+
if (useragent.isDesktop) {
|
|
72
|
+
device = "pc";
|
|
73
|
+
} else if (useragent.isiPad) {
|
|
74
|
+
//开发者工具中仅ipad mini会被认为是ipad
|
|
75
|
+
device = "ipad";
|
|
76
|
+
} else if (useragent.isMobile) {
|
|
77
|
+
device = "mobile";
|
|
78
|
+
}
|
|
79
|
+
let language = config.languageList[0];
|
|
80
|
+
let lastPath = pagesPathFilter(req.path);
|
|
81
|
+
let data;
|
|
82
|
+
let jsonDataPath;
|
|
83
|
+
let pugPath;
|
|
84
|
+
let findPageInfoObj = await matchFileMapTable(lastPath, language, device);
|
|
85
|
+
|
|
86
|
+
let otherPath = [language + "/" + device, language, device, ""];
|
|
87
|
+
if (!findPageInfoObj) {
|
|
88
|
+
if (lastPath.endsWith(".html")) {
|
|
89
|
+
lastPath = lastPath.slice(0, -5);
|
|
90
|
+
} else {
|
|
91
|
+
lastPath = path.join(lastPath, "index");
|
|
92
|
+
}
|
|
93
|
+
jsonDataPath =
|
|
94
|
+
path.join(projectRoot, "jsonData", language, lastPath) + ".json";
|
|
95
|
+
if (fse.pathExistsSync(jsonDataPath)) {
|
|
96
|
+
data = await fse.readJSON(jsonDataPath);
|
|
97
|
+
} else {
|
|
98
|
+
console.log(jsonDataPath, "不存在此json文件页面data数据将为null");
|
|
99
|
+
jsonDataPath = null;
|
|
100
|
+
}
|
|
101
|
+
for (let index = 0; index < otherPath.length; index++) {
|
|
102
|
+
const element = otherPath[index];
|
|
103
|
+
if (data) {
|
|
104
|
+
lastPath = pagesPathFilter(data._template[0]).replace(".pug", "");
|
|
105
|
+
}
|
|
106
|
+
pugPath = path.join(pagsTemplatePath, element, lastPath) + ".pug";
|
|
107
|
+
if (fse.pathExistsSync(pugPath)) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
for (let index = 0; index < otherPath.length; index++) {
|
|
113
|
+
const element = otherPath[index];
|
|
114
|
+
pugPath = path.join(
|
|
115
|
+
pagsTemplatePath,
|
|
116
|
+
element,
|
|
117
|
+
findPageInfoObj.pugPath
|
|
118
|
+
);
|
|
119
|
+
if (fse.pathExistsSync(pugPath)) {
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
data = findPageInfoObj.data;
|
|
124
|
+
jsonDataPath = findPageInfoObj.getDataFn;
|
|
125
|
+
}
|
|
126
|
+
if (fse.pathExistsSync(pugPath)) {
|
|
127
|
+
console.log(
|
|
128
|
+
`请求路径:${req.path} 模版路径:${pugPath} 数据JSON文件路径或getData中的函数名:${jsonDataPath}`
|
|
129
|
+
);
|
|
130
|
+
let commonData = {};
|
|
131
|
+
if (config.languageFileChangeUpdateCommon) {
|
|
132
|
+
const getDataPath = pathToFileURL(
|
|
133
|
+
path.resolve(projectRoot, "getData.js")
|
|
134
|
+
).href;
|
|
135
|
+
commonData = await (
|
|
136
|
+
await import(getDataPath)
|
|
137
|
+
)["get_common_data"](language);
|
|
138
|
+
await fse.writeJSON(
|
|
139
|
+
path.join(projectRoot, "jsonData", language, "_common.json"),
|
|
140
|
+
commonData
|
|
141
|
+
);
|
|
142
|
+
} else {
|
|
143
|
+
commonData = await fse.readJSON(
|
|
144
|
+
path.join(projectRoot, "jsonData", language, "_common.json")
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
let _refreshScript = `<script>const ws=new WebSocket('ws://${localIp}:${port}');ws.onmessage=function(event){if(event.data==='refresh'){console.log('Refreshing page...');location.reload()}}</script>`;
|
|
148
|
+
res.render(
|
|
149
|
+
pugPath,
|
|
150
|
+
_.merge(
|
|
151
|
+
{
|
|
152
|
+
data,
|
|
153
|
+
_pagePath: pugPath.split(pathSymbol + "pages")[1],
|
|
154
|
+
common: _.merge(commonData, config.commonData)
|
|
155
|
+
},
|
|
156
|
+
{ filters: getCompilePugFilter() }
|
|
157
|
+
),
|
|
158
|
+
async function (err, html) {
|
|
159
|
+
if (err) {
|
|
160
|
+
console.log(err);
|
|
161
|
+
res.send(_refreshScript + err);
|
|
162
|
+
} else {
|
|
163
|
+
if (config.isMatchEsi) {
|
|
164
|
+
html = await matchESI(html, data);
|
|
165
|
+
}
|
|
166
|
+
res.send(_refreshScript + html);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
} else {
|
|
171
|
+
let html = `<h1>${pugPath}的模版函数不存在!</h1>`;
|
|
172
|
+
console.log(`${pugPath}的模版函数不存在!`);
|
|
173
|
+
res.send(html);
|
|
174
|
+
}
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.log(error);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function matchFileMapTable(reqPath, language, device) {
|
|
182
|
+
let fileMapTable = config.fileMapTable.filter((obj) => {
|
|
183
|
+
let flag = obj.devMatchFn && obj.pugPath && obj.outPutPath && obj.getDataFn;
|
|
184
|
+
if (obj.languageList && !obj.languageList.includes(language)) {
|
|
185
|
+
flag = false;
|
|
186
|
+
}
|
|
187
|
+
if (obj.deviceList && !obj.deviceList.includes(device)) {
|
|
188
|
+
flag = false;
|
|
189
|
+
}
|
|
190
|
+
return flag;
|
|
191
|
+
});
|
|
192
|
+
for (let index = 0; index < fileMapTable.length; index++) {
|
|
193
|
+
const obj = fileMapTable[index];
|
|
194
|
+
if (obj.devMatchFn(reqPath, device)) {
|
|
195
|
+
const getDataPath = pathToFileURL(
|
|
196
|
+
path.resolve(projectRoot, "getData.js")
|
|
197
|
+
).href;
|
|
198
|
+
const getData = await import(getDataPath);
|
|
199
|
+
let data = await getData[obj.getDataFn](language);
|
|
200
|
+
if (Array.isArray(data)) {
|
|
201
|
+
let name = obj.outPutPath.split("/").pop().replace(/\..*$/, "");
|
|
202
|
+
const regex = /^\[.+\]$/;
|
|
203
|
+
if (regex.test(name)) {
|
|
204
|
+
let property = name.slice(1, -1);
|
|
205
|
+
data = data.find((item) => {
|
|
206
|
+
let str = String(item[property]);
|
|
207
|
+
return reqPath.includes(str);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
pugPath: obj.pugPath,
|
|
213
|
+
data,
|
|
214
|
+
getDataFn: obj.getDataFn
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export async function startDevServer() {
|
|
221
|
+
const server = createServer();
|
|
222
|
+
|
|
223
|
+
console.log(
|
|
224
|
+
"Listening:",
|
|
225
|
+
`http://${localIp}:${port}`,
|
|
226
|
+
"语言为:",
|
|
227
|
+
config.languageList[0]
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
server.listen(port);
|
|
231
|
+
return server;
|
|
232
|
+
}
|