slidev-prerender 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yuma Ichimura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # tsdown-starter
2
+
3
+ A starter for creating a TypeScript package.
4
+
5
+ ## Development
6
+
7
+ - Install dependencies:
8
+
9
+ ```bash
10
+ npm install
11
+ ```
12
+
13
+ - Run the unit tests:
14
+
15
+ ```bash
16
+ npm run test
17
+ ```
18
+
19
+ - Build the library:
20
+
21
+ ```bash
22
+ npm run build
23
+ ```
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ 'use strict'
3
+ import('../dist/run.mjs')
@@ -0,0 +1,31 @@
1
+ import { SeoMeta } from "@slidev/types";
2
+ import { ResolvableLink } from "unhead/types";
3
+
4
+ //#region src/build/handle-head.d.ts
5
+ type BuildHeadOptions = {
6
+ lang?: string;
7
+ title: string;
8
+ description?: string | null;
9
+ canonicalUrl?: string | null;
10
+ ogImage?: string | null;
11
+ twitterCard?: string | null;
12
+ favicon?: string | null;
13
+ webFonts?: ResolvableLink[];
14
+ seoMeta?: Partial<SeoMeta>;
15
+ };
16
+ //#endregion
17
+ //#region src/config/loadConfig.d.ts
18
+ type UserConfig = {
19
+ slidevDist?: string;
20
+ outDir?: string;
21
+ pages?: {
22
+ fileName: string;
23
+ meta?: BuildHeadOptions;
24
+ }[];
25
+ port?: number;
26
+ };
27
+ //#endregion
28
+ //#region src/config/defineConfig.d.ts
29
+ declare function defineConfig(config: UserConfig): UserConfig;
30
+ //#endregion
31
+ export { type UserConfig, defineConfig };
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ //#region src/config/defineConfig.ts
2
+ function defineConfig(config) {
3
+ return config;
4
+ }
5
+
6
+ //#endregion
7
+ export { defineConfig };
package/dist/run.d.mts ADDED
@@ -0,0 +1 @@
1
+ export { };
package/dist/run.mjs ADDED
@@ -0,0 +1,195 @@
1
+ import fs from "node:fs/promises";
2
+ import path, { resolve } from "node:path";
3
+ import { chromium } from "@playwright/test";
4
+ import http from "node:http";
5
+ import sirv from "sirv";
6
+ import { escapeHtml } from "markdown-it/lib/common/utils.mjs";
7
+ import { createHead, transformHtmlTemplate } from "unhead/server";
8
+ import { parseHtmlForUnheadExtraction } from "unhead/parser";
9
+ import { pathToFileURL } from "node:url";
10
+ import { existsSync } from "node:fs";
11
+
12
+ //#region src/build/handle-dist.ts
13
+ async function removeDist(outDir) {
14
+ await fs.rm(outDir, {
15
+ recursive: true,
16
+ force: true
17
+ });
18
+ await fs.mkdir(outDir, { recursive: true });
19
+ }
20
+ async function serveDist(distPath, port) {
21
+ const serve = sirv(distPath, {
22
+ etag: true,
23
+ single: true
24
+ });
25
+ const server = http.createServer((req, res) => serve(req, res));
26
+ await new Promise((resolve$1, reject) => {
27
+ server.once("error", reject);
28
+ server.listen(port, resolve$1);
29
+ });
30
+ return {
31
+ origin: `http://localhost:${port}`,
32
+ close: () => new Promise((resolve$1) => server.close(() => resolve$1()))
33
+ };
34
+ }
35
+ async function getPageHtml(page, pageUrl) {
36
+ await page.goto(pageUrl, { waitUntil: "networkidle" });
37
+ return "<!doctype html>\n" + await page.content();
38
+ }
39
+
40
+ //#endregion
41
+ //#region src/utils/file.ts
42
+ async function copyDir(src, dst) {
43
+ await fs.mkdir(dst, { recursive: true });
44
+ for (const e of await fs.readdir(src, { withFileTypes: true })) {
45
+ const s = path.join(src, e.name);
46
+ const d = path.join(dst, e.name);
47
+ e.isDirectory() ? await copyDir(s, d) : await fs.copyFile(s, d);
48
+ }
49
+ }
50
+
51
+ //#endregion
52
+ //#region src/build/handle-head.ts
53
+ function toAttrValue(unsafe) {
54
+ return JSON.stringify(escapeHtml(String(unsafe)));
55
+ }
56
+ async function applyHead(html, opt) {
57
+ const extracted = parseHtmlForUnheadExtraction(html).input;
58
+ const description = opt.description ? toAttrValue(opt.description) : null;
59
+ return await transformHtmlTemplate(createHead({ init: [extracted, {
60
+ htmlAttrs: opt.lang ? { lang: opt.lang } : void 0,
61
+ title: opt.title,
62
+ link: [
63
+ opt.favicon ? {
64
+ rel: "icon",
65
+ href: opt.favicon
66
+ } : null,
67
+ ...opt.webFonts ?? [],
68
+ opt.canonicalUrl ? {
69
+ rel: "canonical",
70
+ href: opt.canonicalUrl
71
+ } : null
72
+ ].filter(Boolean),
73
+ meta: [
74
+ {
75
+ name: "description",
76
+ content: description
77
+ },
78
+ {
79
+ property: "og:title",
80
+ content: opt.seoMeta?.ogTitle || opt.title
81
+ },
82
+ {
83
+ property: "og:description",
84
+ content: opt.seoMeta?.ogDescription || description
85
+ },
86
+ {
87
+ property: "og:image",
88
+ content: opt.ogImage ?? opt.seoMeta?.ogImage
89
+ },
90
+ {
91
+ property: "og:url",
92
+ content: opt.seoMeta?.ogUrl || opt.canonicalUrl
93
+ },
94
+ {
95
+ name: "twitter:card",
96
+ content: opt.twitterCard ?? opt.seoMeta?.twitterCard
97
+ },
98
+ {
99
+ name: "twitter:title",
100
+ content: opt.seoMeta?.twitterTitle || opt.title
101
+ },
102
+ {
103
+ name: "twitter:description",
104
+ content: opt.seoMeta?.twitterDescription || description
105
+ },
106
+ {
107
+ name: "twitter:image",
108
+ content: (opt.seoMeta?.twitterImage || opt.ogImage) ?? opt.seoMeta?.ogImage
109
+ },
110
+ {
111
+ name: "twitter:url",
112
+ content: opt.seoMeta?.twitterUrl || opt.canonicalUrl
113
+ }
114
+ ].filter((x) => x.content)
115
+ }] }), html);
116
+ }
117
+
118
+ //#endregion
119
+ //#region src/config/loadConfig.ts
120
+ const CONFIG_FILE_NAMES = [
121
+ "slidev-prerender.config.ts",
122
+ "slidev-prerender.config.mts",
123
+ "slidev-prerender.config.js",
124
+ "slidev-prerender.config.mjs"
125
+ ];
126
+ async function loadConfig(cwd = process.cwd()) {
127
+ for (const fileName of CONFIG_FILE_NAMES) {
128
+ const configPath = resolve(cwd, fileName);
129
+ if (existsSync(configPath)) try {
130
+ const configModule = await import(pathToFileURL(configPath).href);
131
+ return configModule.default || configModule;
132
+ } catch (error) {
133
+ throw new Error(`Failed to load config from ${configPath}: ${error}`);
134
+ }
135
+ }
136
+ return {};
137
+ }
138
+
139
+ //#endregion
140
+ //#region src/build/getConfig.ts
141
+ const DEFAULT_CONFIG = {
142
+ slidevDist: "dist",
143
+ outDir: "dist-prerender",
144
+ pages: [],
145
+ port: 4173
146
+ };
147
+ async function getConfig() {
148
+ const config = await loadConfig();
149
+ return {
150
+ slidevDist: config.slidevDist ?? DEFAULT_CONFIG.slidevDist,
151
+ outDir: config.outDir ?? DEFAULT_CONFIG.outDir,
152
+ pages: config.pages ?? DEFAULT_CONFIG.pages,
153
+ port: config.port ?? DEFAULT_CONFIG.port
154
+ };
155
+ }
156
+
157
+ //#endregion
158
+ //#region src/build/index.ts
159
+ async function run() {
160
+ const { slidevDist, outDir, pages, port } = await getConfig();
161
+ try {
162
+ await fs.access(slidevDist);
163
+ } catch {
164
+ throw new Error(`Slidev dist directory not found: ${slidevDist}\nPlease run 'slidev build' first.`);
165
+ }
166
+ await removeDist(outDir);
167
+ const assetsPath = path.join(slidevDist, "assets");
168
+ try {
169
+ await fs.access(assetsPath);
170
+ await copyDir(assetsPath, path.join(outDir, "assets"));
171
+ } catch {
172
+ console.warn(`Assets directory not found: ${assetsPath}, skipping...`);
173
+ }
174
+ const { origin, close: serverClose } = await serveDist(slidevDist, port);
175
+ const browser = await chromium.launch();
176
+ const page = await browser.newPage();
177
+ try {
178
+ for (const n of pages) {
179
+ console.log(`[SSG] page ${n.fileName}`);
180
+ const html = await applyHead(await getPageHtml(page, `${origin}/${n.fileName}`), n.meta ?? { title: `ページ${n.fileName}` });
181
+ await fs.writeFile(path.join(outDir, `${n.fileName}.html`), html);
182
+ }
183
+ } finally {
184
+ await browser.close();
185
+ serverClose();
186
+ }
187
+ console.log("done");
188
+ }
189
+
190
+ //#endregion
191
+ //#region src/run.ts
192
+ await run();
193
+
194
+ //#endregion
195
+ export { };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "slidev-prerender",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "",
6
+ "author": "petaxa <soccer.i_y@icloud.com>",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/petaxa/slidev-prerender/packages/core#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/petaxa/slidev-prerender.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/petaxa/slidev-prerender/issues"
15
+ },
16
+ "exports": {
17
+ ".": "./dist/index.mjs",
18
+ "./package.json": "./package.json"
19
+ },
20
+ "main": "./dist/index.mjs",
21
+ "module": "./dist/index.mjs",
22
+ "types": "./dist/index.d.mts",
23
+ "bin": {
24
+ "slidev-prerender": "./bin/slidev-prerender.mjs"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "bin"
29
+ ],
30
+ "dependencies": {
31
+ "@playwright/test": "^1.57.0",
32
+ "markdown-it": "^14.1.0",
33
+ "unhead": "^2.1.1",
34
+ "sirv": "^3.0.2"
35
+ },
36
+ "scripts": {
37
+ "build": "tsdown",
38
+ "dev": "tsdown --watch",
39
+ "test": "vitest",
40
+ "typecheck": "tsc --noEmit"
41
+ }
42
+ }