@kiln-js/vite-plugin 0.0.0-stage → 0.1.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.
@@ -0,0 +1,6 @@
1
+ import type { PluginOption } from "vite";
2
+ export interface KilnOptions {
3
+ pagesDir?: string;
4
+ apiDir?: string;
5
+ }
6
+ export declare function kiln(opts?: KilnOptions): PluginOption[];
package/dist/index.js ADDED
@@ -0,0 +1,87 @@
1
+ // src/index.ts
2
+ import path from "node:path";
3
+ import react from "@vitejs/plugin-react";
4
+ import { scanRoutes, scanApi } from "@kiln/core/scan";
5
+ var VIRTUAL = "virtual:kiln-routes";
6
+ var RESOLVED = "\x00" + VIRTUAL;
7
+ var SERVER_FILE = /\.server(\.[jt]sx?)?$/;
8
+ var ENTRY_FILE = /[\\/](app|\.kiln)[\\/](client|server)\.(tsx|ts|jsx|js)$/;
9
+ var PRELUDE = `import __kilnRoutes, { api as __kilnApi } from "${VIRTUAL}";
10
+ globalThis.__KILN_ROUTES__ = __kilnRoutes;
11
+ globalThis.__KILN_API__ = __kilnApi;
12
+ `;
13
+ var TITLE_IMPORT = `import { Title as KilnTitle } from "@kiln/core";
14
+ `;
15
+ var norm = (p) => p.replace(/\\/g, "/");
16
+ function routesPlugin(pagesDir, apiDir) {
17
+ const pagesPrefix = norm(pagesDir) + "/";
18
+ const apiPrefix = norm(apiDir) + "/";
19
+ return {
20
+ name: "kiln:routes",
21
+ enforce: "pre",
22
+ config() {
23
+ return { envPrefix: ["VITE_", "KILN_PUBLIC_"] };
24
+ },
25
+ resolveId(source, _importer, options) {
26
+ if (source === VIRTUAL)
27
+ return RESOLVED;
28
+ if (!options?.ssr && SERVER_FILE.test(source)) {
29
+ this.error(`"${source}" is server-only and cannot be imported by client code.`);
30
+ }
31
+ },
32
+ load(id, options) {
33
+ if (id !== RESOLVED)
34
+ return;
35
+ const ssr = !!options?.ssr;
36
+ const pages = scanRoutes(pagesDir).map((r) => {
37
+ const importable = ssr ? r.mode !== "client" : r.mode !== "server";
38
+ const load = importable ? `() => import(${JSON.stringify(r.file)})` : "null";
39
+ const layouts = r.layouts.map((f) => `() => import(${JSON.stringify(f)})`);
40
+ return `{ pattern: ${JSON.stringify(r.pattern)}, mode: ${JSON.stringify(r.mode)}, load: ${load}, layouts: [${layouts.join(", ")}] }`;
41
+ });
42
+ const apis = ssr ? scanApi(apiDir).map((r) => `{ pattern: ${JSON.stringify(r.pattern)}, load: () => import(${JSON.stringify(r.file)}) }`) : [];
43
+ return `export default [
44
+ ${pages.join(`,
45
+ `)}
46
+ ];
47
+
48
+ export const api = [
49
+ ${apis.join(`,
50
+ `)}
51
+ ];
52
+ `;
53
+ },
54
+ transform(code, rawId) {
55
+ const id = rawId.split("?")[0];
56
+ const file = norm(id);
57
+ if (file.startsWith(pagesPrefix) && /\.(tsx|jsx)$/.test(file) && code.includes("<kiln:title")) {
58
+ const rewritten = code.replace(/<kiln:title>/g, "<KilnTitle>").replace(/<\/kiln:title>/g, "</KilnTitle>");
59
+ return { code: TITLE_IMPORT + rewritten, map: null };
60
+ }
61
+ if (ENTRY_FILE.test(file)) {
62
+ return { code: PRELUDE + code, map: null };
63
+ }
64
+ },
65
+ configureServer(server) {
66
+ const onChange = (file) => {
67
+ const f = norm(file);
68
+ if (!f.startsWith(pagesPrefix) && !f.startsWith(apiPrefix))
69
+ return;
70
+ const mod = server.moduleGraph.getModuleById(RESOLVED);
71
+ if (mod)
72
+ server.moduleGraph.invalidateModule(mod);
73
+ server.ws.send({ type: "full-reload" });
74
+ };
75
+ server.watcher.on("add", onChange);
76
+ server.watcher.on("unlink", onChange);
77
+ }
78
+ };
79
+ }
80
+ function kiln(opts = {}) {
81
+ const pagesDir = path.resolve(opts.pagesDir ?? "app/pages");
82
+ const apiDir = path.resolve(opts.apiDir ?? "app/api");
83
+ return [react(), routesPlugin(pagesDir, apiDir)];
84
+ }
85
+ export {
86
+ kiln
87
+ };
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
- {
2
- "name": "@kiln-js/vite-plugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
1
+ {
2
+ "name": "@kiln-js/vite-plugin",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "import": "./dist/index.js"
9
+ }
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "scripts": {
15
+ "build": "bun build ./src/index.ts --outdir ./dist --target node --packages external \u0026\u0026 tsc -p tsconfig.json --emitDeclarationOnly",
16
+ "typecheck": "tsc -p tsconfig.json --noEmit"
17
+ },
18
+ "dependencies": {
19
+ "@kiln/core": "*",
20
+ "@vitejs/plugin-react": "^4.3.0"
21
+ },
22
+ "peerDependencies": {
23
+ "vite": "\u003e=5"
24
+ },
25
+ "devDependencies": {
26
+ "vite": "^5.4.0"
27
+ }
6
28
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.