Skip to content

test(rsc): test stable client chunks #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion packages/plugin-rsc/examples/basic/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import assert from 'node:assert'
import rsc, { transformHoistInlineDirective } from '@vitejs/plugin-rsc'
import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
import { type Plugin, defineConfig, normalizePath, parseAstAsync } from 'vite'
import {
type Plugin,
Rollup,
defineConfig,
normalizePath,
parseAstAsync,
} from 'vite'
import inspect from 'vite-plugin-inspect'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

// log unhandled rejection to debug e2e failures
if (!(globalThis as any).__debugHandlerRegisterd) {
Expand Down Expand Up @@ -104,6 +111,76 @@ export default defineConfig({
}
},
},
{
name: 'optimize-chunks',
apply: 'build',
config() {
const resolvePackageSource = (source: string) =>
normalizePath(fileURLToPath(import.meta.resolve(source)))

// TODO: this entrypoint shouldn't be a public API.
const pkgBrowserPath = resolvePackageSource(
'@vitejs/plugin-rsc/react/browser',
)

return {
environments: {
client: {
build: {
rollupOptions: {
output: {
manualChunks: (id) => {
// need to use functional form to handle commonjs plugin proxy module
// e.g. `(id)?commonjs-es-import`
if (
id.includes('node_modules/react/') ||
id.includes('node_modules/react-dom/') ||
id.includes(pkgBrowserPath)
) {
return 'lib-react'
}
if (id === '\0vite/preload-helper.js') {
return 'lib-vite'
}
},
},
},
},
},
},
}
},
// verify chunks are "stable"
writeBundle(_options, bundle) {
if (this.environment.name === 'client') {
const entryChunks: Rollup.OutputChunk[] = []
const vendorChunks: Rollup.OutputChunk[] = []
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk') {
if (chunk.facadeModuleId?.endsWith('/src/client.tsx')) {
entryChunks.push(chunk)
} else if (chunk.name === 'lib-react') {
vendorChunks.push(chunk)
}
}
}

// react vendor chunk has no import
assert.equal(vendorChunks.length, 1)
assert.deepEqual(
vendorChunks[0].imports.filter(
(f) => !f.includes('rolldown-runtime'),
),
[],
)
assert.deepEqual(vendorChunks[0].dynamicImports, [])

// entry chunk has no export
assert.equal(entryChunks.length, 1)
assert.deepEqual(entryChunks[0].exports, [])
}
},
},
{
name: 'cf-build',
enforce: 'post',
Expand Down
Loading