-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathindex.ts
More file actions
212 lines (195 loc) · 6.36 KB
/
index.ts
File metadata and controls
212 lines (195 loc) · 6.36 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
import {
build,
copyPublicAssets,
createNitro,
type Nitro,
type NitroConfig,
prepare,
prerender,
} from "nitropack";
import { promises as fsp } from "node:fs";
import path, { dirname, resolve } from "node:path";
import type { PluginOption, Rollup } from "vite";
let ssrBundle: Rollup.OutputBundle;
let ssrEntryFile: string;
export type UserNitroConfig = Omit<NitroConfig, "dev" | "publicAssets" | "renderer">;
export function nitroV2Plugin(nitroConfig?: UserNitroConfig): PluginOption {
return [
{
name: "solid-start-vite-plugin-nitro",
generateBundle: {
handler(_options, bundle) {
if (this.environment.name !== "ssr") {
return;
}
// find entry point of ssr bundle
let entryFile: string | undefined;
for (const [_name, file] of Object.entries(bundle)) {
if (file.type === "chunk") {
if (file.isEntry) {
if (entryFile !== undefined) {
this.error(
`Multiple entry points found for service "${this.environment.name}". Only one entry point is allowed.`,
);
}
entryFile = file.fileName;
}
}
}
if (entryFile === undefined) {
this.error(`No entry point found for service "${this.environment.name}".`);
}
ssrEntryFile = entryFile!;
ssrBundle = bundle;
},
},
config() {
return {
environments: {
ssr: {
consumer: "server",
build: {
commonjsOptions: {
include: [],
},
ssr: true,
sourcemap: true,
},
},
},
builder: {
sharedPlugins: true,
async buildApp(builder) {
const client = builder.environments.client;
const server = builder.environments.ssr;
if (!client) throw new Error("Client environment not found");
if (!server) throw new Error("SSR environment not found");
await builder.build(client);
await builder.build(server);
const virtualEntry = "#solid-start/entry";
const resolvedNitroConfig: NitroConfig = {
compatibilityDate: "2024-11-19",
logLevel: 3,
preset: "node-server",
typescript: {
generateTsConfig: false,
generateRuntimeConfigTypes: false,
},
compressPublicAssets: true,
...nitroConfig,
dev: false,
routeRules: {
"/_build/assets/**": {
headers: {
"cache-control": "public, immutable, max-age=31536000",
},
},
},
publicAssets: [
{
dir: client.config.build.outDir,
maxAge: 31536000, // 1 year
baseURL: "/",
},
],
noExternals: false,
renderer: virtualEntry,
rollupConfig: {
...nitroConfig?.rollupConfig,
plugins: [virtualBundlePlugin(ssrBundle) as any],
},
experimental: {
...nitroConfig?.experimental,
asyncContext: true,
},
virtual: {
...nitroConfig?.virtual,
[virtualEntry]: `import { fromWebHandler } from 'h3'
import handler from '${ssrEntryFile}'
export default fromWebHandler(handler.fetch)`,
},
};
const nitro = await createNitro(resolvedNitroConfig);
await buildNitroEnvironment(nitro, () => build(nitro));
},
},
};
},
},
{
name: "solid-start-nitro-edge-fix",
enforce: "post",
async config() {
await fsp.rm(".solid-start", { recursive: true, force: true });
return {
environments: {
client: { build: { outDir: ".solid-start/client" } },
ssr: {
build: nitroConfig?.preset?.toLowerCase().includes("static")
? undefined
: { outDir: ".solid-start/server" },
},
},
};
},
},
];
}
export async function buildNitroEnvironment(nitro: Nitro, build: () => Promise<any>) {
await prepare(nitro);
await copyPublicAssets(nitro);
await prerender(nitro);
await build();
const publicDir = nitro.options.output.publicDir;
// As a part of the build process, the `.vite/` directory
// is copied over from `node_modules/.tanstack-start/client-dist/`
// to the `publicDir` (e.g. `.output/public/`).
// This directory (containing the vite manifest) should not be
// included in the final build, so we remove it here.
const viteDir = path.resolve(publicDir, ".vite");
if (await fsp.stat(viteDir).catch(() => false)) {
await fsp.rm(viteDir, { recursive: true, force: true });
}
await nitro.close();
}
function virtualBundlePlugin(ssrBundle: Rollup.OutputBundle): PluginOption {
type VirtualModule = { code: string; map: string | null };
const _modules = new Map<string, VirtualModule>();
// group chunks and source maps
for (const [fileName, content] of Object.entries(ssrBundle)) {
if (content.type === "chunk") {
const virtualModule: VirtualModule = {
code: content.code,
map: null,
};
const maybeMap = ssrBundle[`${fileName}.map`];
if (maybeMap && maybeMap.type === "asset") {
virtualModule.map = maybeMap.source as string;
}
_modules.set(fileName, virtualModule);
_modules.set(resolve(fileName), virtualModule);
}
}
return {
name: "virtual-bundle",
resolveId(id, importer) {
if (_modules.has(id)) {
return resolve(id);
}
if (importer) {
const resolved = resolve(dirname(importer), id);
if (_modules.has(resolved)) {
return resolved;
}
}
return null;
},
load(id) {
const m = _modules.get(id);
if (!m) {
return null;
}
return m;
},
};
}