|
1 | 1 | import assert from "node:assert"; |
| 2 | +import { fileURLToPath } from "node:url"; |
2 | 3 | import { transformHoistInlineDirective } from "@hiogawa/transforms"; |
3 | 4 | import rsc from "@hiogawa/vite-rsc/plugin"; |
4 | 5 | import tailwindcss from "@tailwindcss/vite"; |
5 | 6 | import react from "@vitejs/plugin-react"; |
6 | | -import { type Plugin, defineConfig, parseAstAsync } from "vite"; |
| 7 | +import { |
| 8 | + type Plugin, |
| 9 | + type Rollup, |
| 10 | + defineConfig, |
| 11 | + normalizePath, |
| 12 | + parseAstAsync, |
| 13 | +} from "vite"; |
7 | 14 | import inspect from "vite-plugin-inspect"; |
8 | 15 |
|
9 | 16 | // log unhandled rejection to debug e2e failures |
@@ -126,6 +133,75 @@ export default { fetch: handler }; |
126 | 133 | } |
127 | 134 | }, |
128 | 135 | }, |
| 136 | + { |
| 137 | + name: "optimize-chunks", |
| 138 | + apply: "build", |
| 139 | + config() { |
| 140 | + const resolvePackageSource = (source: string) => |
| 141 | + normalizePath(fileURLToPath(import.meta.resolve(source))); |
| 142 | + |
| 143 | + const pkgBrowserPath = resolvePackageSource( |
| 144 | + "@hiogawa/vite-rsc/react/browser", |
| 145 | + ); |
| 146 | + |
| 147 | + const manualChunksFn: Rollup.ManualChunksOption = (id) => { |
| 148 | + // need to use functional form to handle commonjs plugin proxy module |
| 149 | + // e.g. `(id)?commonjs-es-import` |
| 150 | + if ( |
| 151 | + id.includes("node_modules/react/") || |
| 152 | + id.includes("node_modules/react-dom/") || |
| 153 | + id.includes(pkgBrowserPath) |
| 154 | + ) { |
| 155 | + return "lib-react"; |
| 156 | + } |
| 157 | + }; |
| 158 | + |
| 159 | + return { |
| 160 | + environments: { |
| 161 | + client: { |
| 162 | + build: { |
| 163 | + manifest: true, // for debugging |
| 164 | + rollupOptions: { |
| 165 | + output: { |
| 166 | + manualChunks: manualChunksFn, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }; |
| 173 | + }, |
| 174 | + // verify chunks are "stable" |
| 175 | + writeBundle(_options, bundle) { |
| 176 | + if (this.environment.name === "client") { |
| 177 | + const entryChunks: Rollup.OutputChunk[] = []; |
| 178 | + const vendorChunks: Rollup.OutputChunk[] = []; |
| 179 | + for (const chunk of Object.values(bundle)) { |
| 180 | + if (chunk.type === "chunk") { |
| 181 | + if (chunk.facadeModuleId === "\0virtual:vite-rsc/entry-browser") { |
| 182 | + entryChunks.push(chunk); |
| 183 | + } else if (chunk.name === "lib-react") { |
| 184 | + vendorChunks.push(chunk); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // react vendor chunk has no import |
| 190 | + assert(vendorChunks.length === 1); |
| 191 | + assert.deepEqual( |
| 192 | + vendorChunks[0].imports.filter( |
| 193 | + (f) => !f.includes("rolldown-runtime"), |
| 194 | + ), |
| 195 | + [], |
| 196 | + ); |
| 197 | + assert.deepEqual(vendorChunks[0].dynamicImports, []); |
| 198 | + |
| 199 | + // entry chunk has no export |
| 200 | + assert(entryChunks.length === 1); |
| 201 | + assert.deepEqual(entryChunks[0].exports, []); |
| 202 | + } |
| 203 | + }, |
| 204 | + }, |
129 | 205 | ], |
130 | 206 | build: { |
131 | 207 | minify: false, |
|
0 commit comments