@kiln-js/vite-plugin 0.1.5 → 0.2.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/README.md +42 -0
- package/dist/cache.d.ts +20 -0
- package/dist/index.d.ts +8 -5
- package/dist/index.js +341 -41
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @kiln-js/vite-plugin
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@kiln-js/vite-plugin)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
Part of the Kiln framework.
|
|
7
|
+
|
|
8
|
+
Part of [Kiln](https://github.com/kiln-js/kiln), a file-based server-side rendering framework for React and Vite.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
bun add -d @kiln-js/vite-plugin
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { defineConfig } from "vite";
|
|
20
|
+
import { kiln } from "@kiln-js/vite-plugin";
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
plugins: [...kiln({ pagesDir: "app/pages", apiDir: "app/api" })],
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## What it provides
|
|
28
|
+
|
|
29
|
+
The route table (`virtual:kiln-routes`), the `<kiln:title>` rewrite, the entry prelude, and the server-only import guard.
|
|
30
|
+
The Kiln CLI calls `kiln()` for you, so add it yourself only when you run Vite without the CLI.
|
|
31
|
+
|
|
32
|
+
## Documentation
|
|
33
|
+
|
|
34
|
+
See the [Kiln repository](https://github.com/kiln-js/kiln) for the full API reference and guides.
|
|
35
|
+
|
|
36
|
+
## Development
|
|
37
|
+
|
|
38
|
+
This repository is generated from the Kiln monorepo. Make changes there, then run `scripts/publish-repo.ps1` to publish them here. Report issues in this repository, and send pull requests to the monorepo.
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ApiRouteRecord, type RouteRecord, type SpecialRecord } from "@kiln-js/core/scan";
|
|
2
|
+
export interface CacheStats {
|
|
3
|
+
hits: number;
|
|
4
|
+
misses: number;
|
|
5
|
+
invalidations: number;
|
|
6
|
+
}
|
|
7
|
+
export interface ScanCache {
|
|
8
|
+
routes(): RouteRecord[];
|
|
9
|
+
api(): ApiRouteRecord[];
|
|
10
|
+
specials(kind: "_error" | "_not-found"): SpecialRecord[];
|
|
11
|
+
isIsland(file: string): boolean;
|
|
12
|
+
invalidate(file: string): void;
|
|
13
|
+
stats(): CacheStats;
|
|
14
|
+
}
|
|
15
|
+
export declare function buildCacheStats(): CacheStats;
|
|
16
|
+
export declare function createScanCache(opts: {
|
|
17
|
+
pagesDir: string;
|
|
18
|
+
apiDir: string;
|
|
19
|
+
appDir: string;
|
|
20
|
+
}): ScanCache;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import type { PluginOption } from "vite";
|
|
2
|
-
export
|
|
2
|
+
export { buildCacheStats } from "./cache";
|
|
3
|
+
export type { CacheStats } from "./cache";
|
|
4
|
+
export interface KilnPluginSpec {
|
|
3
5
|
name: string;
|
|
4
6
|
vite?: () => PluginOption[];
|
|
5
7
|
}
|
|
6
|
-
export interface KilnConfig {
|
|
7
|
-
plugins?: KilnPlugin[];
|
|
8
|
-
}
|
|
9
8
|
export interface KilnOptions {
|
|
10
9
|
pagesDir?: string;
|
|
11
10
|
apiDir?: string;
|
|
11
|
+
appDir?: string;
|
|
12
|
+
plugins?: KilnPluginSpec[];
|
|
12
13
|
}
|
|
13
|
-
export declare function
|
|
14
|
+
export declare function rewriteTitleTags(code: string): string;
|
|
15
|
+
export declare function stripServerCode(code: string, parse: (c: string) => any): string | null;
|
|
16
|
+
export declare function islandCode(code: string, parse: (c: string) => any, islandId: string): string | null;
|
|
14
17
|
export declare function kiln(opts?: KilnOptions): PluginOption[];
|
package/dist/index.js
CHANGED
|
@@ -1,88 +1,388 @@
|
|
|
1
|
-
// src/index.ts
|
|
1
|
+
// packages/vite-plugin/src/index.ts
|
|
2
|
+
import fs2 from "node:fs";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import react from "@vitejs/plugin-react";
|
|
4
|
-
import
|
|
5
|
+
import "@kiln-js/core/scan";
|
|
6
|
+
|
|
7
|
+
// packages/vite-plugin/src/cache.ts
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import {
|
|
10
|
+
scanApi,
|
|
11
|
+
scanRoutes,
|
|
12
|
+
scanSpecials
|
|
13
|
+
} from "@kiln-js/core/scan";
|
|
14
|
+
var ISLAND = /^\s*(?:\/\/[^\n]*\n\s*)*(["'])use island\1/;
|
|
15
|
+
var norm = (p) => p.replace(/\\/g, "/");
|
|
16
|
+
var registry = /* @__PURE__ */ new Set();
|
|
17
|
+
function buildCacheStats() {
|
|
18
|
+
const total = { hits: 0, misses: 0, invalidations: 0 };
|
|
19
|
+
for (const cache of registry) {
|
|
20
|
+
const s = cache.stats();
|
|
21
|
+
total.hits += s.hits;
|
|
22
|
+
total.misses += s.misses;
|
|
23
|
+
total.invalidations += s.invalidations;
|
|
24
|
+
}
|
|
25
|
+
return total;
|
|
26
|
+
}
|
|
27
|
+
function createScanCache(opts) {
|
|
28
|
+
const pagesPrefix = norm(opts.pagesDir) + "/";
|
|
29
|
+
const apiPrefix = norm(opts.apiDir) + "/";
|
|
30
|
+
const memo = /* @__PURE__ */ new Map();
|
|
31
|
+
const islandMemo = /* @__PURE__ */ new Map();
|
|
32
|
+
let hits = 0;
|
|
33
|
+
let misses = 0;
|
|
34
|
+
let invalidations = 0;
|
|
35
|
+
function memoized(key, compute) {
|
|
36
|
+
if (memo.has(key)) {
|
|
37
|
+
hits++;
|
|
38
|
+
return memo.get(key);
|
|
39
|
+
}
|
|
40
|
+
misses++;
|
|
41
|
+
const value = compute();
|
|
42
|
+
memo.set(key, value);
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
const cache = {
|
|
46
|
+
routes: () => memoized("routes", () => scanRoutes(opts.pagesDir)),
|
|
47
|
+
api: () => memoized("api", () => scanApi(opts.apiDir)),
|
|
48
|
+
specials: (kind) => memoized(`special:${kind}`, () => scanSpecials(opts.pagesDir, kind)),
|
|
49
|
+
isIsland(file) {
|
|
50
|
+
let key;
|
|
51
|
+
try {
|
|
52
|
+
const st = fs.statSync(file);
|
|
53
|
+
key = `${st.mtimeMs}:${st.size}`;
|
|
54
|
+
} catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
const cached = islandMemo.get(file);
|
|
58
|
+
if (cached && cached.key === key) {
|
|
59
|
+
hits++;
|
|
60
|
+
return cached.value;
|
|
61
|
+
}
|
|
62
|
+
misses++;
|
|
63
|
+
const value = ISLAND.test(fs.readFileSync(file, "utf8"));
|
|
64
|
+
islandMemo.set(file, { key, value });
|
|
65
|
+
return value;
|
|
66
|
+
},
|
|
67
|
+
invalidate(file) {
|
|
68
|
+
const f = norm(file);
|
|
69
|
+
let touched = false;
|
|
70
|
+
if (f.startsWith(pagesPrefix)) {
|
|
71
|
+
memo.delete("routes");
|
|
72
|
+
for (const key of [...memo.keys()]) if (key.startsWith("special:")) memo.delete(key);
|
|
73
|
+
touched = true;
|
|
74
|
+
}
|
|
75
|
+
if (f.startsWith(apiPrefix)) {
|
|
76
|
+
memo.delete("api");
|
|
77
|
+
touched = true;
|
|
78
|
+
}
|
|
79
|
+
islandMemo.delete(file);
|
|
80
|
+
if (touched) invalidations++;
|
|
81
|
+
},
|
|
82
|
+
stats: () => ({ hits, misses, invalidations })
|
|
83
|
+
};
|
|
84
|
+
registry.add(cache);
|
|
85
|
+
return cache;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// packages/vite-plugin/src/index.ts
|
|
5
89
|
var VIRTUAL = "virtual:kiln-routes";
|
|
6
|
-
var
|
|
90
|
+
var VIRTUAL_ISLANDS = "virtual:kiln-islands";
|
|
91
|
+
var RESOLVED = "\0" + VIRTUAL;
|
|
92
|
+
var RESOLVED_ISLANDS = "\0" + VIRTUAL_ISLANDS;
|
|
7
93
|
var SERVER_FILE = /\.server(\.[jt]sx?)?$/;
|
|
8
94
|
var ENTRY_FILE = /[\\/](app|\.kiln)[\\/](client|server)\.(tsx|ts|jsx|js)$/;
|
|
9
|
-
var
|
|
95
|
+
var SERVER_EXPORTS = /* @__PURE__ */ new Set(["loader", "action", "revalidate"]);
|
|
96
|
+
var PRELUDE = `import __kilnRoutes, { api as __kilnApi, specials as __kilnSpecials } from "${VIRTUAL}";
|
|
97
|
+
import __kilnIslands from "${VIRTUAL_ISLANDS}";
|
|
10
98
|
globalThis.__KILN_ROUTES__ = __kilnRoutes;
|
|
11
99
|
globalThis.__KILN_API__ = __kilnApi;
|
|
100
|
+
globalThis.__KILN_SPECIALS__ = __kilnSpecials;
|
|
101
|
+
globalThis.__KILN_ISLANDS__ = __kilnIslands;
|
|
12
102
|
`;
|
|
13
103
|
var TITLE_IMPORT = `import { Title as KilnTitle } from "@kiln-js/core";
|
|
14
104
|
`;
|
|
15
|
-
var
|
|
16
|
-
function
|
|
17
|
-
|
|
105
|
+
var norm2 = (p) => p.replace(/\\/g, "/");
|
|
106
|
+
function rewriteTitleTags(code) {
|
|
107
|
+
const n = code.length;
|
|
108
|
+
let out = "";
|
|
109
|
+
let i = 0;
|
|
110
|
+
const isStart = (k) => {
|
|
111
|
+
const c = code[k];
|
|
112
|
+
if (c === '"' || c === "'" || c === "`") return true;
|
|
113
|
+
return c === "/" && (code[k + 1] === "/" || code[k + 1] === "*");
|
|
114
|
+
};
|
|
115
|
+
while (i < n) {
|
|
116
|
+
let j = i;
|
|
117
|
+
while (j < n && !isStart(j)) j++;
|
|
118
|
+
out += code.slice(i, j).replace(/<(\/?)kiln:title\b/g, "<$1KilnTitle");
|
|
119
|
+
i = j;
|
|
120
|
+
if (i >= n) break;
|
|
121
|
+
const c = code[i];
|
|
122
|
+
let end;
|
|
123
|
+
if (c === "/" && code[i + 1] === "/") {
|
|
124
|
+
end = code.indexOf("\n", i);
|
|
125
|
+
if (end === -1) end = n;
|
|
126
|
+
} else if (c === "/" && code[i + 1] === "*") {
|
|
127
|
+
end = code.indexOf("*/", i + 2);
|
|
128
|
+
end = end === -1 ? n : end + 2;
|
|
129
|
+
} else if (c === "`") {
|
|
130
|
+
end = i + 1;
|
|
131
|
+
while (end < n && code[end] !== "`") end += code[end] === "\\" ? 2 : 1;
|
|
132
|
+
end = Math.min(end + 1, n);
|
|
133
|
+
} else {
|
|
134
|
+
end = i + 1;
|
|
135
|
+
while (end < n && code[end] !== c && code[end] !== "\n") end += code[end] === "\\" ? 2 : 1;
|
|
136
|
+
if (end < n && code[end] === c) end += 1;
|
|
137
|
+
}
|
|
138
|
+
out += code.slice(i, end);
|
|
139
|
+
i = end;
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
function declaredNames(decl) {
|
|
144
|
+
if (decl.type === "FunctionDeclaration") return decl.id ? [decl.id.name] : [];
|
|
145
|
+
if (decl.type === "VariableDeclaration") {
|
|
146
|
+
return decl.declarations.flatMap((d) => d.id.type === "Identifier" ? [d.id.name] : []);
|
|
147
|
+
}
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
function collectIdentifiers(nodes) {
|
|
151
|
+
const names = /* @__PURE__ */ new Set();
|
|
152
|
+
const visit = (node) => {
|
|
153
|
+
if (!node || typeof node !== "object") return;
|
|
154
|
+
if (Array.isArray(node)) {
|
|
155
|
+
node.forEach(visit);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (node.type === "Identifier") names.add(node.name);
|
|
159
|
+
for (const key of Object.keys(node)) {
|
|
160
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc") continue;
|
|
161
|
+
visit(node[key]);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
nodes.forEach(visit);
|
|
165
|
+
return names;
|
|
166
|
+
}
|
|
167
|
+
function applyEdits(code, edits) {
|
|
168
|
+
let out = code;
|
|
169
|
+
for (const e of [...edits].sort((a, b) => b.start - a.start)) {
|
|
170
|
+
out = out.slice(0, e.start) + e.text + out.slice(e.end);
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
18
173
|
}
|
|
19
|
-
function
|
|
20
|
-
const
|
|
21
|
-
|
|
174
|
+
function rebuildImport(node, used) {
|
|
175
|
+
const specifiers = node.specifiers;
|
|
176
|
+
if (specifiers.length === 0) return null;
|
|
177
|
+
const keep = specifiers.filter((s) => used.has(s.local.name));
|
|
178
|
+
if (keep.length === specifiers.length) return null;
|
|
179
|
+
if (keep.length === 0) return "";
|
|
180
|
+
const parts = [];
|
|
181
|
+
const def = keep.find((s) => s.type === "ImportDefaultSpecifier");
|
|
182
|
+
const ns = keep.find((s) => s.type === "ImportNamespaceSpecifier");
|
|
183
|
+
const named = keep.filter((s) => s.type === "ImportSpecifier");
|
|
184
|
+
if (def) parts.push(def.local.name);
|
|
185
|
+
if (ns) parts.push(`* as ${ns.local.name}`);
|
|
186
|
+
if (named.length) {
|
|
187
|
+
const list = named.map((s) => {
|
|
188
|
+
const imported = s.imported.name ?? s.imported.value;
|
|
189
|
+
return imported === s.local.name ? imported : `${imported} as ${s.local.name}`;
|
|
190
|
+
});
|
|
191
|
+
parts.push(`{ ${list.join(", ")} }`);
|
|
192
|
+
}
|
|
193
|
+
return `import ${parts.join(", ")} from ${JSON.stringify(node.source.value)};`;
|
|
194
|
+
}
|
|
195
|
+
function stripServerCode(code, parse) {
|
|
196
|
+
const program = parse(code);
|
|
197
|
+
const edits = [];
|
|
198
|
+
for (const node of program.body) {
|
|
199
|
+
if (node.type !== "ExportNamedDeclaration" || !node.declaration) continue;
|
|
200
|
+
if (declaredNames(node.declaration).some((n) => SERVER_EXPORTS.has(n))) {
|
|
201
|
+
edits.push({ start: node.start, end: node.end, text: "" });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (edits.length === 0) return null;
|
|
205
|
+
const stripped = applyEdits(code, edits);
|
|
206
|
+
const next = parse(stripped);
|
|
207
|
+
const used = collectIdentifiers(next.body.filter((n) => n.type !== "ImportDeclaration"));
|
|
208
|
+
const importEdits = [];
|
|
209
|
+
for (const node of next.body) {
|
|
210
|
+
if (node.type !== "ImportDeclaration") continue;
|
|
211
|
+
const text = rebuildImport(node, used);
|
|
212
|
+
if (text !== null) importEdits.push({ start: node.start, end: node.end, text });
|
|
213
|
+
}
|
|
214
|
+
return applyEdits(stripped, importEdits);
|
|
215
|
+
}
|
|
216
|
+
function islandCode(code, parse, islandId2) {
|
|
217
|
+
const program = parse(code);
|
|
218
|
+
const def = program.body.find((n) => n.type === "ExportDefaultDeclaration");
|
|
219
|
+
if (!def) return null;
|
|
220
|
+
const decl = def.declaration;
|
|
221
|
+
let raw;
|
|
222
|
+
const edits = [];
|
|
223
|
+
if (decl.type === "FunctionDeclaration" && decl.id) {
|
|
224
|
+
raw = decl.id.name;
|
|
225
|
+
edits.push({ start: def.start, end: decl.start, text: "" });
|
|
226
|
+
} else if (decl.type === "Identifier") {
|
|
227
|
+
raw = decl.name;
|
|
228
|
+
edits.push({ start: def.start, end: def.end, text: "" });
|
|
229
|
+
} else {
|
|
230
|
+
raw = "__KilnIslandRaw";
|
|
231
|
+
edits.push({ start: def.start, end: decl.start, text: `const ${raw} = ` });
|
|
232
|
+
}
|
|
233
|
+
return applyEdits(code, edits) + `
|
|
234
|
+
import { island as __kilnIsland } from "@kiln-js/core";
|
|
235
|
+
export const __kilnRaw = ${raw};
|
|
236
|
+
export default __kilnIsland(${JSON.stringify(islandId2)}, ${raw});
|
|
237
|
+
`;
|
|
238
|
+
}
|
|
239
|
+
function sourceFiles(dir, out = []) {
|
|
240
|
+
if (!fs2.existsSync(dir)) return out;
|
|
241
|
+
for (const entry of fs2.readdirSync(dir, { withFileTypes: true })) {
|
|
242
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules" || entry.name === "dist") continue;
|
|
243
|
+
const full = path.join(dir, entry.name);
|
|
244
|
+
if (entry.isDirectory()) sourceFiles(full, out);
|
|
245
|
+
else if (/\.[jt]sx?$/.test(entry.name) && !SERVER_FILE.test(entry.name)) out.push(full);
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
var islandId = (file) => norm2(path.relative(process.cwd(), file));
|
|
250
|
+
function islandFiles(appDir, cache) {
|
|
251
|
+
return sourceFiles(appDir).filter((f) => cache.isIsland(f));
|
|
252
|
+
}
|
|
253
|
+
function postPlugin(pagesDir, appDir, cache) {
|
|
254
|
+
const pagesPrefix = norm2(pagesDir) + "/";
|
|
255
|
+
const appPrefix = norm2(appDir) + "/";
|
|
256
|
+
return {
|
|
257
|
+
name: "kiln:compile-server-split",
|
|
258
|
+
enforce: "post",
|
|
259
|
+
transform(code, rawId, options) {
|
|
260
|
+
const file = norm2(rawId.split("?")[0]);
|
|
261
|
+
if (!/\.[jt]sx?$/.test(file) || file.includes("/node_modules/")) return;
|
|
262
|
+
const parse = (c) => this.parse(c);
|
|
263
|
+
let out = code;
|
|
264
|
+
let changed = false;
|
|
265
|
+
if (!options?.ssr && file.startsWith(pagesPrefix) && !SERVER_FILE.test(file) && /\b(loader|action|revalidate)\b/.test(out)) {
|
|
266
|
+
const next = stripServerCode(out, parse);
|
|
267
|
+
if (next !== null) {
|
|
268
|
+
out = next;
|
|
269
|
+
changed = true;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (file.startsWith(appPrefix) && !SERVER_FILE.test(file)) {
|
|
273
|
+
const isIsland = cache.isIsland(file);
|
|
274
|
+
if (isIsland) {
|
|
275
|
+
const next = islandCode(out, parse, islandId(file));
|
|
276
|
+
if (next !== null) {
|
|
277
|
+
out = next;
|
|
278
|
+
changed = true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return changed ? { code: out, map: null } : void 0;
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
var specialEntry = (r) => `{ pattern: ${JSON.stringify(r.pattern)}, load: () => import(${JSON.stringify(r.file)}), layouts: [${r.layouts.map((f) => `() => import(${JSON.stringify(f)})`).join(", ")}] }`;
|
|
287
|
+
function routesPlugin(pagesDir, apiDir, appDir, cache) {
|
|
288
|
+
const pagesPrefix = norm2(pagesDir) + "/";
|
|
289
|
+
const apiPrefix = norm2(apiDir) + "/";
|
|
22
290
|
return {
|
|
23
291
|
name: "kiln:routes",
|
|
292
|
+
// Runs before Vite compiles JSX, so <kiln:title> is still visible.
|
|
24
293
|
enforce: "pre",
|
|
25
294
|
resolveId(source, _importer, options) {
|
|
26
|
-
if (source === VIRTUAL)
|
|
27
|
-
|
|
295
|
+
if (source === VIRTUAL) return RESOLVED;
|
|
296
|
+
if (source === VIRTUAL_ISLANDS) return RESOLVED_ISLANDS;
|
|
28
297
|
if (!options?.ssr && SERVER_FILE.test(source)) {
|
|
29
298
|
this.error(`"${source}" is server-only and cannot be imported by client code.`);
|
|
30
299
|
}
|
|
31
300
|
},
|
|
32
301
|
load(id, options) {
|
|
33
|
-
if (id !== RESOLVED)
|
|
34
|
-
return;
|
|
35
302
|
const ssr = !!options?.ssr;
|
|
36
|
-
|
|
303
|
+
if (id === RESOLVED_ISLANDS) {
|
|
304
|
+
if (ssr) return "export default {};\n";
|
|
305
|
+
const entries = islandFiles(appDir, cache).map(
|
|
306
|
+
(f) => `${JSON.stringify(islandId(f))}: () => import(${JSON.stringify(norm2(f))}).then((m) => ({ default: m.__kilnRaw }))`
|
|
307
|
+
);
|
|
308
|
+
return `export default {
|
|
309
|
+
${entries.join(",\n ")}
|
|
310
|
+
};
|
|
311
|
+
`;
|
|
312
|
+
}
|
|
313
|
+
if (id !== RESOLVED) return;
|
|
314
|
+
const pages = cache.routes().map((r) => {
|
|
37
315
|
const importable = ssr ? r.mode !== "client" : r.mode !== "server";
|
|
38
316
|
const load = importable ? `() => import(${JSON.stringify(r.file)})` : "null";
|
|
39
317
|
const layouts = r.layouts.map((f) => `() => import(${JSON.stringify(f)})`);
|
|
40
318
|
return `{ pattern: ${JSON.stringify(r.pattern)}, mode: ${JSON.stringify(r.mode)}, load: ${load}, layouts: [${layouts.join(", ")}] }`;
|
|
41
319
|
});
|
|
42
|
-
const apis = ssr ?
|
|
320
|
+
const apis = ssr ? cache.api().map(
|
|
321
|
+
(r) => `{ pattern: ${JSON.stringify(r.pattern)}, load: () => import(${JSON.stringify(r.file)}) }`
|
|
322
|
+
) : [];
|
|
323
|
+
const specials = ssr ? `{ errors: [${cache.specials("_error").map(specialEntry).join(", ")}], notFounds: [${cache.specials("_not-found").map(specialEntry).join(", ")}] }` : `{ errors: [], notFounds: [] }`;
|
|
43
324
|
return `export default [
|
|
44
|
-
${pages.join(
|
|
45
|
-
`)}
|
|
325
|
+
${pages.join(",\n ")}
|
|
46
326
|
];
|
|
47
327
|
|
|
48
328
|
export const api = [
|
|
49
|
-
${apis.join(
|
|
50
|
-
`)}
|
|
329
|
+
${apis.join(",\n ")}
|
|
51
330
|
];
|
|
331
|
+
|
|
332
|
+
export const specials = ${specials};
|
|
52
333
|
`;
|
|
53
334
|
},
|
|
54
335
|
transform(code, rawId) {
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
if (file.startsWith(pagesPrefix) && /\.(tsx|jsx)$/.test(file) && code.includes("<kiln:title")) {
|
|
58
|
-
const rewritten = code
|
|
59
|
-
return { code: TITLE_IMPORT + rewritten, map: null };
|
|
60
|
-
}
|
|
61
|
-
if (ENTRY_FILE.test(file)) {
|
|
62
|
-
return { code: PRELUDE + code, map: null };
|
|
336
|
+
const file = norm2(rawId.split("?")[0]);
|
|
337
|
+
const isMarkdownModule = file.endsWith(".kiln-md.tsx");
|
|
338
|
+
if (!isMarkdownModule && file.startsWith(pagesPrefix) && /\.(tsx|jsx)$/.test(file) && code.includes("<kiln:title")) {
|
|
339
|
+
const rewritten = rewriteTitleTags(code);
|
|
340
|
+
if (rewritten !== code) return { code: TITLE_IMPORT + rewritten, map: null };
|
|
63
341
|
}
|
|
342
|
+
if (ENTRY_FILE.test(file)) return { code: PRELUDE + code, map: null };
|
|
64
343
|
},
|
|
65
344
|
configureServer(server) {
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
345
|
+
const isKilnFile = (file) => {
|
|
346
|
+
const n = norm2(file);
|
|
347
|
+
return n.startsWith(pagesPrefix) || n.startsWith(apiPrefix) || n.startsWith(norm2(appDir) + "/");
|
|
348
|
+
};
|
|
349
|
+
const refresh = (file) => {
|
|
350
|
+
cache.invalidate(file);
|
|
351
|
+
for (const id of [RESOLVED, RESOLVED_ISLANDS]) {
|
|
352
|
+
const mod = server.moduleGraph.getModuleById(id);
|
|
353
|
+
if (mod) server.moduleGraph.invalidateModule(mod);
|
|
354
|
+
}
|
|
74
355
|
};
|
|
75
|
-
server.watcher.on("
|
|
76
|
-
|
|
356
|
+
server.watcher.on("change", (file) => {
|
|
357
|
+
if (isKilnFile(file)) refresh(file);
|
|
358
|
+
});
|
|
359
|
+
for (const event of ["add", "unlink"]) {
|
|
360
|
+
server.watcher.on(event, (file) => {
|
|
361
|
+
if (!isKilnFile(file)) return;
|
|
362
|
+
refresh(file);
|
|
363
|
+
server.ws.send({ type: "full-reload" });
|
|
364
|
+
});
|
|
365
|
+
}
|
|
77
366
|
}
|
|
78
367
|
};
|
|
79
368
|
}
|
|
80
369
|
function kiln(opts = {}) {
|
|
81
370
|
const pagesDir = path.resolve(opts.pagesDir ?? "app/pages");
|
|
82
371
|
const apiDir = path.resolve(opts.apiDir ?? "app/api");
|
|
83
|
-
|
|
372
|
+
const appDir = path.resolve(opts.appDir ?? "app");
|
|
373
|
+
const cache = createScanCache({ pagesDir, apiDir, appDir });
|
|
374
|
+
const extra = (opts.plugins ?? []).flatMap((p) => p.vite?.() ?? []);
|
|
375
|
+
return [
|
|
376
|
+
react(),
|
|
377
|
+
routesPlugin(pagesDir, apiDir, appDir, cache),
|
|
378
|
+
postPlugin(pagesDir, appDir, cache),
|
|
379
|
+
...extra
|
|
380
|
+
];
|
|
84
381
|
}
|
|
85
382
|
export {
|
|
86
|
-
|
|
87
|
-
|
|
383
|
+
buildCacheStats,
|
|
384
|
+
islandCode,
|
|
385
|
+
kiln,
|
|
386
|
+
rewriteTitleTags,
|
|
387
|
+
stripServerCode
|
|
88
388
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kiln-js/vite-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@kiln-js/core": "^0.
|
|
19
|
+
"@kiln-js/core": "^0.2.0",
|
|
20
20
|
"@vitejs/plugin-react": "^4.3.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|