-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
175 lines (159 loc) · 4.67 KB
/
cli.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { defineCommand, runMain } from "citty";
import { readFileSync } from "fs";
import { fileURLToPath } from "node:url";
import solid from "vite-plugin-solid";
function clientViteConfig() {
return {
plugins: [solid()]
}
}
const command = defineCommand({
subCommands: {
dev: {
args: {
port: {
description: "port (default: 3000)",
default: 3000,
},
},
run: async ({ args: { port } }) => {
const { createNitro, createDevServer, prepare, build } = await import("nitropack");
const devHandler = await createDevHandler();
const nitro = await createNitro({
dev: true,
noPublicDir: true, // Leave handling of public dir to vite
experimental: {
websocket: true
},
devHandlers: [
{
route: "/",
handler: devHandler
}
],
rollupConfig: {
plugins: [
{
load(id) {
if (id.endsWith("nitro-dev.mjs")) {
return readFileSync(fileURLToPath(new URL("./lib/nitro/nitro-dev.mjs", import.meta.url)), "utf-8");
}
}
}
]
}
});
const server = createDevServer(nitro);
await server.listen(port);
await prepare(nitro);
await build(nitro);
},
},
build: {
run: async () => {
const { build: viteBuild } = await import("vite");
const { rmSync } = await import("fs");
const { createNitro, prepare, copyPublicAssets, build } = await import("nitropack");
await viteBuild(clientViteConfig());
const nitro = await createNitro({
experimental: {
websocket: true
},
publicAssets: [
{
baseURL: "/",
dir: fileURLToPath(new URL("./dist", import.meta.url)),
},
],
handlers: [
{ route: "/**", handler: "#indexHandler" }
],
virtual: {
"#indexHandler": `
import { eventHandler } from 'h3';
export default eventHandler(() => $fetch('/'));
`,
},
rollupConfig: {
plugins: [
{
load(id) {
if (id.endsWith("node-server.mjs")) {
return readFileSync(fileURLToPath(new URL("./lib/nitro/node-server.mjs", import.meta.url)), "utf-8");
}
}
}
]
}
});
await prepare(nitro);
await copyPublicAssets(nitro);
rmSync("dist", { recursive: true, force: true });
await build(nitro);
await nitro.close();
},
},
start: {
run: () => {
import(new URL(".output/server/index.mjs", import.meta.url).href);
},
},
},
});
async function createDevHandler() {
const { fromNodeMiddleware, eventHandler } = await import("h3");
const { createServer } = await import("vite");
const viteServer = await createServer({
...clientViteConfig(),
server: {
middlewareMode: true,
},
});
const viteHandler = fromNodeMiddleware(viteServer.middlewares);
const router = await createNitroRouter();
return eventHandler(event => {
if (router.isNitroRoute(event)) return;
return viteHandler(event);
});
}
async function createNitroRouter() {
const { default: fg } = (await import("fast-glob"));
const { createRouter, addRoute, findRoute } = await import("rou3");
const extensions = ["js", "ts"];
const router = createRouter();
fg.globSync([
`./routes/**/*.{${extensions.join(",")}}`,
`./api/**/*.{${extensions.join(",")}}`
]).forEach(src => {
const path = src
.slice(src.startsWith("./api") ? 1 : "./routes".length)
.replace(new RegExp(`\.(${(extensions).join("|")})$`), "");
let routePath = path
.slice(1)
.replace(/index$/, "")
.replace(/\[([^\/]+)\]/g, (_, m) => {
if (m.length > 3 && m.startsWith("...")) {
return `*${m.slice(3)}`;
}
if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
return `:${m.slice(1, -1)}?`;
}
return `:${m}`;
});
routePath = routePath?.length > 0 ? `/${routePath}` : "/";
addRoute(router, undefined, routePath);
});
const cache = new Set();
return {
isNitroRoute: (event) => {
const pathname = event.path.split("?")[0];
if (cache.has(pathname)) return true;
return Boolean(
findRoute(router, event.method, pathname) &&
cache.add(pathname) &&
true
);
}
};
}
runMain(command);