@kiln-js/vite-plugin 0.1.6 → 0.2.0-hotfix
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 +21 -0
- package/dist/cache.d.ts +20 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +129 -35
- package/package.json +4 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zxsty
|
|
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/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
package/dist/index.js
CHANGED
|
@@ -1,15 +1,97 @@
|
|
|
1
1
|
// packages/vite-plugin/src/index.ts
|
|
2
|
-
import
|
|
2
|
+
import fs2 from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import react from "@vitejs/plugin-react";
|
|
5
|
-
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
|
|
6
89
|
var VIRTUAL = "virtual:kiln-routes";
|
|
7
90
|
var VIRTUAL_ISLANDS = "virtual:kiln-islands";
|
|
8
91
|
var RESOLVED = "\0" + VIRTUAL;
|
|
9
92
|
var RESOLVED_ISLANDS = "\0" + VIRTUAL_ISLANDS;
|
|
10
93
|
var SERVER_FILE = /\.server(\.[jt]sx?)?$/;
|
|
11
94
|
var ENTRY_FILE = /[\\/](app|\.kiln)[\\/](client|server)\.(tsx|ts|jsx|js)$/;
|
|
12
|
-
var ISLAND_DIRECTIVE = /^\s*(?:\/\/[^\n]*\n\s*)*(["'])use island\1/;
|
|
13
95
|
var SERVER_EXPORTS = /* @__PURE__ */ new Set(["loader", "action", "revalidate"]);
|
|
14
96
|
var PRELUDE = `import __kilnRoutes, { api as __kilnApi, specials as __kilnSpecials } from "${VIRTUAL}";
|
|
15
97
|
import __kilnIslands from "${VIRTUAL_ISLANDS}";
|
|
@@ -20,7 +102,7 @@ globalThis.__KILN_ISLANDS__ = __kilnIslands;
|
|
|
20
102
|
`;
|
|
21
103
|
var TITLE_IMPORT = `import { Title as KilnTitle } from "@kiln-js/core";
|
|
22
104
|
`;
|
|
23
|
-
var
|
|
105
|
+
var norm2 = (p) => p.replace(/\\/g, "/");
|
|
24
106
|
function rewriteTitleTags(code) {
|
|
25
107
|
const n = code.length;
|
|
26
108
|
let out = "";
|
|
@@ -155,8 +237,8 @@ export default __kilnIsland(${JSON.stringify(islandId2)}, ${raw});
|
|
|
155
237
|
`;
|
|
156
238
|
}
|
|
157
239
|
function sourceFiles(dir, out = []) {
|
|
158
|
-
if (!
|
|
159
|
-
for (const entry of
|
|
240
|
+
if (!fs2.existsSync(dir)) return out;
|
|
241
|
+
for (const entry of fs2.readdirSync(dir, { withFileTypes: true })) {
|
|
160
242
|
if (entry.name.startsWith(".") || entry.name === "node_modules" || entry.name === "dist") continue;
|
|
161
243
|
const full = path.join(dir, entry.name);
|
|
162
244
|
if (entry.isDirectory()) sourceFiles(full, out);
|
|
@@ -164,18 +246,18 @@ function sourceFiles(dir, out = []) {
|
|
|
164
246
|
}
|
|
165
247
|
return out;
|
|
166
248
|
}
|
|
167
|
-
var islandId = (file) =>
|
|
168
|
-
function islandFiles(appDir) {
|
|
169
|
-
return sourceFiles(appDir).filter((f) =>
|
|
249
|
+
var islandId = (file) => norm2(path.relative(process.cwd(), file));
|
|
250
|
+
function islandFiles(appDir, cache) {
|
|
251
|
+
return sourceFiles(appDir).filter((f) => cache.isIsland(f));
|
|
170
252
|
}
|
|
171
|
-
function postPlugin(pagesDir, appDir) {
|
|
172
|
-
const pagesPrefix =
|
|
173
|
-
const appPrefix =
|
|
253
|
+
function postPlugin(pagesDir, appDir, cache) {
|
|
254
|
+
const pagesPrefix = norm2(pagesDir) + "/";
|
|
255
|
+
const appPrefix = norm2(appDir) + "/";
|
|
174
256
|
return {
|
|
175
257
|
name: "kiln:compile-server-split",
|
|
176
258
|
enforce: "post",
|
|
177
259
|
transform(code, rawId, options) {
|
|
178
|
-
const file =
|
|
260
|
+
const file = norm2(rawId.split("?")[0]);
|
|
179
261
|
if (!/\.[jt]sx?$/.test(file) || file.includes("/node_modules/")) return;
|
|
180
262
|
const parse = (c) => this.parse(c);
|
|
181
263
|
let out = code;
|
|
@@ -188,12 +270,7 @@ function postPlugin(pagesDir, appDir) {
|
|
|
188
270
|
}
|
|
189
271
|
}
|
|
190
272
|
if (file.startsWith(appPrefix) && !SERVER_FILE.test(file)) {
|
|
191
|
-
|
|
192
|
-
try {
|
|
193
|
-
isIsland = ISLAND_DIRECTIVE.test(fs.readFileSync(file, "utf8"));
|
|
194
|
-
} catch {
|
|
195
|
-
isIsland = false;
|
|
196
|
-
}
|
|
273
|
+
const isIsland = cache.isIsland(file);
|
|
197
274
|
if (isIsland) {
|
|
198
275
|
const next = islandCode(out, parse, islandId(file));
|
|
199
276
|
if (next !== null) {
|
|
@@ -207,9 +284,9 @@ function postPlugin(pagesDir, appDir) {
|
|
|
207
284
|
};
|
|
208
285
|
}
|
|
209
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(", ")}] }`;
|
|
210
|
-
function routesPlugin(pagesDir, apiDir, appDir) {
|
|
211
|
-
const pagesPrefix =
|
|
212
|
-
const apiPrefix =
|
|
287
|
+
function routesPlugin(pagesDir, apiDir, appDir, cache) {
|
|
288
|
+
const pagesPrefix = norm2(pagesDir) + "/";
|
|
289
|
+
const apiPrefix = norm2(apiDir) + "/";
|
|
213
290
|
return {
|
|
214
291
|
name: "kiln:routes",
|
|
215
292
|
// Runs before Vite compiles JSX, so <kiln:title> is still visible.
|
|
@@ -225,8 +302,8 @@ function routesPlugin(pagesDir, apiDir, appDir) {
|
|
|
225
302
|
const ssr = !!options?.ssr;
|
|
226
303
|
if (id === RESOLVED_ISLANDS) {
|
|
227
304
|
if (ssr) return "export default {};\n";
|
|
228
|
-
const entries = islandFiles(appDir).map(
|
|
229
|
-
(f) => `${JSON.stringify(islandId(f))}: () => import(${JSON.stringify(
|
|
305
|
+
const entries = islandFiles(appDir, cache).map(
|
|
306
|
+
(f) => `${JSON.stringify(islandId(f))}: () => import(${JSON.stringify(norm2(f))}).then((m) => ({ default: m.__kilnRaw }))`
|
|
230
307
|
);
|
|
231
308
|
return `export default {
|
|
232
309
|
${entries.join(",\n ")}
|
|
@@ -234,16 +311,16 @@ function routesPlugin(pagesDir, apiDir, appDir) {
|
|
|
234
311
|
`;
|
|
235
312
|
}
|
|
236
313
|
if (id !== RESOLVED) return;
|
|
237
|
-
const pages =
|
|
314
|
+
const pages = cache.routes().map((r) => {
|
|
238
315
|
const importable = ssr ? r.mode !== "client" : r.mode !== "server";
|
|
239
316
|
const load = importable ? `() => import(${JSON.stringify(r.file)})` : "null";
|
|
240
317
|
const layouts = r.layouts.map((f) => `() => import(${JSON.stringify(f)})`);
|
|
241
318
|
return `{ pattern: ${JSON.stringify(r.pattern)}, mode: ${JSON.stringify(r.mode)}, load: ${load}, layouts: [${layouts.join(", ")}] }`;
|
|
242
319
|
});
|
|
243
|
-
const apis = ssr ?
|
|
320
|
+
const apis = ssr ? cache.api().map(
|
|
244
321
|
(r) => `{ pattern: ${JSON.stringify(r.pattern)}, load: () => import(${JSON.stringify(r.file)}) }`
|
|
245
322
|
) : [];
|
|
246
|
-
const specials = ssr ? `{ errors: [${
|
|
323
|
+
const specials = ssr ? `{ errors: [${cache.specials("_error").map(specialEntry).join(", ")}], notFounds: [${cache.specials("_not-found").map(specialEntry).join(", ")}] }` : `{ errors: [], notFounds: [] }`;
|
|
247
324
|
return `export default [
|
|
248
325
|
${pages.join(",\n ")}
|
|
249
326
|
];
|
|
@@ -256,7 +333,7 @@ export const specials = ${specials};
|
|
|
256
333
|
`;
|
|
257
334
|
},
|
|
258
335
|
transform(code, rawId) {
|
|
259
|
-
const file =
|
|
336
|
+
const file = norm2(rawId.split("?")[0]);
|
|
260
337
|
const isMarkdownModule = file.endsWith(".kiln-md.tsx");
|
|
261
338
|
if (!isMarkdownModule && file.startsWith(pagesPrefix) && /\.(tsx|jsx)$/.test(file) && code.includes("<kiln:title")) {
|
|
262
339
|
const rewritten = rewriteTitleTags(code);
|
|
@@ -265,17 +342,27 @@ export const specials = ${specials};
|
|
|
265
342
|
if (ENTRY_FILE.test(file)) return { code: PRELUDE + code, map: null };
|
|
266
343
|
},
|
|
267
344
|
configureServer(server) {
|
|
268
|
-
const
|
|
269
|
-
const
|
|
270
|
-
|
|
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);
|
|
271
351
|
for (const id of [RESOLVED, RESOLVED_ISLANDS]) {
|
|
272
352
|
const mod = server.moduleGraph.getModuleById(id);
|
|
273
353
|
if (mod) server.moduleGraph.invalidateModule(mod);
|
|
274
354
|
}
|
|
275
|
-
server.ws.send({ type: "full-reload" });
|
|
276
355
|
};
|
|
277
|
-
server.watcher.on("
|
|
278
|
-
|
|
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
|
+
}
|
|
279
366
|
}
|
|
280
367
|
};
|
|
281
368
|
}
|
|
@@ -283,10 +370,17 @@ function kiln(opts = {}) {
|
|
|
283
370
|
const pagesDir = path.resolve(opts.pagesDir ?? "app/pages");
|
|
284
371
|
const apiDir = path.resolve(opts.apiDir ?? "app/api");
|
|
285
372
|
const appDir = path.resolve(opts.appDir ?? "app");
|
|
373
|
+
const cache = createScanCache({ pagesDir, apiDir, appDir });
|
|
286
374
|
const extra = (opts.plugins ?? []).flatMap((p) => p.vite?.() ?? []);
|
|
287
|
-
return [
|
|
375
|
+
return [
|
|
376
|
+
react(),
|
|
377
|
+
routesPlugin(pagesDir, apiDir, appDir, cache),
|
|
378
|
+
postPlugin(pagesDir, appDir, cache),
|
|
379
|
+
...extra
|
|
380
|
+
];
|
|
288
381
|
}
|
|
289
382
|
export {
|
|
383
|
+
buildCacheStats,
|
|
290
384
|
islandCode,
|
|
291
385
|
kiln,
|
|
292
386
|
rewriteTitleTags,
|
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-hotfix",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -15,8 +15,10 @@
|
|
|
15
15
|
"build": "bun build ./src/index.ts --outdir ./dist --target node --packages external \u0026\u0026 tsc -p tsconfig.json --emitDeclarationOnly",
|
|
16
16
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
17
17
|
},
|
|
18
|
+
"repository": "github:kiln-js/vite-plugin",
|
|
19
|
+
"homepage": "https://kiln-js.vercel.app",
|
|
18
20
|
"dependencies": {
|
|
19
|
-
"@kiln-js/core": "^0.
|
|
21
|
+
"@kiln-js/core": "^0.2.0-hotfix",
|
|
20
22
|
"@vitejs/plugin-react": "^4.3.0"
|
|
21
23
|
},
|
|
22
24
|
"peerDependencies": {
|