@@ -2,9 +2,16 @@ import assert from 'node:assert'
2
2
import rsc , { transformHoistInlineDirective } from '@vitejs/plugin-rsc'
3
3
import tailwindcss from '@tailwindcss/vite'
4
4
import react from '@vitejs/plugin-react'
5
- import { type Plugin , defineConfig , normalizePath , parseAstAsync } from 'vite'
5
+ import {
6
+ type Plugin ,
7
+ Rollup ,
8
+ defineConfig ,
9
+ normalizePath ,
10
+ parseAstAsync ,
11
+ } from 'vite'
6
12
import inspect from 'vite-plugin-inspect'
7
13
import path from 'node:path'
14
+ import { fileURLToPath } from 'node:url'
8
15
9
16
// log unhandled rejection to debug e2e failures
10
17
if ( ! ( globalThis as any ) . __debugHandlerRegisterd ) {
@@ -114,6 +121,76 @@ export default defineConfig({
114
121
}
115
122
} ,
116
123
} ,
124
+ {
125
+ name : 'optimize-chunks' ,
126
+ apply : 'build' ,
127
+ config ( ) {
128
+ const resolvePackageSource = ( source : string ) =>
129
+ normalizePath ( fileURLToPath ( import . meta. resolve ( source ) ) )
130
+
131
+ // TODO: this entrypoint shouldn't be a public API.
132
+ const pkgBrowserPath = resolvePackageSource (
133
+ '@vitejs/plugin-rsc/react/browser' ,
134
+ )
135
+
136
+ return {
137
+ environments : {
138
+ client : {
139
+ build : {
140
+ rollupOptions : {
141
+ output : {
142
+ manualChunks : ( id ) => {
143
+ // need to use functional form to handle commonjs plugin proxy module
144
+ // e.g. `(id)?commonjs-es-import`
145
+ if (
146
+ id . includes ( 'node_modules/react/' ) ||
147
+ id . includes ( 'node_modules/react-dom/' ) ||
148
+ id . includes ( pkgBrowserPath )
149
+ ) {
150
+ return 'lib-react'
151
+ }
152
+ if ( id === '\0vite/preload-helper.js' ) {
153
+ return 'lib-vite'
154
+ }
155
+ } ,
156
+ } ,
157
+ } ,
158
+ } ,
159
+ } ,
160
+ } ,
161
+ }
162
+ } ,
163
+ // verify chunks are "stable"
164
+ writeBundle ( _options , bundle ) {
165
+ if ( this . environment . name === 'client' ) {
166
+ const entryChunks : Rollup . OutputChunk [ ] = [ ]
167
+ const vendorChunks : Rollup . OutputChunk [ ] = [ ]
168
+ for ( const chunk of Object . values ( bundle ) ) {
169
+ if ( chunk . type === 'chunk' ) {
170
+ if ( chunk . facadeModuleId ?. endsWith ( '/src/client.tsx' ) ) {
171
+ entryChunks . push ( chunk )
172
+ } else if ( chunk . name === 'lib-react' ) {
173
+ vendorChunks . push ( chunk )
174
+ }
175
+ }
176
+ }
177
+
178
+ // react vendor chunk has no import
179
+ assert . equal ( vendorChunks . length , 1 )
180
+ assert . deepEqual (
181
+ vendorChunks [ 0 ] . imports . filter (
182
+ ( f ) => ! f . includes ( 'rolldown-runtime' ) ,
183
+ ) ,
184
+ [ ] ,
185
+ )
186
+ assert . deepEqual ( vendorChunks [ 0 ] . dynamicImports , [ ] )
187
+
188
+ // entry chunk has no export
189
+ assert . equal ( entryChunks . length , 1 )
190
+ assert . deepEqual ( entryChunks [ 0 ] . exports , [ ] )
191
+ }
192
+ } ,
193
+ } ,
117
194
{
118
195
name : 'cf-build' ,
119
196
enforce : 'post' ,
0 commit comments