@kaathewise/ssg 0.8.3 → 0.8.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/dist/render.d.ts +1 -0
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +17 -29
- package/dist/render_worker.d.ts +4 -0
- package/dist/render_worker.d.ts.map +1 -0
- package/dist/render_worker.js +50 -0
- package/dist/server.js +13 -7
- package/package.json +1 -1
package/dist/render.d.ts
CHANGED
|
@@ -6,4 +6,5 @@ export type Page = {
|
|
|
6
6
|
};
|
|
7
7
|
export declare function render(modulePath: string, pagesDir: string, params?: Params): Promise<Page>;
|
|
8
8
|
export declare function renderAll(modulePath: string, pagesDir: string): Promise<Page[]>;
|
|
9
|
+
export declare function substituteParams(inputPath: string, params: Params): string;
|
|
9
10
|
//# sourceMappingURL=render.d.ts.map
|
package/dist/render.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,MAAM,MAAM,IAAI,GAAG;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;IACX,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B,CAAA;AAED,wBAAsB,MAAM,CAC3B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,MAAW,GACjB,OAAO,CAAC,IAAI,CAAC,CAoBf;AAED,wBAAsB,SAAS,CAC9B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,EAAE,CAAC,CAgBjB;AAED,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAgB1E"}
|
package/dist/render.js
CHANGED
|
@@ -6,35 +6,23 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
6
6
|
}
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
|
-
import * as path from "node:path";
|
|
10
9
|
export async function render(modulePath, pagesDir, params = {}) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
default: {
|
|
28
|
-
throw "Not implemented";
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
let pagePath = path.relative(pagesDir, modulePath);
|
|
32
|
-
pagePath = substituteParams(pagePath, params);
|
|
33
|
-
pagePath = pagePath.replace(/\.tsx?$/, "");
|
|
34
|
-
if (pagePath.endsWith("index")) {
|
|
35
|
-
pagePath += ".html";
|
|
36
|
-
}
|
|
37
|
-
return { path: pagePath, src, contentType };
|
|
10
|
+
const worker = new Worker(new URL("./render_worker.ts", import.meta.url), {
|
|
11
|
+
type: "module",
|
|
12
|
+
});
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
worker.onmessage = (e) => {
|
|
15
|
+
console.log("e:", e);
|
|
16
|
+
worker.terminate();
|
|
17
|
+
if (e.data.page) {
|
|
18
|
+
resolve(e.data.page);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
reject(e.data.error);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
worker.postMessage({ modulePath, pagesDir, params });
|
|
25
|
+
});
|
|
38
26
|
}
|
|
39
27
|
export async function renderAll(modulePath, pagesDir) {
|
|
40
28
|
const module = await import(__rewriteRelativeImportExtension(modulePath));
|
|
@@ -50,7 +38,7 @@ export async function renderAll(modulePath, pagesDir) {
|
|
|
50
38
|
}
|
|
51
39
|
return out;
|
|
52
40
|
}
|
|
53
|
-
function substituteParams(inputPath, params) {
|
|
41
|
+
export function substituteParams(inputPath, params) {
|
|
54
42
|
let path = inputPath;
|
|
55
43
|
for (const [key, value] of Object.entries(params)) {
|
|
56
44
|
const single = `[${key}]`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render_worker.d.ts","sourceRoot":"","sources":["../src/render_worker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAoB,MAAM,UAAU,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,wBAAsB,MAAM,CAC3B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,MAAW,GACjB,OAAO,CAAC,IAAI,CAAC,CAmCf"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import { substituteParams } from "./render";
|
|
11
|
+
export async function render(modulePath, pagesDir, params = {}) {
|
|
12
|
+
const module = await import(__rewriteRelativeImportExtension(modulePath));
|
|
13
|
+
console.log(module);
|
|
14
|
+
let contentType = null;
|
|
15
|
+
if ("getContentType" in module) {
|
|
16
|
+
contentType = module.getContentType();
|
|
17
|
+
}
|
|
18
|
+
const def = module.default;
|
|
19
|
+
let src;
|
|
20
|
+
switch (typeof def) {
|
|
21
|
+
case "string": {
|
|
22
|
+
src = def;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
case "function": {
|
|
26
|
+
src = await def(params);
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
default: {
|
|
30
|
+
throw "Not implemented";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
let pagePath = path.relative(pagesDir, modulePath);
|
|
34
|
+
pagePath = substituteParams(pagePath, params);
|
|
35
|
+
pagePath = pagePath.replace(/\.tsx?$/, "");
|
|
36
|
+
if (pagePath.endsWith("index")) {
|
|
37
|
+
pagePath += ".html";
|
|
38
|
+
}
|
|
39
|
+
return { path: pagePath, src, contentType };
|
|
40
|
+
}
|
|
41
|
+
self.onmessage = async (e) => {
|
|
42
|
+
const { modulePath, pagesDir, params } = e.data;
|
|
43
|
+
try {
|
|
44
|
+
const page = await render(modulePath, pagesDir, params);
|
|
45
|
+
self.postMessage({ page });
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
self.postMessage({ error: err });
|
|
49
|
+
}
|
|
50
|
+
};
|
package/dist/server.js
CHANGED
|
@@ -83,12 +83,18 @@ const RELOAD_SCRIPT = `
|
|
|
83
83
|
</script>
|
|
84
84
|
`;
|
|
85
85
|
async function serveHtml(response, route, pagesDir) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
page.
|
|
86
|
+
try {
|
|
87
|
+
const page = await render(route.filePath, pagesDir, route.params);
|
|
88
|
+
if (page.contentType === "text/html" || !page.contentType) {
|
|
89
|
+
page.src += RELOAD_SCRIPT;
|
|
90
|
+
}
|
|
91
|
+
response.writeHead(200, {
|
|
92
|
+
"Content-Type": page.contentType ?? "text/html",
|
|
93
|
+
});
|
|
94
|
+
response.end(page.src);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
response.writeHead(500, { "Content-Type": "text/plain" });
|
|
98
|
+
response.end(`Error: ${err}`);
|
|
89
99
|
}
|
|
90
|
-
response.writeHead(200, {
|
|
91
|
-
"Content-Type": page.contentType ?? "text/html",
|
|
92
|
-
});
|
|
93
|
-
response.end(page.src);
|
|
94
100
|
}
|