@kaathewise/ssg 0.9.2 → 0.11.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/dist/build.js +3 -3
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +5 -0
- package/dist/render.d.ts +8 -5
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +48 -25
- package/dist/router.d.ts.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5 -6
- package/package.json +6 -5
package/dist/build.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as fs from "node:fs/promises";
|
|
2
2
|
import * as path from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import { makeAllPages, render } from "./render.js";
|
|
4
4
|
import { walk } from "./utils.js";
|
|
5
5
|
export async function build(config) {
|
|
6
6
|
const pages = [];
|
|
7
7
|
for await (const modulePath of walk(config.routesDir)) {
|
|
8
|
-
const p = await
|
|
8
|
+
const p = await makeAllPages(modulePath, config);
|
|
9
9
|
pages.push(...p);
|
|
10
10
|
}
|
|
11
11
|
await fs.rm(config.outputDir, { recursive: true, force: true });
|
|
@@ -15,7 +15,7 @@ export async function build(config) {
|
|
|
15
15
|
await fs.mkdir(path.dirname(destPath), {
|
|
16
16
|
recursive: true,
|
|
17
17
|
});
|
|
18
|
-
await fs.writeFile(destPath, page
|
|
18
|
+
await fs.writeFile(destPath, render(page));
|
|
19
19
|
}
|
|
20
20
|
if (config.assetDir) {
|
|
21
21
|
await fs.cp(config.assetDir, config.outputDir, {
|
package/dist/config.d.ts
CHANGED
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,aAAa,GAAG;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;CAClB,CAAA;AAED,qBAAa,MAAM;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IAEZ,OAAO,EAAE,MAAM,EAAE,CAAA;gBAEL,OAAO,EAAE,aAAa;IAclC,SAAS,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAS5C,IAAI,UAAU,IAAI,MAAM,CAEvB;CACD;AAED,MAAM,MAAM,MAAM,GAAG;IACpB,OAAO,EAAE,MAAM,CAAA;CACf,CAAA;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CAE3D"}
|
package/dist/config.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
1
2
|
import * as path from "node:path";
|
|
2
3
|
const OUTPUT_DIR = "dist/";
|
|
3
4
|
const DEFAULT_PORT = 3001;
|
|
@@ -14,6 +15,7 @@ export class Config {
|
|
|
14
15
|
if (options.assetDir) {
|
|
15
16
|
this.assetDir = path.resolve(options.assetDir);
|
|
16
17
|
}
|
|
18
|
+
fs.mkdirSync(this.scratchDir, { recursive: true });
|
|
17
19
|
this.loaders = options.loaders ?? [];
|
|
18
20
|
}
|
|
19
21
|
getLoader(modulePath) {
|
|
@@ -24,6 +26,9 @@ export class Config {
|
|
|
24
26
|
}
|
|
25
27
|
return null;
|
|
26
28
|
}
|
|
29
|
+
get scratchDir() {
|
|
30
|
+
return path.join(this.outputDir, "scratch");
|
|
31
|
+
}
|
|
27
32
|
}
|
|
28
33
|
export function defineConfig(options) {
|
|
29
34
|
return new Config(options);
|
package/dist/render.d.ts
CHANGED
|
@@ -2,16 +2,19 @@ import type { Config } from "./config.ts";
|
|
|
2
2
|
import type { Params } from "./router.ts";
|
|
3
3
|
export type Page = {
|
|
4
4
|
path: string;
|
|
5
|
-
|
|
5
|
+
head: string;
|
|
6
|
+
body: string;
|
|
6
7
|
contentType: string | null;
|
|
7
8
|
};
|
|
8
9
|
export type ArbitraryModule = {
|
|
9
10
|
getStaticParams?: (() => Params[]) | (() => Promise<Params[]>);
|
|
10
11
|
getContentType?(params: Params): string;
|
|
11
|
-
|
|
12
|
+
Body: (params: Params) => Promise<string>;
|
|
13
|
+
Head: (params: Params, body: string) => Promise<string>;
|
|
12
14
|
};
|
|
13
|
-
export declare function load(modulePath: string): Promise<ArbitraryModule>;
|
|
14
|
-
export declare function
|
|
15
|
-
export declare function
|
|
15
|
+
export declare function load(modulePath: string, config: Config): Promise<ArbitraryModule>;
|
|
16
|
+
export declare function makePage(modulePath: string, params: Params, config: Config): Promise<Page>;
|
|
17
|
+
export declare function makeAllPages(modulePath: string, config: Config): Promise<Page[]>;
|
|
16
18
|
export declare function substituteParams(inputPath: string, params: Params): string;
|
|
19
|
+
export declare function render(page: Page): string;
|
|
17
20
|
//# 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":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,MAAM,MAAM,IAAI,GAAG;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,MAAM,MAAM,IAAI,GAAG;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B,CAAA;AAED,MAAM,MAAM,eAAe,GAAG;IAC7B,eAAe,CAAC,EAAE,CAAC,MAAM,MAAM,EAAE,CAAC,GAAG,CAAC,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;IAC9D,cAAc,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;IACvC,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IACzC,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;CACvD,CAAA;AAED,wBAAsB,IAAI,CACzB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,GACZ,OAAO,CAAC,eAAe,CAAC,CAoB1B;AAED,wBAAsB,QAAQ,CAC7B,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC,CAmBf;AAED,wBAAsB,YAAY,CACjC,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,EAAE,CAAC,CAgBjB;AAED,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAgB1E;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAWzC"}
|
package/dist/render.js
CHANGED
|
@@ -1,49 +1,60 @@
|
|
|
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
|
+
};
|
|
1
9
|
import * as path from "node:path";
|
|
2
|
-
import
|
|
3
|
-
export async function load(modulePath) {
|
|
4
|
-
const
|
|
10
|
+
import * as esbuild from "esbuild";
|
|
11
|
+
export async function load(modulePath, config) {
|
|
12
|
+
const result = await esbuild.build({
|
|
13
|
+
entryPoints: [modulePath],
|
|
14
|
+
bundle: true,
|
|
15
|
+
format: "esm",
|
|
16
|
+
outdir: config.scratchDir,
|
|
17
|
+
packages: "external",
|
|
18
|
+
platform: "node",
|
|
19
|
+
metafile: true,
|
|
20
|
+
});
|
|
21
|
+
if (!result.metafile)
|
|
22
|
+
throw Error("unreachable");
|
|
23
|
+
const outputFiles = Object.keys(result.metafile.outputs);
|
|
24
|
+
if (!outputFiles[0])
|
|
25
|
+
throw Error("unreachable");
|
|
26
|
+
const fileName = outputFiles[0].split("/").pop();
|
|
27
|
+
const outputPath = `${config.scratchDir}/${fileName}?t=${Date.now()}`;
|
|
28
|
+
const module = await import(__rewriteRelativeImportExtension(outputPath));
|
|
5
29
|
return module;
|
|
6
30
|
}
|
|
7
|
-
export async function
|
|
8
|
-
const module = await load(modulePath);
|
|
31
|
+
export async function makePage(modulePath, params, config) {
|
|
32
|
+
const module = await load(modulePath, config);
|
|
9
33
|
let contentType = null;
|
|
10
34
|
if (module.getContentType) {
|
|
11
35
|
contentType = module.getContentType(params);
|
|
12
36
|
}
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
switch (typeof def) {
|
|
16
|
-
case "string": {
|
|
17
|
-
src = def;
|
|
18
|
-
break;
|
|
19
|
-
}
|
|
20
|
-
case "function": {
|
|
21
|
-
src = await def(params);
|
|
22
|
-
break;
|
|
23
|
-
}
|
|
24
|
-
default: {
|
|
25
|
-
throw "`default` must be a string or a function";
|
|
26
|
-
}
|
|
27
|
-
}
|
|
37
|
+
const body = await module.Body(params);
|
|
38
|
+
const head = await module.Head(params, body);
|
|
28
39
|
let pagePath = path.relative(config.routesDir, modulePath);
|
|
29
40
|
pagePath = substituteParams(pagePath, params);
|
|
30
41
|
pagePath = pagePath.replace(/\.tsx?$/, "");
|
|
31
42
|
if (pagePath.endsWith("index")) {
|
|
32
43
|
pagePath += ".html";
|
|
33
44
|
}
|
|
34
|
-
return { path: pagePath,
|
|
45
|
+
return { path: pagePath, head, body, contentType };
|
|
35
46
|
}
|
|
36
|
-
export async function
|
|
37
|
-
const module = await load(modulePath);
|
|
47
|
+
export async function makeAllPages(modulePath, config) {
|
|
48
|
+
const module = await load(modulePath, config);
|
|
38
49
|
const out = [];
|
|
39
50
|
if (module.getStaticParams) {
|
|
40
51
|
const paramsList = await module.getStaticParams();
|
|
41
52
|
for (const params of paramsList) {
|
|
42
|
-
out.push(await
|
|
53
|
+
out.push(await makePage(modulePath, params, config));
|
|
43
54
|
}
|
|
44
55
|
}
|
|
45
56
|
else {
|
|
46
|
-
out.push(await
|
|
57
|
+
out.push(await makePage(modulePath, {}, config));
|
|
47
58
|
}
|
|
48
59
|
return out;
|
|
49
60
|
}
|
|
@@ -62,3 +73,15 @@ export function substituteParams(inputPath, params) {
|
|
|
62
73
|
path = path.replace(/\.tsx?$/, "");
|
|
63
74
|
return path;
|
|
64
75
|
}
|
|
76
|
+
export function render(page) {
|
|
77
|
+
return `<!doctype html>
|
|
78
|
+
<html>
|
|
79
|
+
<head>
|
|
80
|
+
${page.head}
|
|
81
|
+
</head>
|
|
82
|
+
<body>
|
|
83
|
+
${page.body}
|
|
84
|
+
</body>
|
|
85
|
+
</html>
|
|
86
|
+
`;
|
|
87
|
+
}
|
package/dist/router.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,KAAK,GAAG;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAA;AAEtD,qBAAa,MAAM;;gBAIN,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE;WAK1B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAe9C,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI;
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,KAAK,GAAG;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAA;AAEtD,qBAAa,MAAM;;gBAIN,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE;WAK1B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAe9C,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI;IAY/B,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAI7B;AAED,KAAK,IAAI,GAAG;IACX,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,QAAQ,EAAE,CAAA;CACrB,CAAA;AAgED,KAAK,QAAQ,GAAG;IACf,IAAI,EAAE,SAAS,GAAG,SAAS,GAAG,QAAQ,CAAA;IACtC,KAAK,EAAE,MAAM,CAAA;CACb,CAAA"}
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAMzC,wBAAsB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAMzC,wBAAsB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqCzD"}
|
package/dist/server.js
CHANGED
|
@@ -3,7 +3,7 @@ import { watch } from "node:fs/promises";
|
|
|
3
3
|
import http from "node:http";
|
|
4
4
|
import * as path from "node:path";
|
|
5
5
|
import url from "node:url";
|
|
6
|
-
import { render } from "./render.js";
|
|
6
|
+
import { makePage, render } from "./render.js";
|
|
7
7
|
import { Router } from "./router.js";
|
|
8
8
|
const EVENT_PATH = "/__ssg_dev_sse";
|
|
9
9
|
export async function serve(config) {
|
|
@@ -30,8 +30,7 @@ export async function serve(config) {
|
|
|
30
30
|
server.listen(config.port);
|
|
31
31
|
console.log(`Listening on :${config.port}`);
|
|
32
32
|
const watcher = watch(process.cwd(), { recursive: true });
|
|
33
|
-
for await (const
|
|
34
|
-
console.log(e);
|
|
33
|
+
for await (const _e of watcher) {
|
|
35
34
|
await router.reload();
|
|
36
35
|
for (const client of clients) {
|
|
37
36
|
client.write("data: RELOAD\n\n");
|
|
@@ -85,14 +84,14 @@ const RELOAD_SCRIPT = `
|
|
|
85
84
|
`;
|
|
86
85
|
async function serveHtml(response, route, config) {
|
|
87
86
|
try {
|
|
88
|
-
const page = await
|
|
87
|
+
const page = await makePage(route.filePath, route.params, config);
|
|
89
88
|
if (page.contentType === "text/html" || !page.contentType) {
|
|
90
|
-
page.
|
|
89
|
+
page.head += RELOAD_SCRIPT;
|
|
91
90
|
}
|
|
92
91
|
response.writeHead(200, {
|
|
93
92
|
"Content-Type": page.contentType ?? "text/html",
|
|
94
93
|
});
|
|
95
|
-
response.end(page
|
|
94
|
+
response.end(render(page));
|
|
96
95
|
}
|
|
97
96
|
catch (err) {
|
|
98
97
|
response.writeHead(500, { "Content-Type": "text/plain" });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaathewise/ssg",
|
|
3
3
|
"description": "Static site generator with TS and JSX support",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.11.0",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"author": "Andrej Kolčin",
|
|
7
7
|
"type": "module",
|
|
@@ -22,13 +22,14 @@
|
|
|
22
22
|
"files": [
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"tsx": "^4.21.0"
|
|
27
|
-
},
|
|
28
25
|
"devDependencies": {
|
|
29
26
|
"@biomejs/biome": "^2.2.3",
|
|
30
27
|
"@types/node": "^24.3.1",
|
|
31
28
|
"typescript": "^5.9.2",
|
|
32
|
-
"typescript-language-server": "^4.4.0"
|
|
29
|
+
"typescript-language-server": "^4.4.0",
|
|
30
|
+
"zx": "^8.8.5-lite"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"esbuild": "^0.28.0"
|
|
33
34
|
}
|
|
34
35
|
}
|