|
1 | | -import { promises as fsp } from "node:fs"; |
2 | | -import path, { dirname, resolve } from "node:path"; |
3 | 1 | import { |
4 | | - build, |
5 | | - copyPublicAssets, |
6 | | - createNitro, |
7 | | - type Nitro, |
8 | | - type NitroConfig, |
9 | | - prepare, |
10 | | - prerender, |
| 2 | + build, |
| 3 | + copyPublicAssets, |
| 4 | + createNitro, |
| 5 | + type Nitro, |
| 6 | + type NitroConfig, |
| 7 | + prepare, |
| 8 | + prerender |
11 | 9 | } from "nitropack"; |
| 10 | +import { promises as fsp } from "node:fs"; |
| 11 | +import path, { dirname, resolve } from "node:path"; |
12 | 12 | import type { PluginOption, Rollup } from "vite"; |
13 | 13 |
|
14 | 14 | let ssrBundle: Rollup.OutputBundle; |
15 | 15 | let ssrEntryFile: string; |
16 | 16 |
|
17 | | -export type UserNitroConfig = Omit< |
18 | | - NitroConfig, |
19 | | - "dev" | "publicAssets" | "renderer" |
20 | | ->; |
| 17 | +export type UserNitroConfig = Omit<NitroConfig, "dev" | "publicAssets" | "renderer">; |
21 | 18 |
|
22 | 19 | export function nitroV2Plugin(nitroConfig?: UserNitroConfig): PluginOption { |
23 | | - return { |
24 | | - name: "solid-start-vite-plugin-nitro", |
25 | | - generateBundle: { |
26 | | - handler(_options, bundle) { |
27 | | - if (this.environment.name !== "ssr") { |
28 | | - return; |
29 | | - } |
30 | | - |
31 | | - // find entry point of ssr bundle |
32 | | - let entryFile: string | undefined; |
33 | | - for (const [_name, file] of Object.entries(bundle)) { |
34 | | - if (file.type === "chunk") { |
35 | | - if (file.isEntry) { |
36 | | - if (entryFile !== undefined) { |
37 | | - this.error( |
38 | | - `Multiple entry points found for service "${this.environment.name}". Only one entry point is allowed.`, |
39 | | - ); |
40 | | - } |
41 | | - entryFile = file.fileName; |
42 | | - } |
43 | | - } |
44 | | - } |
45 | | - if (entryFile === undefined) { |
46 | | - this.error( |
47 | | - `No entry point found for service "${this.environment.name}".`, |
48 | | - ); |
49 | | - } |
50 | | - ssrEntryFile = entryFile!; |
51 | | - ssrBundle = bundle; |
52 | | - }, |
53 | | - }, |
54 | | - config() { |
55 | | - return { |
56 | | - environments: { |
57 | | - ssr: { |
58 | | - consumer: "server", |
59 | | - build: { |
60 | | - commonjsOptions: { |
61 | | - include: [], |
62 | | - }, |
63 | | - ssr: true, |
64 | | - sourcemap: true, |
65 | | - }, |
66 | | - }, |
67 | | - }, |
68 | | - builder: { |
69 | | - sharedPlugins: true, |
70 | | - async buildApp(builder) { |
71 | | - const client = builder.environments.client; |
72 | | - const server = builder.environments.ssr; |
73 | | - |
74 | | - if (!client) throw new Error("Client environment not found"); |
75 | | - if (!server) throw new Error("SSR environment not found"); |
76 | | - |
77 | | - await builder.build(client); |
78 | | - await builder.build(server); |
79 | | - |
80 | | - const virtualEntry = "#solid-start/entry"; |
81 | | - const resolvedNitroConfig: NitroConfig = { |
82 | | - compatibilityDate: "2024-11-19", |
83 | | - logLevel: 3, |
84 | | - preset: "node-server", |
85 | | - typescript: { |
86 | | - generateTsConfig: false, |
87 | | - generateRuntimeConfigTypes: false, |
88 | | - }, |
89 | | - ...nitroConfig, |
90 | | - dev: false, |
91 | | - publicAssets: [ |
92 | | - { |
93 | | - dir: client.config.build.outDir, |
94 | | - maxAge: 31536000, // 1 year |
95 | | - baseURL: "/", |
96 | | - }, |
97 | | - ], |
98 | | - renderer: virtualEntry, |
99 | | - rollupConfig: { |
100 | | - ...nitroConfig?.rollupConfig, |
101 | | - plugins: [virtualBundlePlugin(ssrBundle) as any], |
102 | | - }, |
103 | | - experimental: { |
104 | | - ...nitroConfig?.experimental, |
105 | | - asyncContext: true, |
106 | | - }, |
107 | | - virtual: { |
108 | | - ...nitroConfig?.virtual, |
109 | | - [virtualEntry]: `import { fromWebHandler } from 'h3' |
| 20 | + return { |
| 21 | + name: "solid-start-vite-plugin-nitro", |
| 22 | + generateBundle: { |
| 23 | + handler(_options, bundle) { |
| 24 | + if (this.environment.name !== "ssr") { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // find entry point of ssr bundle |
| 29 | + let entryFile: string | undefined; |
| 30 | + for (const [_name, file] of Object.entries(bundle)) { |
| 31 | + if (file.type === "chunk") { |
| 32 | + if (file.isEntry) { |
| 33 | + if (entryFile !== undefined) { |
| 34 | + this.error( |
| 35 | + `Multiple entry points found for service "${this.environment.name}". Only one entry point is allowed.` |
| 36 | + ); |
| 37 | + } |
| 38 | + entryFile = file.fileName; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + if (entryFile === undefined) { |
| 43 | + this.error(`No entry point found for service "${this.environment.name}".`); |
| 44 | + } |
| 45 | + ssrEntryFile = entryFile; |
| 46 | + ssrBundle = bundle; |
| 47 | + } |
| 48 | + }, |
| 49 | + config() { |
| 50 | + return { |
| 51 | + environments: { |
| 52 | + ssr: { |
| 53 | + consumer: "server", |
| 54 | + build: { |
| 55 | + commonjsOptions: { |
| 56 | + include: [] |
| 57 | + }, |
| 58 | + ssr: true, |
| 59 | + sourcemap: true |
| 60 | + } |
| 61 | + } |
| 62 | + }, |
| 63 | + builder: { |
| 64 | + sharedPlugins: true, |
| 65 | + async buildApp(builder) { |
| 66 | + const client = builder.environments.client; |
| 67 | + const server = builder.environments.ssr; |
| 68 | + |
| 69 | + if (!client) throw new Error("Client environment not found"); |
| 70 | + if (!server) throw new Error("SSR environment not found"); |
| 71 | + |
| 72 | + await builder.build(client); |
| 73 | + await builder.build(server); |
| 74 | + |
| 75 | + const virtualEntry = "#solid-start/entry"; |
| 76 | + const resolvedNitroConfig: NitroConfig = { |
| 77 | + compatibilityDate: "2024-11-19", |
| 78 | + logLevel: 3, |
| 79 | + preset: "node-server", |
| 80 | + typescript: { |
| 81 | + generateTsConfig: false, |
| 82 | + generateRuntimeConfigTypes: false |
| 83 | + }, |
| 84 | + ...nitroConfig, |
| 85 | + dev: false, |
| 86 | + publicAssets: [ |
| 87 | + { |
| 88 | + dir: client.config.build.outDir, |
| 89 | + maxAge: 31536000, // 1 year |
| 90 | + baseURL: "/" |
| 91 | + } |
| 92 | + ], |
| 93 | + renderer: virtualEntry, |
| 94 | + rollupConfig: { |
| 95 | + ...nitroConfig?.rollupConfig, |
| 96 | + plugins: [virtualBundlePlugin(ssrBundle) as any] |
| 97 | + }, |
| 98 | + experimental: { |
| 99 | + ...nitroConfig?.experimental, |
| 100 | + asyncContext: true |
| 101 | + }, |
| 102 | + virtual: { |
| 103 | + ...nitroConfig?.virtual, |
| 104 | + [virtualEntry]: `import { fromWebHandler } from 'h3' |
110 | 105 | import handler from '${ssrEntryFile}' |
111 | | - export default fromWebHandler(handler.fetch)`, |
112 | | - }, |
113 | | - }; |
114 | | - |
115 | | - const nitro = await createNitro(resolvedNitroConfig); |
116 | | - |
117 | | - await buildNitroEnvironment(nitro, () => build(nitro)); |
118 | | - }, |
119 | | - }, |
120 | | - }; |
121 | | - }, |
122 | | - }; |
| 106 | + export default fromWebHandler(handler.fetch)` |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + const nitro = await createNitro(resolvedNitroConfig); |
| 111 | + |
| 112 | + await buildNitroEnvironment(nitro, () => build(nitro)); |
| 113 | + } |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + }; |
123 | 118 | } |
124 | 119 |
|
125 | | -export async function buildNitroEnvironment( |
126 | | - nitro: Nitro, |
127 | | - build: () => Promise<any>, |
128 | | -) { |
129 | | - await prepare(nitro); |
130 | | - await copyPublicAssets(nitro); |
131 | | - await prerender(nitro); |
132 | | - await build(); |
133 | | - |
134 | | - const publicDir = nitro.options.output.publicDir; |
135 | | - |
136 | | - // As a part of the build process, the `.vite/` directory |
137 | | - // is copied over from `node_modules/.tanstack-start/client-dist/` |
138 | | - // to the `publicDir` (e.g. `.output/public/`). |
139 | | - // This directory (containing the vite manifest) should not be |
140 | | - // included in the final build, so we remove it here. |
141 | | - const viteDir = path.resolve(publicDir, ".vite"); |
142 | | - if (await fsp.stat(viteDir).catch(() => false)) { |
143 | | - await fsp.rm(viteDir, { recursive: true, force: true }); |
144 | | - } |
145 | | - |
146 | | - await nitro.close(); |
| 120 | +export async function buildNitroEnvironment(nitro: Nitro, build: () => Promise<any>) { |
| 121 | + await prepare(nitro); |
| 122 | + await copyPublicAssets(nitro); |
| 123 | + await prerender(nitro); |
| 124 | + await build(); |
| 125 | + |
| 126 | + const publicDir = nitro.options.output.publicDir; |
| 127 | + |
| 128 | + // As a part of the build process, the `.vite/` directory |
| 129 | + // is copied over from `node_modules/.tanstack-start/client-dist/` |
| 130 | + // to the `publicDir` (e.g. `.output/public/`). |
| 131 | + // This directory (containing the vite manifest) should not be |
| 132 | + // included in the final build, so we remove it here. |
| 133 | + const viteDir = path.resolve(publicDir, ".vite"); |
| 134 | + if (await fsp.stat(viteDir).catch(() => false)) { |
| 135 | + await fsp.rm(viteDir, { recursive: true, force: true }); |
| 136 | + } |
| 137 | + |
| 138 | + await nitro.close(); |
147 | 139 | } |
148 | 140 |
|
149 | 141 | function virtualBundlePlugin(ssrBundle: Rollup.OutputBundle): PluginOption { |
150 | | - type VirtualModule = { code: string; map: string | null }; |
151 | | - const _modules = new Map<string, VirtualModule>(); |
152 | | - |
153 | | - // group chunks and source maps |
154 | | - for (const [fileName, content] of Object.entries(ssrBundle)) { |
155 | | - if (content.type === "chunk") { |
156 | | - const virtualModule: VirtualModule = { |
157 | | - code: content.code, |
158 | | - map: null, |
159 | | - }; |
160 | | - const maybeMap = ssrBundle[`${fileName}.map`]; |
161 | | - if (maybeMap && maybeMap.type === "asset") { |
162 | | - virtualModule.map = maybeMap.source as string; |
163 | | - } |
164 | | - _modules.set(fileName, virtualModule); |
165 | | - _modules.set(resolve(fileName), virtualModule); |
166 | | - } |
167 | | - } |
168 | | - |
169 | | - return { |
170 | | - name: "virtual-bundle", |
171 | | - resolveId(id, importer) { |
172 | | - if (_modules.has(id)) { |
173 | | - return resolve(id); |
174 | | - } |
175 | | - |
176 | | - if (importer) { |
177 | | - const resolved = resolve(dirname(importer), id); |
178 | | - if (_modules.has(resolved)) { |
179 | | - return resolved; |
180 | | - } |
181 | | - } |
182 | | - return null; |
183 | | - }, |
184 | | - load(id) { |
185 | | - const m = _modules.get(id); |
186 | | - if (!m) { |
187 | | - return null; |
188 | | - } |
189 | | - return m; |
190 | | - }, |
191 | | - }; |
| 142 | + type VirtualModule = { code: string; map: string | null }; |
| 143 | + const _modules = new Map<string, VirtualModule>(); |
| 144 | + |
| 145 | + // group chunks and source maps |
| 146 | + for (const [fileName, content] of Object.entries(ssrBundle)) { |
| 147 | + if (content.type === "chunk") { |
| 148 | + const virtualModule: VirtualModule = { |
| 149 | + code: content.code, |
| 150 | + map: null |
| 151 | + }; |
| 152 | + const maybeMap = ssrBundle[`${fileName}.map`]; |
| 153 | + if (maybeMap && maybeMap.type === "asset") { |
| 154 | + virtualModule.map = maybeMap.source as string; |
| 155 | + } |
| 156 | + _modules.set(fileName, virtualModule); |
| 157 | + _modules.set(resolve(fileName), virtualModule); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return { |
| 162 | + name: "virtual-bundle", |
| 163 | + resolveId(id, importer) { |
| 164 | + if (_modules.has(id)) { |
| 165 | + return resolve(id); |
| 166 | + } |
| 167 | + |
| 168 | + if (importer) { |
| 169 | + const resolved = resolve(dirname(importer), id); |
| 170 | + if (_modules.has(resolved)) { |
| 171 | + return resolved; |
| 172 | + } |
| 173 | + } |
| 174 | + return null; |
| 175 | + }, |
| 176 | + load(id) { |
| 177 | + const m = _modules.get(id); |
| 178 | + if (!m) { |
| 179 | + return null; |
| 180 | + } |
| 181 | + return m; |
| 182 | + } |
| 183 | + }; |
192 | 184 | } |
0 commit comments