-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathindex.ts
More file actions
238 lines (229 loc) · 8.29 KB
/
index.ts
File metadata and controls
238 lines (229 loc) · 8.29 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { TanStackServerFnPlugin } from "@tanstack/server-functions-plugin";
import { defu } from "defu";
import { globSync } from "node:fs";
import { extname, isAbsolute, join } from "node:path";
import { fileURLToPath } from "node:url";
import { normalizePath, type PluginOption } from "vite";
import solid, { type Options as SolidOptions } from "vite-plugin-solid";
import { imagePlugin, type StartImageOptions } from "../image/plugin/index.ts";
import { DEFAULT_EXTENSIONS, VIRTUAL_MODULES, VITE_ENVIRONMENTS } from "./constants.ts";
import { devServer } from "./dev-server.ts";
import { SolidStartClientFileRouter, SolidStartServerFileRouter } from "./fs-router.ts";
import { fsRoutes } from "./fs-routes/index.ts";
import type { BaseFileSystemRouter } from "./fs-routes/router.ts";
import lazy from "./lazy.ts";
import { manifest } from "./manifest.ts";
import { parseIdQuery } from "./utils.ts";
export interface SolidStartOptions {
solid?: Partial<SolidOptions>;
ssr?: boolean;
routeDir?: string;
extensions?: string[];
middleware?: string;
image?: StartImageOptions;
}
const absolute = (path: string, root: string) =>
path ? (isAbsolute(path) ? path : join(root, path)) : path;
export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
const start = defu(options ?? {}, {
appRoot: "./src",
routeDir: "./routes",
ssr: true,
devOverlay: true,
experimental: {
islands: false,
},
solid: {},
extensions: [],
});
const extensions = [...DEFAULT_EXTENSIONS, ...(start.extensions || [])];
const routeDir = join(start.appRoot, start.routeDir);
const root = process.cwd();
const appEntryPath = globSync(join(root, start.appRoot, "app.{j,t}sx"))[0];
if (!appEntryPath) {
throw new Error(`Could not find an app jsx/tsx entry in ${start.appRoot}.`);
}
const entryExtension = extname(appEntryPath);
const handlers = {
client: `${start.appRoot}/entry-client${entryExtension}`,
server: `${start.appRoot}/entry-server${entryExtension}`,
};
return [
{
name: "solid-start:config",
enforce: "pre",
configEnvironment(name) {
return {
resolve: {
// remove when https://github.com/solidjs/vite-plugin-solid/pull/228 is released
externalConditions: ["solid", "node"],
},
};
},
async config(_, env) {
const clientInput = [handlers.client];
if (env.command === "build") {
const clientRouter: BaseFileSystemRouter = (globalThis as any).ROUTERS.client;
for (const route of await clientRouter.getRoutes()) {
for (const [key, value] of Object.entries(route)) {
if (value && key.startsWith("$") && !key.startsWith("$$")) {
function toRouteId(route: any) {
return `${route.src}?${route.pick.map((p: string) => `pick=${p}`).join("&")}`;
}
clientInput.push(toRouteId(value));
}
}
}
}
return {
appType: "custom",
build: { assetsDir: "_build/assets" },
environments: {
[VITE_ENVIRONMENTS.client]: {
consumer: "client",
build: {
write: true,
manifest: true,
outDir: "dist/client",
rollupOptions: {
input: clientInput,
treeshake: true,
preserveEntrySignatures: "exports-only",
},
},
},
[VITE_ENVIRONMENTS.server]: {
consumer: "server",
build: {
ssr: true,
write: true,
manifest: true,
copyPublicDir: false,
rollupOptions: {
input: "~/entry-server.tsx",
},
outDir: "dist/server",
commonjsOptions: {
include: [/node_modules/],
},
},
},
},
resolve: {
alias: {
"@solidjs/start/server/entry": handlers.server,
"~": join(process.cwd(), start.appRoot),
...(!start.ssr
? {
"@solidjs/start/server": "@solidjs/start/server/spa",
"@solidjs/start/client": "@solidjs/start/client/spa",
}
: {}),
},
},
define: {
"import.meta.env.MANIFEST": `globalThis.MANIFEST`,
"import.meta.env.START_SSR": JSON.stringify(start.ssr),
// Use JSON.stringify so backslashes on Windows are escaped and
// esbuild receives a valid JS string literal for the define value
"import.meta.env.START_APP_ENTRY": JSON.stringify(appEntryPath),
"import.meta.env.START_CLIENT_ENTRY": JSON.stringify(handlers.client),
"import.meta.env.START_DEV_OVERLAY": JSON.stringify(start.devOverlay),
},
builder: {
sharedPlugins: true,
async buildApp(builder) {
const client = builder.environments[VITE_ENVIRONMENTS.client];
const server = builder.environments[VITE_ENVIRONMENTS.server];
if (!client) throw new Error("Client environment not found");
if (!server) throw new Error("SSR environment not found");
if (!client.isBuilt) await builder.build(client);
if (!server.isBuilt) await builder.build(server);
},
},
};
},
},
manifest(start),
fsRoutes({
routers: {
client: new SolidStartClientFileRouter({
dir: absolute(routeDir, root),
extensions,
}),
ssr: new SolidStartServerFileRouter({
dir: absolute(routeDir, root),
extensions,
dataOnly: !start.ssr,
}),
},
}),
lazy(),
// Must be placed after fsRoutes, as treeShake will remove the
// server fn exports added in by this plugin
TanStackServerFnPlugin({
// This is the ID that will be available to look up and import
// our server function manifest and resolve its module
manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,
directive: "use server",
callers: [
{
envConsumer: "client",
envName: VITE_ENVIRONMENTS.client,
getRuntimeCode: () =>
`import { createServerReference } from "${normalizePath(
fileURLToPath(new URL("../server/server-runtime", import.meta.url)),
)}"`,
replacer: opts => `createServerReference('${opts.functionId}')`,
},
{
envConsumer: "server",
envName: VITE_ENVIRONMENTS.server,
getRuntimeCode: () =>
`import { createServerReference } from '${normalizePath(
fileURLToPath(new URL("../server/server-fns-runtime", import.meta.url)),
)}'`,
replacer: opts => `createServerReference(${opts.fn}, '${opts.functionId}')`,
},
],
provider: {
envName: VITE_ENVIRONMENTS.server,
getRuntimeCode: () =>
`import { createServerReference } from '${normalizePath(
fileURLToPath(new URL("../server/server-fns-runtime", import.meta.url)),
)}'`,
replacer: opts => `createServerReference(${opts.fn}, '${opts.functionId}')`,
},
}),
options?.image ? imagePlugin(options.image) : undefined,
{
name: "solid-start:virtual-modules",
async resolveId(id) {
const { filename, query } = parseIdQuery(id);
let base;
if (filename === VIRTUAL_MODULES.clientEntry) base = handlers.client;
if (filename === VIRTUAL_MODULES.serverEntry) base = handlers.server;
if (filename === VIRTUAL_MODULES.app) base = appEntryPath;
if (base) {
let id = (await this.resolve(base))?.id;
if (!id) return;
if (query.size > 0) id += `?${query.toString()}`;
return id;
}
},
},
{
name: "solid-start:capture-client-bundle",
enforce: "post",
generateBundle(_options, bundle) {
globalThis.START_CLIENT_BUNDLE = bundle;
},
},
devServer(),
solid({
...start.solid,
ssr: true,
extensions: extensions.map(ext => `.${ext}`),
}),
];
}