satto 1.0.8 → 1.1.0
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/package.json +1 -1
- package/src/lib/index.js +48 -44
package/package.json
CHANGED
package/src/lib/index.js
CHANGED
|
@@ -18,38 +18,42 @@ function applyParams(template, params) {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
async function renderPage(html, params) {
|
|
21
|
-
|
|
21
|
+
try {
|
|
22
|
+
const match = html.match(/<ssr\s+url="([^"]+)"\s+.*response="([^"]+)"[^>]*>/);
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
if (match) {
|
|
25
|
+
const url = applyParams(match?.[1], params);
|
|
26
|
+
const resVar = match?.[2];
|
|
27
|
+
let data = {};
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
if (url) {
|
|
30
|
+
const res = await fetch(url);
|
|
31
|
+
data = await res.json();
|
|
32
|
+
}
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
34
|
+
html = html.replace(/<ssr[^>]*>([\s\S]*?)<\/ssr>/g, (_, content) => {
|
|
35
|
+
return content.replace(
|
|
36
|
+
/<ssr[^>]*>|<\/ssr>|\{\{(.*?)\}\}|<for\s+condition="let\s+(.+?)\s+in\s+(.+?)"\s*>|<\/for>|<if\s+condition="(.*?)"\s*>|<\/if>|\[(.+?)\]="(.+?)"/g,
|
|
37
|
+
(match, expr, item, arr, cond, attr, val) => {
|
|
38
|
+
if (match.startsWith("<ssr")) return "";
|
|
39
|
+
if (match === "</ssr>") return "";
|
|
40
|
+
if (expr) return `<%= ${expr.trim()} %>`;
|
|
41
|
+
if (item && arr) return `<% ${arr}.forEach(${item} => { %>`;
|
|
42
|
+
if (match === "</for>") return "<% }) %>";
|
|
43
|
+
if (cond) return `<% if (${cond}) { %>`;
|
|
44
|
+
if (match === "</if>") return "<% } %>";
|
|
45
|
+
if (attr && val) return `${attr}="<%= ${val} %>"`;
|
|
46
|
+
return match;
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
});
|
|
49
50
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
return ejs.render(html, { params, [resVar]: data });
|
|
52
|
+
} else {
|
|
53
|
+
return html;
|
|
54
|
+
}
|
|
55
|
+
} catch (err) {
|
|
56
|
+
throw err;
|
|
53
57
|
}
|
|
54
58
|
}
|
|
55
59
|
|
|
@@ -65,15 +69,15 @@ function createServer(__dirname, routes = [], port = 3000) {
|
|
|
65
69
|
app.use(express.static(path.join(__dirname, "static")));
|
|
66
70
|
|
|
67
71
|
routes.forEach((route) => {
|
|
68
|
-
app.get(route.path,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
app.use(express.static(path.join(__dirname, "app", page)));
|
|
72
|
+
app.get(route.path, (req, res, next) => {
|
|
73
|
+
const page = route.page;
|
|
74
|
+
const htmlPath = path.join(__dirname, "app", page, page + ".html");
|
|
75
|
+
app.use(express.static(path.join(__dirname, "app", page)));
|
|
73
76
|
|
|
74
|
-
|
|
77
|
+
fs.readFile(htmlPath, "utf8", async (err, html) => {
|
|
78
|
+
try {
|
|
75
79
|
if (err) {
|
|
76
|
-
|
|
80
|
+
res.status(404).send();
|
|
77
81
|
}
|
|
78
82
|
|
|
79
83
|
const params = { ...req.params };
|
|
@@ -82,27 +86,27 @@ function createServer(__dirname, routes = [], port = 3000) {
|
|
|
82
86
|
const CssPath = path.join(__dirname, "app", page, page + ".css");
|
|
83
87
|
const JsPath = path.join(__dirname, "app", page, page + ".js");
|
|
84
88
|
|
|
85
|
-
if(hasContent(JsPath)) {
|
|
89
|
+
if (hasContent(JsPath)) {
|
|
86
90
|
html += `\n<script src="./${page}.js?v=${versionApp}"></script>`;
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
html = index.replace("<routes></routes>", html);
|
|
90
94
|
html = html.replace(`<link rel="stylesheet" href="/styles.css">`, `<link rel="stylesheet" href="/styles.css?v=${versionApp}">`);
|
|
91
95
|
|
|
92
|
-
if(hasContent(CssPath)) {
|
|
96
|
+
if (hasContent(CssPath)) {
|
|
93
97
|
html = html.replace(
|
|
94
|
-
"</head>",
|
|
98
|
+
"</head>",
|
|
95
99
|
`<link rel="stylesheet" href="./${page}.css?v=${versionApp}">
|
|
96
|
-
|
|
100
|
+
</head>`
|
|
97
101
|
);
|
|
98
102
|
}
|
|
99
103
|
|
|
100
104
|
res.send(await renderPage(html, params));
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
105
|
+
} catch (err) {
|
|
106
|
+
console.error(err);
|
|
107
|
+
res.status(500).send();
|
|
108
|
+
}
|
|
109
|
+
});
|
|
106
110
|
});
|
|
107
111
|
});
|
|
108
112
|
|