-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathindex.ts
More file actions
136 lines (113 loc) · 3.46 KB
/
index.ts
File metadata and controls
136 lines (113 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { relative } from "node:path";
import { type PluginOption } from "vite";
import { fileSystemWatcher } from "./fs-watcher.ts";
import type { BaseFileSystemRouter } from "./router.ts";
import { treeShake } from "./tree-shake.ts";
export const moduleId = "solid-start:routes";
export interface FsRoutesArgs {
routers: Record<"client" | "ssr", BaseFileSystemRouter>;
}
export function fsRoutes({ routers }: FsRoutesArgs): Array<PluginOption> {
(globalThis as any).ROUTERS = routers;
return [
{
name: "solid-start-fs-routes",
enforce: "pre",
resolveId(id) {
if (id === moduleId) return id;
},
async load(id) {
const root = this.environment.config.root;
const isBuild = this.environment.mode === "build";
if (id !== moduleId) return;
const js = jsCode();
const router = (globalThis as any).ROUTERS[this.environment.name];
const routes = await router.getRoutes();
let routesCode = JSON.stringify(routes ?? [], (k, v) => {
if (v === undefined) return undefined;
if (k.startsWith("$$")) {
const buildId = `${v.src}?${v.pick.map((p: any) => `pick=${p}`).join("&")}`;
/**
* @type {{ [key: string]: string }}
*/
const refs: Record<string, string> = {};
for (var pick of v.pick) {
refs[pick] = js.addNamedImport(pick, buildId);
}
return {
require: `_$() => ({ ${Object.entries(refs)
.map(([pick, namedImport]) => `'${pick}': ${namedImport}`)
.join(", ")} })$_`,
// src: isBuild ? relative(root, buildId) : buildId
};
} else if (k.startsWith("$")) {
const buildId = `${v.src}?${v.pick.map((p: any) => `pick=${p}`).join("&")}`;
return {
src: relative(root, buildId),
build: isBuild ? `_$() => import('${buildId}')$_` : undefined,
import: `_$() => import('${buildId}')$_`,
};
}
return v;
});
routesCode = routesCode.replaceAll('"_$(', "(").replaceAll(')$_"', ")");
const code = `
${js.getImportStatements()}
export default ${routesCode}`;
return code;
},
},
treeShake(),
fileSystemWatcher(routers),
];
}
function jsCode() {
const imports = new Map();
let vars = 0;
function addImport(p: any) {
let id = imports.get(p);
if (!id) {
id = {};
imports.set(p, id);
}
const d = "routeData" + vars++;
id["default"] = d;
return d;
}
function addNamedImport(name: string | number, p: any) {
let id = imports.get(p);
if (!id) {
id = {};
imports.set(p, id);
}
const d = "routeData" + vars++;
id[name] = d;
return d;
}
const getNamedExport = (p: any) => {
const id = imports.get(p);
delete id["default"];
return Object.keys(id).length > 0
? `{ ${Object.keys(id)
.map(k => `${k} as ${id[k]}`)
.join(", ")} }`
: "";
};
const getImportStatements = () => {
return `${[...imports.keys()]
.map(
i =>
`import ${
imports.get(i).default
? `${imports.get(i).default}${Object.keys(imports.get(i)).length > 1 ? ", " : ""}`
: ""
} ${getNamedExport(i)} from '${i}';`,
)
.join("\n")}`;
};
return {
addImport,
addNamedImport,
getImportStatements,
};
}