forked from vdqbstp/playground-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
81 lines (70 loc) · 2.3 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const path = require("path");
const fs = require("fs-extra");
const SRC_PATH = "demos";
const OUT_PATH = "mpa";
const templateStr = fs.readFileSync("homepage/iframe.ejs", "utf8");
// 替换 ejs 模版格式的字符串,如 <%= title %>: templateStr.replaceEJS("title","replaced title");
String.prototype.replaceEJS = function (regStr, replaceStr) {
return this.replace(new RegExp(`<%=\\s*${regStr}\\s*%>`, "g"), replaceStr);
};
// clear mpa
fs.emptyDirSync(path.resolve(__dirname, OUT_PATH));
// filter index
const pages = fs
.readdirSync(path.resolve(__dirname, SRC_PATH))
.filter((pageName) => {
const pagePath = path.resolve(__dirname, SRC_PATH, pageName);
const stats = fs.statSync(pagePath);
if (stats.isDirectory()) {
const files = fs.readdirSync(pagePath);
const haveIndex = files.some((fileName) => fileName === "index.ts");
if (haveIndex) return true;
}
})
// generate mpa/*
.map((pageName) => {
const pagePath = path.resolve(__dirname, SRC_PATH, pageName);
const files = fs.readdirSync(pagePath);
let img = null;
let intro = "";
let codeUrl = "";
fs.outputFileSync(path.resolve(__dirname, OUT_PATH, pageName + ".js"), `import "../demos/${pageName}"`);
// avatar.jpg
files.some((fileName) => {
if (/^(avatar\.(jpg|png))$/.test(fileName)) {
img = RegExp.$1;
return true;
}
});
// README.md
files.some((fileName) => {
if (/^README\.md$/.test(fileName)) {
intro = fs.readFileSync(path.resolve(pagePath, "README.md"), "utf-8");
return true;
}
});
// code.json
files.some((fileName) => {
if (/^code\.json$/.test(fileName)) {
const code = fs.readFileSync(path.resolve(pagePath, "code.json"), "utf-8");
const codeJSON = JSON.parse(code);
codeUrl = codeJSON.riddle;
return true;
}
});
const ejs = templateStr.replaceEJS("title", pageName).replaceEJS("url", `./${pageName}.js`);
fs.outputFileSync(path.resolve(__dirname, OUT_PATH, pageName + ".html"), ejs);
return {
img,
intro,
name: pageName
};
});
// generate page.json
fs.outputFileSync(path.resolve(__dirname, "homepage/page.json"), JSON.stringify(pages));
module.exports = {
open: true,
optimizeDeps: {
exclude: ["oasis-engine"]
}
};