Skip to content

Commit 436a330

Browse files
committed
test: test stable react vendor chunk
1 parent 0b09907 commit 436a330

1 file changed

Lines changed: 77 additions & 1 deletion

File tree

packages/rsc/examples/basic/vite.config.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import assert from "node:assert";
2+
import { fileURLToPath } from "node:url";
23
import { transformHoistInlineDirective } from "@hiogawa/transforms";
34
import rsc from "@hiogawa/vite-rsc/plugin";
45
import tailwindcss from "@tailwindcss/vite";
56
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";
714
import inspect from "vite-plugin-inspect";
815

916
// log unhandled rejection to debug e2e failures
@@ -126,6 +133,75 @@ export default { fetch: handler };
126133
}
127134
},
128135
},
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+
},
129205
],
130206
build: {
131207
minify: false,

0 commit comments

Comments
 (0)