@smuzi/ssr 0.0.3 → 0.0.4
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 +8 -7
- package/src/globals.d.ts +0 -10
- package/src/index.ts +0 -212
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smuzi/ssr",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Simple template engine for generation HTML pages with advanced syntaxes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
|
-
"
|
|
23
|
-
"
|
|
22
|
+
"build/**/*.js",
|
|
23
|
+
"build/**/*.d.ts",
|
|
24
24
|
"globals.d.ts"
|
|
25
25
|
],
|
|
26
26
|
"exports": {
|
|
@@ -39,17 +39,18 @@
|
|
|
39
39
|
"#lib/*": "./src/*"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@smuzi/std": "0.2.
|
|
42
|
+
"@smuzi/std": "0.2.8"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/node": "^22.15.21",
|
|
46
46
|
"tsx": "^4.20.6",
|
|
47
47
|
"typescript": "^7.0.2",
|
|
48
|
-
"@smuzi/tests": "0.0.
|
|
49
|
-
"@smuzi/faker": "0.0.
|
|
48
|
+
"@smuzi/tests": "0.0.5",
|
|
49
|
+
"@smuzi/faker": "0.0.6"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"test": "tsx tests/index.ts",
|
|
53
53
|
"build": "tsc --project tsconfig.build.json"
|
|
54
|
-
}
|
|
54
|
+
},
|
|
55
|
+
"main": "./build/index.js"
|
|
55
56
|
}
|
package/src/globals.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
declare global {
|
|
3
|
-
function _print(value: string): void;
|
|
4
|
-
async function _component(component: string, slots: Record<string, () => string>): Promise;
|
|
5
|
-
async function _component(component: string): Promise;
|
|
6
|
-
|
|
7
|
-
const _std: typeof import("@smuzi/std");
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export {};
|
package/src/index.ts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import {
|
|
3
|
-
uuid,
|
|
4
|
-
HttpResponse,
|
|
5
|
-
path,
|
|
6
|
-
ResponseHttpHeaders,
|
|
7
|
-
Some,
|
|
8
|
-
None,
|
|
9
|
-
Option,
|
|
10
|
-
dump,
|
|
11
|
-
Result,
|
|
12
|
-
Err,
|
|
13
|
-
Ok,
|
|
14
|
-
transformError, StdError, panic, Pipe, regexp
|
|
15
|
-
} from "@smuzi/std";
|
|
16
|
-
import * as vm from "node:vm";
|
|
17
|
-
|
|
18
|
-
import * as _std from "@smuzi/std"
|
|
19
|
-
|
|
20
|
-
function getPath(pathDir: string, templateName: string, extension: string) {
|
|
21
|
-
return path.join(pathDir, templateName + "." + extension)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
type InputData = Record<string, unknown>
|
|
25
|
-
|
|
26
|
-
type SSREngineConfig = {
|
|
27
|
-
pathDir: string,
|
|
28
|
-
extension: string,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function runSSRCode(context, code: string): Promise<Result<string, StdError>> {
|
|
32
|
-
const printFunc = `
|
|
33
|
-
(async () => {
|
|
34
|
-
function _print(html) {
|
|
35
|
-
_output += html
|
|
36
|
-
}`;
|
|
37
|
-
code = `
|
|
38
|
-
_output = '';
|
|
39
|
-
${printFunc}
|
|
40
|
-
${code}
|
|
41
|
-
return _output
|
|
42
|
-
})()`
|
|
43
|
-
const script = new vm.Script(code);
|
|
44
|
-
|
|
45
|
-
const result = await script.runInContext(context);
|
|
46
|
-
|
|
47
|
-
return Ok(result !== undefined ? String(result) : '');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
type ForBlock = {
|
|
51
|
-
start: number,
|
|
52
|
-
end: number,
|
|
53
|
-
item: string,
|
|
54
|
-
iterable: string,
|
|
55
|
-
body: string,
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function findForBlock(templateCode: string): ForBlock | undefined {
|
|
59
|
-
const openingDirective = /@for\s*\(\s*([A-Za-z_$][\w$]*)\s+of\s+([^)]+?)\s*\)/g;
|
|
60
|
-
const openingMatch = openingDirective.exec(templateCode);
|
|
61
|
-
|
|
62
|
-
if (openingMatch === null || openingMatch.index === undefined) {
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const bodyStart = openingMatch.index + openingMatch[0].length;
|
|
67
|
-
const directive = /@for\s*\(\s*[A-Za-z_$][\w$]*\s+of\s+[^)]*?\s*\)|@end\b/g;
|
|
68
|
-
directive.lastIndex = bodyStart;
|
|
69
|
-
|
|
70
|
-
let depth = 1;
|
|
71
|
-
for (const match of templateCode.matchAll(directive)) {
|
|
72
|
-
if (match[0].startsWith("@for")) {
|
|
73
|
-
depth += 1;
|
|
74
|
-
continue;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
depth -= 1;
|
|
78
|
-
if (depth === 0 && match.index !== undefined) {
|
|
79
|
-
return {
|
|
80
|
-
start: openingMatch.index,
|
|
81
|
-
end: match.index + match[0].length,
|
|
82
|
-
item: openingMatch[1],
|
|
83
|
-
iterable: openingMatch[2],
|
|
84
|
-
body: templateCode.slice(bodyStart, match.index),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return undefined;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function parseCode(context: any, templateCode: string) {
|
|
93
|
-
try {
|
|
94
|
-
let renderedTemplate = '';
|
|
95
|
-
let cursor = 0;
|
|
96
|
-
let forBlock: ForBlock | undefined;
|
|
97
|
-
|
|
98
|
-
while ((forBlock = findForBlock(templateCode.slice(cursor))) !== undefined) {
|
|
99
|
-
const block = {
|
|
100
|
-
...forBlock,
|
|
101
|
-
start: forBlock.start + cursor,
|
|
102
|
-
end: forBlock.end + cursor,
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
renderedTemplate += templateCode.slice(cursor, block.start);
|
|
106
|
-
|
|
107
|
-
const code = `
|
|
108
|
-
for (const ${block.item} of (${block.iterable})) {
|
|
109
|
-
const res = await _ssrEngine.parseCode(
|
|
110
|
-
{ [${JSON.stringify(block.item)}]: ${block.item} },
|
|
111
|
-
${JSON.stringify(block.body)}
|
|
112
|
-
);
|
|
113
|
-
_output += res.unwrap();
|
|
114
|
-
}`;
|
|
115
|
-
const renderedBlock = await runSSRCode(context, code);
|
|
116
|
-
|
|
117
|
-
if (renderedBlock.isErr()) {
|
|
118
|
-
return renderedBlock;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
renderedBlock.runThenOk(html => renderedTemplate += html);
|
|
122
|
-
cursor = block.end;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
renderedTemplate += templateCode.slice(cursor);
|
|
126
|
-
|
|
127
|
-
return await regexp.asyncReplace(renderedTemplate, /{{([\s\S]*?)}}/g, async (match, code) => {
|
|
128
|
-
return await runSSRCode(context, `_print(${code})`)
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
// const res1 = Ok(
|
|
133
|
-
// templateCode
|
|
134
|
-
// //@if(condition)...@else...@end or @if(condition)...@end
|
|
135
|
-
// .replace(/@if\s*\(\s*([^)]+)\s*\)([\s\S]*?)(?:@else([\s\S]*?))?@end/g, (match, condition, ifBody, elseBody) => {
|
|
136
|
-
// const code = `
|
|
137
|
-
// _print((${condition}) ? \`${ifBody.replace(/`/g, '\\`').replace(/\$/g, '\\$')}\` : \`${(elseBody || '').replace(/`/g, '\\`').replace(/\$/g, '\\$')}\`);
|
|
138
|
-
// `;
|
|
139
|
-
// return runSSRCode(context, code);
|
|
140
|
-
// })
|
|
141
|
-
// //<script ssr>...</script>
|
|
142
|
-
// .replace(/<script\b[^>]*\bssr>([\s\S]*?)<\/script>/g, (match, code) => runSSRCode(context, code))
|
|
143
|
-
//
|
|
144
|
-
// //{{ ... }}
|
|
145
|
-
// .replace(/{{([\s\S]*?)}}/g, (match, code) => {
|
|
146
|
-
// return runSSRCode(context, `_print(${code})`)
|
|
147
|
-
// })
|
|
148
|
-
// );
|
|
149
|
-
} catch (err) {
|
|
150
|
-
return Err(transformError(err));
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function createContext(inputData, pathDir, extension )
|
|
155
|
-
{
|
|
156
|
-
return vm.createContext({
|
|
157
|
-
...inputData,
|
|
158
|
-
_std,
|
|
159
|
-
_output: '',
|
|
160
|
-
_ssrEngine: {
|
|
161
|
-
renderComponent: async (templateNameChild: string, inputDataChild) => {
|
|
162
|
-
return (renderTemplate(pathDir, extension))(templateNameChild, inputDataChild)
|
|
163
|
-
},
|
|
164
|
-
parseCode: async (inputDataChild, templateCode) => {
|
|
165
|
-
return parseCode(
|
|
166
|
-
createContext(
|
|
167
|
-
Object.assign(inputDataChild, inputData),
|
|
168
|
-
pathDir,
|
|
169
|
-
extension
|
|
170
|
-
), templateCode)
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
})
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function renderTemplate(
|
|
177
|
-
pathDir: string,
|
|
178
|
-
extension: string,
|
|
179
|
-
): ( templateName: string,
|
|
180
|
-
inputData: InputData,
|
|
181
|
-
slots?: Option
|
|
182
|
-
) => Promise<Result<string, StdError>> {
|
|
183
|
-
return async (
|
|
184
|
-
templateName: string,
|
|
185
|
-
inputData: InputData = {},
|
|
186
|
-
slots: Option = None()
|
|
187
|
-
) => {
|
|
188
|
-
|
|
189
|
-
let templateCode = fs.readFileSync(getPath(pathDir, templateName, extension), 'utf-8');
|
|
190
|
-
|
|
191
|
-
const context = createContext(inputData, pathDir, extension);
|
|
192
|
-
|
|
193
|
-
return parseCode(context, templateCode);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function ssrEngine({pathDir = "./src/templates", extension = "html"}: Partial<SSREngineConfig> = {}) {
|
|
198
|
-
|
|
199
|
-
const render = renderTemplate(pathDir, extension)
|
|
200
|
-
|
|
201
|
-
const response = async (templateName: string, inputData: InputData = {}) => {
|
|
202
|
-
return (await render(templateName, inputData)).match({
|
|
203
|
-
Err: err => err,
|
|
204
|
-
Ok: html => html,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return {
|
|
209
|
-
render,
|
|
210
|
-
response,
|
|
211
|
-
}
|
|
212
|
-
}
|