pug-site-core 1.0.7 → 1.0.9
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 +35 -0
- package/lib/devServer.js +16 -31
- package/lib/generate.js +82 -83
- package/lib/paths.js +38 -0
- package/lib/utils.js +3 -6
- package/lib/watchFile.js +2 -5
- package/package.json +14 -4
package/index.js
CHANGED
|
@@ -17,3 +17,38 @@ export const pugSiteCore = {
|
|
|
17
17
|
buildStatic,
|
|
18
18
|
translateLanguageData
|
|
19
19
|
};
|
|
20
|
+
|
|
21
|
+
let curCmd = process.env.npm_lifecycle_event;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
switch (curCmd) {
|
|
25
|
+
case "getFun":
|
|
26
|
+
await generateGetDataFn();
|
|
27
|
+
break;
|
|
28
|
+
case "getData":
|
|
29
|
+
await generateGetDataFn();
|
|
30
|
+
const args = process.argv.slice(2);
|
|
31
|
+
await fetchDataToJsonFile(args);
|
|
32
|
+
break;
|
|
33
|
+
case "dev":
|
|
34
|
+
startDevServer();
|
|
35
|
+
break;
|
|
36
|
+
case "compileFn":
|
|
37
|
+
await compilePagesPugToFn();
|
|
38
|
+
break;
|
|
39
|
+
case "buildFn":
|
|
40
|
+
await buildFn();
|
|
41
|
+
break;
|
|
42
|
+
case "buildStatic":
|
|
43
|
+
await buildStatic();
|
|
44
|
+
break;
|
|
45
|
+
case "lang":
|
|
46
|
+
await translateLanguageData();
|
|
47
|
+
break;
|
|
48
|
+
default:
|
|
49
|
+
console.log(`未知的命令: ${curCmd}`);
|
|
50
|
+
}
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error(`执行命令 ${curCmd} 时发生错误:`, error);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
package/lib/devServer.js
CHANGED
|
@@ -2,7 +2,6 @@ import express from "express";
|
|
|
2
2
|
import useragent from "express-useragent";
|
|
3
3
|
import ip from "ip";
|
|
4
4
|
import fse from "fs-extra";
|
|
5
|
-
import path from "path";
|
|
6
5
|
import _ from "lodash";
|
|
7
6
|
import {
|
|
8
7
|
getCompilePugFilter,
|
|
@@ -13,13 +12,11 @@ import {
|
|
|
13
12
|
} from "./utils.js";
|
|
14
13
|
import http from "http";
|
|
15
14
|
import WebSocket, { WebSocketServer } from "ws";
|
|
16
|
-
import { pathToFileURL, fileURLToPath } from "url";
|
|
17
15
|
import { Worker } from "worker_threads";
|
|
16
|
+
import { paths } from "./paths.js";
|
|
18
17
|
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const { config } = await import(configPath);
|
|
22
|
-
const pagsTemplatePath = path.join(projectRoot, "/template/pages");
|
|
18
|
+
const { config } = await import(paths.config);
|
|
19
|
+
const pagsTemplatePath = paths.template.pages;
|
|
23
20
|
const localIp = ip.address();
|
|
24
21
|
const port = await getIdleProt(config.devServer.port, localIp);
|
|
25
22
|
process.env._port = port;
|
|
@@ -47,12 +44,9 @@ function setupMiddleware(app) {
|
|
|
47
44
|
app.use(useragent.express());
|
|
48
45
|
app.set("views", config.templatePath);
|
|
49
46
|
app.set("view engine", "pug");
|
|
50
|
-
app.use(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
);
|
|
54
|
-
app.use(express.static(path.join(projectRoot, "public")));
|
|
55
|
-
app.locals.basedir = path.join(projectRoot, "/template");
|
|
47
|
+
app.use("/static", express.static(paths.template.static));
|
|
48
|
+
app.use(express.static(paths.public));
|
|
49
|
+
app.locals.basedir = paths.template.root;
|
|
56
50
|
}
|
|
57
51
|
|
|
58
52
|
function setupRoutes(app, wss) {
|
|
@@ -89,10 +83,10 @@ function setupRoutes(app, wss) {
|
|
|
89
83
|
if (lastPath.endsWith(".html")) {
|
|
90
84
|
lastPath = lastPath.slice(0, -5);
|
|
91
85
|
} else {
|
|
92
|
-
lastPath =
|
|
86
|
+
lastPath = lastPath + "/index";
|
|
93
87
|
}
|
|
94
88
|
jsonDataPath =
|
|
95
|
-
|
|
89
|
+
paths.resolveRoot("jsonData", language, lastPath) + ".json";
|
|
96
90
|
if (fse.pathExistsSync(jsonDataPath)) {
|
|
97
91
|
data = await fse.readJSON(jsonDataPath);
|
|
98
92
|
} else {
|
|
@@ -104,7 +98,8 @@ function setupRoutes(app, wss) {
|
|
|
104
98
|
if (data) {
|
|
105
99
|
lastPath = pagesPathFilter(data._template[0]).replace(".pug", "");
|
|
106
100
|
}
|
|
107
|
-
pugPath =
|
|
101
|
+
pugPath =
|
|
102
|
+
paths.resolveRoot(pagsTemplatePath, element, lastPath) + ".pug";
|
|
108
103
|
if (fse.pathExistsSync(pugPath)) {
|
|
109
104
|
break;
|
|
110
105
|
}
|
|
@@ -112,7 +107,7 @@ function setupRoutes(app, wss) {
|
|
|
112
107
|
} else {
|
|
113
108
|
for (let index = 0; index < otherPath.length; index++) {
|
|
114
109
|
const element = otherPath[index];
|
|
115
|
-
pugPath =
|
|
110
|
+
pugPath = paths.resolveRoot(
|
|
116
111
|
pagsTemplatePath,
|
|
117
112
|
element,
|
|
118
113
|
findPageInfoObj.pugPath
|
|
@@ -130,19 +125,16 @@ function setupRoutes(app, wss) {
|
|
|
130
125
|
);
|
|
131
126
|
let commonData = {};
|
|
132
127
|
if (config.langChangeUpdateCommon) {
|
|
133
|
-
const getDataPath = pathToFileURL(
|
|
134
|
-
path.resolve(projectRoot, "getData.js")
|
|
135
|
-
).href;
|
|
136
128
|
commonData = await (
|
|
137
|
-
await import(
|
|
129
|
+
await import(paths.getData)
|
|
138
130
|
)["get_common_data"](language);
|
|
139
131
|
await fse.writeJSON(
|
|
140
|
-
|
|
132
|
+
paths.resolveRoot("jsonData", language, "_common.json"),
|
|
141
133
|
commonData
|
|
142
134
|
);
|
|
143
135
|
} else {
|
|
144
136
|
commonData = await fse.readJSON(
|
|
145
|
-
|
|
137
|
+
paths.resolveRoot("jsonData", language, "_common.json")
|
|
146
138
|
);
|
|
147
139
|
}
|
|
148
140
|
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>`;
|
|
@@ -193,10 +185,7 @@ async function matchFileMapTable(reqPath, language, device) {
|
|
|
193
185
|
for (let index = 0; index < fileMapTable.length; index++) {
|
|
194
186
|
const obj = fileMapTable[index];
|
|
195
187
|
if (obj.devMatchFn(reqPath, device)) {
|
|
196
|
-
const
|
|
197
|
-
path.resolve(projectRoot, "getData.js")
|
|
198
|
-
).href;
|
|
199
|
-
const getData = await import(getDataPath);
|
|
188
|
+
const getData = await import(paths.getData);
|
|
200
189
|
let data = await getData[obj.getDataFn](language);
|
|
201
190
|
if (Array.isArray(data)) {
|
|
202
191
|
let name = obj.outPutPath.split("/").pop().replace(/\..*$/, "");
|
|
@@ -230,9 +219,5 @@ export async function startDevServer() {
|
|
|
230
219
|
|
|
231
220
|
server.listen(port);
|
|
232
221
|
|
|
233
|
-
|
|
234
|
-
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
235
|
-
// 构建 watchFile.js 的完整路径
|
|
236
|
-
const watchFilePath = path.join(currentDir, "watchFile.js");
|
|
237
|
-
new Worker(watchFilePath);
|
|
222
|
+
new Worker(paths.lib + "/watchFile.js");
|
|
238
223
|
}
|
package/lib/generate.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import fse from "fs-extra";
|
|
2
2
|
import pug from "pug";
|
|
3
|
-
import path from "path";
|
|
4
3
|
import {
|
|
5
4
|
getPagesPugFilePathArr,
|
|
6
5
|
getCompilePugFilter,
|
|
@@ -13,14 +12,9 @@ import {
|
|
|
13
12
|
import _ from "lodash";
|
|
14
13
|
import async from "async";
|
|
15
14
|
import UglifyJS from "uglify-js";
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
const projectRoot = process.cwd();
|
|
19
|
-
const configPath = pathToFileURL(path.resolve(projectRoot, "config.js")).href;
|
|
20
|
-
const { config } = await import(configPath);
|
|
21
|
-
|
|
22
|
-
const pugRootPath = path.join(projectRoot, "template/pages");
|
|
15
|
+
import { paths } from "./paths.js";
|
|
23
16
|
|
|
17
|
+
const { config } = await import(paths.config);
|
|
24
18
|
/**
|
|
25
19
|
* 将pages目录下的pug模板编译为JS函数
|
|
26
20
|
* @param {string} pugPath - 指定编译的pug文件路径(相对于/template/pages)
|
|
@@ -31,16 +25,15 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
31
25
|
try {
|
|
32
26
|
// 获取所有需要编译的pug文件路径
|
|
33
27
|
const pagesPugFilePathArr = await getPagesPugFilePathArr();
|
|
34
|
-
const fnRootPath = path.join(projectRoot, "/pagesPugFn");
|
|
35
28
|
|
|
36
|
-
// 获取当前文件的目录路径
|
|
37
|
-
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
38
29
|
// 读取pug运行时代码作为基础代码
|
|
39
|
-
const
|
|
40
|
-
const lastPugFnStr = await fse.readFile(pugRuntimePath, "utf8");
|
|
30
|
+
const lastPugFnStr = await fse.readFile(paths.pugRuntime, "utf8");
|
|
41
31
|
|
|
42
32
|
// 验证指定路径是否存在
|
|
43
|
-
if (
|
|
33
|
+
if (
|
|
34
|
+
pugPath &&
|
|
35
|
+
!fse.pathExistsSync(paths.resolveRoot(paths.template.pages, pugPath))
|
|
36
|
+
) {
|
|
44
37
|
throw new Error("路径不存在! 注意路径前面会自动拼接/template/pages");
|
|
45
38
|
}
|
|
46
39
|
|
|
@@ -54,14 +47,14 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
54
47
|
),
|
|
55
48
|
10, // 限制并发数为10
|
|
56
49
|
async (fileName) => {
|
|
57
|
-
const filePath =
|
|
50
|
+
const filePath = paths.resolveRoot(paths.template.pages, fileName);
|
|
58
51
|
const funName = fileName.split(pathSymbol).join("_").slice(0, -4);
|
|
59
52
|
|
|
60
53
|
// 读取并编译pug文件
|
|
61
54
|
const pugValue = await fse.readFile(filePath, "utf8");
|
|
62
55
|
const fnStr = pug.compileClient(pugValue, {
|
|
63
56
|
filename: filePath,
|
|
64
|
-
basedir:
|
|
57
|
+
basedir: paths.template.root,
|
|
65
58
|
compileDebug: true,
|
|
66
59
|
name: funName,
|
|
67
60
|
filters: getCompilePugFilter()
|
|
@@ -93,7 +86,7 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
93
86
|
}
|
|
94
87
|
|
|
95
88
|
// 写入最终文件
|
|
96
|
-
const outputPath =
|
|
89
|
+
const outputPath = paths.resolveRoot("pagesPugFn", "index.js");
|
|
97
90
|
await fse.ensureFile(outputPath);
|
|
98
91
|
await fse.writeFile(outputPath, result.code);
|
|
99
92
|
} catch (error) {
|
|
@@ -109,7 +102,7 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
109
102
|
*/
|
|
110
103
|
export async function generateGetDataFn() {
|
|
111
104
|
try {
|
|
112
|
-
const getDataPath =
|
|
105
|
+
const getDataPath = paths.resolveRoot("getData.js");
|
|
113
106
|
const getDataFile = await fse.readFile(getDataPath, "utf8");
|
|
114
107
|
const pagesPugFilePathArr = await getPagesPugFilePathArr(true);
|
|
115
108
|
|
|
@@ -146,7 +139,7 @@ export async function generateGetDataFn() {
|
|
|
146
139
|
*/
|
|
147
140
|
export async function fetchDataToJsonFile(args) {
|
|
148
141
|
try {
|
|
149
|
-
const JsonRootPath =
|
|
142
|
+
const JsonRootPath = paths.resolveRoot("jsonData");
|
|
150
143
|
|
|
151
144
|
// 解析过滤参数
|
|
152
145
|
let filterFun = [];
|
|
@@ -168,10 +161,7 @@ export async function fetchDataToJsonFile(args) {
|
|
|
168
161
|
await fse.remove(JsonRootPath);
|
|
169
162
|
}
|
|
170
163
|
|
|
171
|
-
const
|
|
172
|
-
path.resolve(projectRoot, "getData.js")
|
|
173
|
-
).href;
|
|
174
|
-
const getData = await import(getDataPath);
|
|
164
|
+
const getData = await import(paths.getData);
|
|
175
165
|
let arrPagesPugFilePathArr = await getPagesPugFilePathArr();
|
|
176
166
|
let pagesPugFilePathArr = await getPagesPugFilePathArr(true);
|
|
177
167
|
let filterFinishArr = arrPagesPugFilePathArr.filter(
|
|
@@ -191,7 +181,7 @@ export async function fetchDataToJsonFile(args) {
|
|
|
191
181
|
|
|
192
182
|
// 清空指定语言的数据目录
|
|
193
183
|
if (filterLang.includes(language) && !filterFun.length) {
|
|
194
|
-
await fse.remove(
|
|
184
|
+
await fse.remove(paths.resolveRoot("jsonData", language));
|
|
195
185
|
}
|
|
196
186
|
|
|
197
187
|
// 处理公共数据
|
|
@@ -200,7 +190,7 @@ export async function fetchDataToJsonFile(args) {
|
|
|
200
190
|
const commonData = await getData[commonFuncName](language);
|
|
201
191
|
console.log(language, commonFuncName, "开始写入json文件");
|
|
202
192
|
await fse.outputJSON(
|
|
203
|
-
|
|
193
|
+
paths.resolveRoot("jsonData", language, "_common.json"),
|
|
204
194
|
commonData
|
|
205
195
|
);
|
|
206
196
|
}
|
|
@@ -260,8 +250,8 @@ export async function fetchDataToJsonFile(args) {
|
|
|
260
250
|
`命名但是${index}下标中对象属性不存在`
|
|
261
251
|
);
|
|
262
252
|
}
|
|
263
|
-
jsonFilePath =
|
|
264
|
-
|
|
253
|
+
jsonFilePath = paths.resolveRoot(
|
|
254
|
+
"jsonData",
|
|
265
255
|
language,
|
|
266
256
|
outPutPath.replace(name, fileName)
|
|
267
257
|
);
|
|
@@ -269,12 +259,20 @@ export async function fetchDataToJsonFile(args) {
|
|
|
269
259
|
await fse.outputJson(jsonFilePath, dataItem);
|
|
270
260
|
}
|
|
271
261
|
} else {
|
|
272
|
-
jsonFilePath =
|
|
262
|
+
jsonFilePath = paths.resolveRoot(
|
|
263
|
+
"jsonData",
|
|
264
|
+
language,
|
|
265
|
+
outPutPath
|
|
266
|
+
);
|
|
273
267
|
await fse.remove(jsonFilePath);
|
|
274
268
|
await fse.outputJson(jsonFilePath, data);
|
|
275
269
|
}
|
|
276
270
|
} else if (typeof data === "object") {
|
|
277
|
-
jsonFilePath =
|
|
271
|
+
jsonFilePath = paths.resolveRoot(
|
|
272
|
+
"jsonData",
|
|
273
|
+
language,
|
|
274
|
+
outPutPath
|
|
275
|
+
);
|
|
278
276
|
await fse.remove(jsonFilePath);
|
|
279
277
|
await fse.outputJson(jsonFilePath, data);
|
|
280
278
|
}
|
|
@@ -308,15 +306,15 @@ export async function fetchDataToJsonFile(args) {
|
|
|
308
306
|
}
|
|
309
307
|
let lastJsonFilePath;
|
|
310
308
|
if (item.page_name && item.page_name.length > 0) {
|
|
311
|
-
lastJsonFilePath =
|
|
312
|
-
|
|
309
|
+
lastJsonFilePath = paths.resolveRoot(
|
|
310
|
+
"jsonData",
|
|
313
311
|
language,
|
|
314
312
|
...jsonFilePath.slice(0, -1),
|
|
315
313
|
item.page_name
|
|
316
314
|
);
|
|
317
315
|
} else {
|
|
318
316
|
lastJsonFilePath =
|
|
319
|
-
|
|
317
|
+
paths.resolveRoot("jsonData", language, ...jsonFilePath) +
|
|
320
318
|
"_" +
|
|
321
319
|
++index +
|
|
322
320
|
".json";
|
|
@@ -324,7 +322,11 @@ export async function fetchDataToJsonFile(args) {
|
|
|
324
322
|
let templateArr = filterFinishArr.filter(
|
|
325
323
|
(item) => pagesPathFilter(item) === fileName
|
|
326
324
|
);
|
|
327
|
-
if (
|
|
325
|
+
if (
|
|
326
|
+
fse.pathExistsSync(
|
|
327
|
+
paths.resolveRoot(paths.template.pages, fileName)
|
|
328
|
+
)
|
|
329
|
+
) {
|
|
328
330
|
templateArr.unshift(fileName);
|
|
329
331
|
}
|
|
330
332
|
item._template = templateArr;
|
|
@@ -334,20 +336,25 @@ export async function fetchDataToJsonFile(args) {
|
|
|
334
336
|
} else if (data && typeof data === "object" && !Array.isArray(data)) {
|
|
335
337
|
console.log(language, funName, "开始写入json文件");
|
|
336
338
|
if (data.page_name && data.page_name.length > 0) {
|
|
337
|
-
jsonFilePath =
|
|
338
|
-
|
|
339
|
+
jsonFilePath = paths.resolveRoot(
|
|
340
|
+
"jsonData",
|
|
339
341
|
language,
|
|
340
342
|
...jsonFilePath.slice(0, -1),
|
|
341
343
|
data.page_name
|
|
342
344
|
);
|
|
343
345
|
} else {
|
|
344
346
|
jsonFilePath =
|
|
345
|
-
|
|
347
|
+
paths.resolveRoot("jsonData", language, ...jsonFilePath) +
|
|
348
|
+
".json";
|
|
346
349
|
}
|
|
347
350
|
let templateArr = filterFinishArr.filter(
|
|
348
351
|
(item) => pagesPathFilter(item) === fileName
|
|
349
352
|
);
|
|
350
|
-
if (
|
|
353
|
+
if (
|
|
354
|
+
fse.pathExistsSync(
|
|
355
|
+
paths.resolveRoot(paths.template.pages, fileName)
|
|
356
|
+
)
|
|
357
|
+
) {
|
|
351
358
|
templateArr.unshift(fileName);
|
|
352
359
|
}
|
|
353
360
|
data._template = templateArr;
|
|
@@ -366,7 +373,7 @@ export async function fetchDataToJsonFile(args) {
|
|
|
366
373
|
}
|
|
367
374
|
|
|
368
375
|
export async function buildFn() {
|
|
369
|
-
const jsonDataPath =
|
|
376
|
+
const jsonDataPath = paths.resolveRoot("jsonData");
|
|
370
377
|
if (!fse.pathExistsSync(jsonDataPath)) {
|
|
371
378
|
return Promise.reject(
|
|
372
379
|
jsonDataPath,
|
|
@@ -375,36 +382,33 @@ export async function buildFn() {
|
|
|
375
382
|
}
|
|
376
383
|
console.log("开始打包...");
|
|
377
384
|
let starTime = Date.now();
|
|
378
|
-
let outputPath =
|
|
385
|
+
let outputPath = paths.resolveRoot(config.fnOutput);
|
|
379
386
|
await fse.remove(outputPath);
|
|
380
387
|
await sleep(0);
|
|
381
388
|
await compilePagesPugToFn();
|
|
382
389
|
await fse.copy(
|
|
383
|
-
|
|
384
|
-
|
|
390
|
+
paths.resolveRoot("pagesPugFn/index.js"),
|
|
391
|
+
paths.resolveRoot(outputPath, "page/pages.js")
|
|
385
392
|
);
|
|
386
393
|
|
|
387
|
-
const routerPath =
|
|
394
|
+
const routerPath = paths.resolveRoot("router.js");
|
|
388
395
|
if (!fse.pathExistsSync(routerPath)) {
|
|
389
396
|
return Promise.reject("router.js文件不存在!");
|
|
390
397
|
}
|
|
391
398
|
|
|
392
|
-
await fse.copy(routerPath,
|
|
393
|
-
await fse.copy(
|
|
394
|
-
path.join(projectRoot, "public"),
|
|
395
|
-
path.join(outputPath, "page")
|
|
396
|
-
);
|
|
399
|
+
await fse.copy(routerPath, paths.resolveRoot(outputPath, "page/router.js"));
|
|
400
|
+
await fse.copy(paths.public, paths.resolveRoot(outputPath, "page"));
|
|
397
401
|
|
|
398
402
|
let totalCommonData = {};
|
|
399
403
|
totalCommonData.langCommon = config.commonData;
|
|
400
404
|
await async.each(config.languageList, async (lang) => {
|
|
401
405
|
let commonData = await fse.readJSON(
|
|
402
|
-
|
|
406
|
+
paths.resolveRoot("jsonData", lang, "_common.json")
|
|
403
407
|
);
|
|
404
408
|
totalCommonData[lang] = commonData;
|
|
405
409
|
});
|
|
406
410
|
|
|
407
|
-
await fse.copy(
|
|
411
|
+
await fse.copy(jsonDataPath, paths.resolveRoot(outputPath, "data"), {
|
|
408
412
|
filter: (src, dest) => {
|
|
409
413
|
// 排除_common.json 文件
|
|
410
414
|
return !src.endsWith("_common.json");
|
|
@@ -412,8 +416,8 @@ export async function buildFn() {
|
|
|
412
416
|
});
|
|
413
417
|
|
|
414
418
|
await fse.copy(
|
|
415
|
-
|
|
416
|
-
|
|
419
|
+
paths.template.static,
|
|
420
|
+
paths.resolveRoot(outputPath, "page/static"),
|
|
417
421
|
{
|
|
418
422
|
filter: (src, dest) => {
|
|
419
423
|
//根目录必须要返回true
|
|
@@ -423,7 +427,7 @@ export async function buildFn() {
|
|
|
423
427
|
if (config.buildStaticDirArr && config.buildStaticDirArr.length > 0) {
|
|
424
428
|
return !!config.buildStaticDirArr.find((item) => {
|
|
425
429
|
return src.startsWith(
|
|
426
|
-
|
|
430
|
+
paths.resolveRoot(paths.template.static, item)
|
|
427
431
|
);
|
|
428
432
|
});
|
|
429
433
|
}
|
|
@@ -433,14 +437,14 @@ export async function buildFn() {
|
|
|
433
437
|
);
|
|
434
438
|
|
|
435
439
|
await fse.writeJSON(
|
|
436
|
-
|
|
440
|
+
paths.resolveRoot(outputPath, "page", "common") + ".json",
|
|
437
441
|
totalCommonData
|
|
438
442
|
);
|
|
439
443
|
|
|
440
444
|
if (config.obfuscateJavaScript) {
|
|
441
445
|
console.log("开始混淆js文件...");
|
|
442
446
|
const startTime = Date.now();
|
|
443
|
-
await obfuscateJavaScript(
|
|
447
|
+
await obfuscateJavaScript(paths.resolveRoot(outputPath, "page", "static"));
|
|
444
448
|
const costTime = (Date.now() - startTime) / 1000;
|
|
445
449
|
console.log("混淆js文件耗时:", costTime, "s");
|
|
446
450
|
}
|
|
@@ -450,7 +454,7 @@ export async function buildFn() {
|
|
|
450
454
|
|
|
451
455
|
//html文件打包 不维护了
|
|
452
456
|
export async function buildStatic() {
|
|
453
|
-
let jsonDataPath =
|
|
457
|
+
let jsonDataPath = paths.resolveRoot("jsonData");
|
|
454
458
|
|
|
455
459
|
if (!fse.pathExistsSync(jsonDataPath)) {
|
|
456
460
|
return Promise.reject(
|
|
@@ -459,14 +463,14 @@ export async function buildStatic() {
|
|
|
459
463
|
}
|
|
460
464
|
console.log("开始打包...");
|
|
461
465
|
let starTime = Date.now();
|
|
462
|
-
let distOutputPath =
|
|
466
|
+
let distOutputPath = paths.resolveRoot(config.staticOutput);
|
|
463
467
|
await fse.remove(distOutputPath);
|
|
464
468
|
await sleep(0);
|
|
465
|
-
await fse.copy(
|
|
469
|
+
await fse.copy(paths.public, distOutputPath);
|
|
466
470
|
|
|
467
471
|
await fse.copy(
|
|
468
|
-
|
|
469
|
-
|
|
472
|
+
paths.template.static,
|
|
473
|
+
paths.resolveRoot(distOutputPath, "static"),
|
|
470
474
|
{
|
|
471
475
|
filter: (src, dest) => {
|
|
472
476
|
//根目录必须要返回true
|
|
@@ -476,7 +480,7 @@ export async function buildStatic() {
|
|
|
476
480
|
if (config.buildStaticDirArr && config.buildStaticDirArr.length > 0) {
|
|
477
481
|
return !!config.buildStaticDirArr.find((item) => {
|
|
478
482
|
return src.startsWith(
|
|
479
|
-
|
|
483
|
+
paths.resolveRoot(paths.template.static, item)
|
|
480
484
|
);
|
|
481
485
|
});
|
|
482
486
|
}
|
|
@@ -486,18 +490,12 @@ export async function buildStatic() {
|
|
|
486
490
|
);
|
|
487
491
|
|
|
488
492
|
await compilePagesPugToFn();
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
).href;
|
|
492
|
-
let PagesPugToFn = await import(pagesPugFnPath);
|
|
493
|
-
const getDataPath = pathToFileURL(
|
|
494
|
-
path.resolve(projectRoot, "getData.js")
|
|
495
|
-
).href;
|
|
496
|
-
const getData = await import(getDataPath);
|
|
493
|
+
let PagesPugToFn = await import(paths.pagesPugFn);
|
|
494
|
+
const getData = await import(paths.getData);
|
|
497
495
|
|
|
498
496
|
const fileMapTable = config.fileMapTable;
|
|
499
497
|
await async.each(config.languageList, async (lang) => {
|
|
500
|
-
let langDataPath =
|
|
498
|
+
let langDataPath = paths.resolveRoot(jsonDataPath, lang);
|
|
501
499
|
if (!fse.pathExistsSync(langDataPath)) {
|
|
502
500
|
console.log(
|
|
503
501
|
`注意配置了${lang}语言但${langDataPath}中没有生成${lang}语言的数据!`
|
|
@@ -505,7 +503,7 @@ export async function buildStatic() {
|
|
|
505
503
|
return;
|
|
506
504
|
}
|
|
507
505
|
let commonData = await fse.readJSON(
|
|
508
|
-
|
|
506
|
+
paths.resolveRoot("jsonData", lang, "_common.json")
|
|
509
507
|
);
|
|
510
508
|
commonData = _.merge(commonData, config.commonData);
|
|
511
509
|
|
|
@@ -531,8 +529,8 @@ export async function buildStatic() {
|
|
|
531
529
|
pugPathPreArr = obj.deviceList;
|
|
532
530
|
}
|
|
533
531
|
await async.each(pugPathPreArr, async (devicePrefix) => {
|
|
534
|
-
let pugPath =
|
|
535
|
-
|
|
532
|
+
let pugPath = paths.resolveRoot(
|
|
533
|
+
paths.template.pages,
|
|
536
534
|
langPrefix,
|
|
537
535
|
devicePrefix,
|
|
538
536
|
obj.pugPath.split("/").join(pathSymbol)
|
|
@@ -574,14 +572,14 @@ export async function buildStatic() {
|
|
|
574
572
|
`命名但是${index}下标中对象${property}属性为:${fileName}`
|
|
575
573
|
);
|
|
576
574
|
}
|
|
577
|
-
htmlPath =
|
|
575
|
+
htmlPath = paths.resolveRoot.join(
|
|
578
576
|
distOutputPath,
|
|
579
577
|
lang,
|
|
580
578
|
devicePrefix,
|
|
581
579
|
outPutPath.replace(name, fileName)
|
|
582
580
|
);
|
|
583
581
|
html = pug.compileFile(pugPath, {
|
|
584
|
-
basedir:
|
|
582
|
+
basedir: paths.template.root,
|
|
585
583
|
compileDebug: true,
|
|
586
584
|
filters: getCompilePugFilter()
|
|
587
585
|
})({
|
|
@@ -593,15 +591,14 @@ export async function buildStatic() {
|
|
|
593
591
|
await fse.writeFile(htmlPath, html);
|
|
594
592
|
}
|
|
595
593
|
} else {
|
|
596
|
-
htmlPath =
|
|
597
|
-
|
|
598
|
-
"template",
|
|
594
|
+
htmlPath = paths.resolveRoot(
|
|
595
|
+
paths.template.root,
|
|
599
596
|
lang,
|
|
600
597
|
devicePrefix,
|
|
601
598
|
outPutPath
|
|
602
599
|
);
|
|
603
600
|
html = pug.compileFile(pugPath, {
|
|
604
|
-
basedir:
|
|
601
|
+
basedir: paths.template.root,
|
|
605
602
|
compileDebug: true,
|
|
606
603
|
filters: getCompilePugFilter()
|
|
607
604
|
})({
|
|
@@ -613,14 +610,14 @@ export async function buildStatic() {
|
|
|
613
610
|
await fse.writeFile(htmlPath, html);
|
|
614
611
|
}
|
|
615
612
|
} else if (typeof data === "object") {
|
|
616
|
-
htmlPath =
|
|
613
|
+
htmlPath = paths.resolveRoot(
|
|
617
614
|
distOutputPath,
|
|
618
615
|
lang,
|
|
619
616
|
devicePrefix,
|
|
620
617
|
outPutPath
|
|
621
618
|
);
|
|
622
619
|
html = pug.compileFile(pugPath, {
|
|
623
|
-
basedir:
|
|
620
|
+
basedir: paths.template.root,
|
|
624
621
|
compileDebug: true,
|
|
625
622
|
filters: getCompilePugFilter()
|
|
626
623
|
})({
|
|
@@ -637,12 +634,14 @@ export async function buildStatic() {
|
|
|
637
634
|
}
|
|
638
635
|
|
|
639
636
|
let pagesAllJsonFileName = (
|
|
640
|
-
await fse.readdir(
|
|
637
|
+
await fse.readdir(langDataPath, {
|
|
641
638
|
recursive: true
|
|
642
639
|
})
|
|
643
640
|
).filter((fileName) => fileName.endsWith(".json"));
|
|
644
641
|
await async.eachLimit(pagesAllJsonFileName, 64, async (jsonFileName) => {
|
|
645
|
-
let data = await fse.readJSON(
|
|
642
|
+
let data = await fse.readJSON(
|
|
643
|
+
paths.resolveRoot(langDataPath, jsonFileName)
|
|
644
|
+
);
|
|
646
645
|
let pugTemplateArr = data._template;
|
|
647
646
|
if (!pugTemplateArr) {
|
|
648
647
|
return;
|
|
@@ -685,7 +684,7 @@ export async function buildStatic() {
|
|
|
685
684
|
pathSymbol +
|
|
686
685
|
data.page_name;
|
|
687
686
|
}
|
|
688
|
-
let htmlPath =
|
|
687
|
+
let htmlPath = paths.resolveRoot(
|
|
689
688
|
distOutputPath,
|
|
690
689
|
lang,
|
|
691
690
|
pugTemplate.replace(/\..*$/, ".html")
|
package/lib/paths.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { pathToFileURL, fileURLToPath } from "url";
|
|
3
|
+
|
|
4
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
+
const __dirname = path.dirname(__filename);
|
|
6
|
+
const projectRoot = process.cwd();
|
|
7
|
+
|
|
8
|
+
export const paths = {
|
|
9
|
+
// 项目根目录相关
|
|
10
|
+
projectRoot,
|
|
11
|
+
lib: __dirname,
|
|
12
|
+
config: pathToFileURL(path.resolve(projectRoot, "config.js")).href,
|
|
13
|
+
getData: pathToFileURL(path.resolve(projectRoot, "getData.js")).href,
|
|
14
|
+
pagesPugFn: pathToFileURL(path.resolve(projectRoot, "pagesPugFn/index.js"))
|
|
15
|
+
.href,
|
|
16
|
+
// 模板相关路径
|
|
17
|
+
template: {
|
|
18
|
+
root: path.join(projectRoot, "template"),
|
|
19
|
+
pages: path.join(projectRoot, "template/pages"),
|
|
20
|
+
static: path.join(projectRoot, "template/static")
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
// 公共资源
|
|
24
|
+
public: path.join(projectRoot, "public"),
|
|
25
|
+
|
|
26
|
+
// 运行时相关
|
|
27
|
+
pugRuntime: path.join(__dirname, "pugRuntime.js"),
|
|
28
|
+
|
|
29
|
+
// 工具函数
|
|
30
|
+
resolveRoot: (...args) => {
|
|
31
|
+
if (args[0].startsWith(projectRoot)) {
|
|
32
|
+
return path.join(...args);
|
|
33
|
+
}
|
|
34
|
+
return path.join(projectRoot, ...args);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export default paths;
|
package/lib/utils.js
CHANGED
|
@@ -2,12 +2,9 @@ import fse from "fs-extra";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import tcpPortUsed from "tcp-port-used";
|
|
4
4
|
import JavaScriptObfuscator from "javascript-obfuscator";
|
|
5
|
-
import {
|
|
5
|
+
import { paths } from "./paths.js";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
const __dirname = process.cwd();
|
|
9
|
-
const configPath = pathToFileURL(path.resolve(__dirname, "config.js")).href;
|
|
10
|
-
const { config } = await import(configPath);
|
|
7
|
+
const { config } = await import(paths.config);
|
|
11
8
|
|
|
12
9
|
// 根据操作系统设置路径分隔符
|
|
13
10
|
export const pathSymbol = process.platform.startsWith("win") ? "\\" : "/";
|
|
@@ -19,7 +16,7 @@ export const pathSymbol = process.platform.startsWith("win") ? "\\" : "/";
|
|
|
19
16
|
*/
|
|
20
17
|
export async function getPagesPugFilePathArr(isFilter) {
|
|
21
18
|
let pagesPugFilePathArr = (
|
|
22
|
-
await fse.readdir(
|
|
19
|
+
await fse.readdir(paths.template.pages, {
|
|
23
20
|
recursive: true
|
|
24
21
|
})
|
|
25
22
|
).filter((fileName) => fileName.endsWith(".pug"));
|
package/lib/watchFile.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import chokidar from "chokidar";
|
|
2
|
-
import path from "path";
|
|
3
2
|
import { exec } from "child_process";
|
|
4
3
|
import { debounce } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
const projectRoot = process.cwd();
|
|
4
|
+
import { paths } from "./paths.js";
|
|
7
5
|
|
|
8
6
|
/**
|
|
9
7
|
* 更改/template刷新网页
|
|
@@ -13,8 +11,7 @@ function watchTemplate() {
|
|
|
13
11
|
exec(`curl http://${process.env._localIp}:${process.env._port}/_refresh`);
|
|
14
12
|
}, 300);
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
let watch = chokidar.watch(path.join(projectRoot, "template"), {
|
|
14
|
+
let watch = chokidar.watch(paths.template.root, {
|
|
18
15
|
persistent: true
|
|
19
16
|
// ignored: [/node_modules/, /\.git/]
|
|
20
17
|
});
|
package/package.json
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pug-site-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"scripts": {
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "nodemon --ext js --ignore node_modules/ --ignore template/ index.js",
|
|
8
|
+
"getData": "node index.js",
|
|
9
|
+
"getFun": "node index.js",
|
|
10
|
+
"compileFn": "node index.js",
|
|
11
|
+
"buildFn": "node index.js",
|
|
12
|
+
"buildStatic": "node index.js",
|
|
13
|
+
"lang": "node index.js",
|
|
14
|
+
"build": "npm run getData && npm run buildFn"
|
|
15
|
+
},
|
|
7
16
|
"keywords": [],
|
|
8
17
|
"author": "xy",
|
|
9
18
|
"dependencies": {
|
|
@@ -24,10 +33,11 @@
|
|
|
24
33
|
"pug": "^3.0.3",
|
|
25
34
|
"tcp-port-used": "^1.0.2",
|
|
26
35
|
"uglify-js": "^3.19.3",
|
|
27
|
-
"ws": "^8.18.0"
|
|
36
|
+
"ws": "^8.18.0",
|
|
37
|
+
"axios": "^1.7.7"
|
|
28
38
|
},
|
|
29
39
|
"license": "ISC",
|
|
30
|
-
"description": "
|
|
40
|
+
"description": "增加指令以及调用逻辑",
|
|
31
41
|
"files": [
|
|
32
42
|
"lib/",
|
|
33
43
|
"index.js"
|