1
1
import type { Rollup } from 'vite' ;
2
-
3
2
import type {
4
3
Diagnostic ,
5
4
EntryStrategy ,
@@ -18,6 +17,7 @@ import {
18
17
type QwikPlugin ,
19
18
type QwikPluginOptions ,
20
19
} from './plugin' ;
20
+ import { findDepPkgJsonPath } from './utils' ;
21
21
22
22
type QwikRollupPluginApi = {
23
23
getOptimizer : ( ) => Optimizer ;
@@ -76,7 +76,7 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
76
76
} ,
77
77
78
78
outputOptions ( rollupOutputOpts ) {
79
- return normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , false ) ;
79
+ return normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , false ) as any ;
80
80
} ,
81
81
82
82
async buildStart ( ) {
@@ -127,35 +127,37 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
127
127
return rollupPlugin ;
128
128
}
129
129
130
- export function normalizeRollupOutputOptions (
130
+ export async function normalizeRollupOutputOptions (
131
131
qwikPlugin : QwikPlugin ,
132
132
rollupOutputOpts : Rollup . OutputOptions | Rollup . OutputOptions [ ] | undefined ,
133
133
useAssetsDir : boolean ,
134
134
outDir ?: string
135
- ) : Rollup . OutputOptions | Rollup . OutputOptions [ ] {
135
+ ) : Promise < Rollup . OutputOptions | Rollup . OutputOptions [ ] > {
136
136
if ( Array . isArray ( rollupOutputOpts ) ) {
137
137
// make sure at least one output is present in every case
138
138
if ( ! rollupOutputOpts . length ) {
139
139
rollupOutputOpts . push ( { } ) ;
140
140
}
141
141
142
- return rollupOutputOpts . map ( ( outputOptsObj ) => ( {
143
- ...normalizeRollupOutputOptionsObject ( qwikPlugin , outputOptsObj , useAssetsDir ) ,
144
- dir : outDir || outputOptsObj . dir ,
145
- } ) ) ;
142
+ return await Promise . all (
143
+ rollupOutputOpts . map ( async ( outputOptsObj ) => ( {
144
+ ...( await normalizeRollupOutputOptionsObject ( qwikPlugin , outputOptsObj , useAssetsDir ) ) ,
145
+ dir : outDir || outputOptsObj . dir ,
146
+ } ) )
147
+ ) ;
146
148
}
147
149
148
150
return {
149
- ...normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , useAssetsDir ) ,
151
+ ...( await normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , useAssetsDir ) ) ,
150
152
dir : outDir || rollupOutputOpts ?. dir ,
151
153
} ;
152
154
}
153
155
154
- export function normalizeRollupOutputOptionsObject (
156
+ export async function normalizeRollupOutputOptionsObject (
155
157
qwikPlugin : QwikPlugin ,
156
158
rollupOutputOptsObj : Rollup . OutputOptions | undefined ,
157
159
useAssetsDir : boolean
158
- ) : Rollup . OutputOptions {
160
+ ) : Promise < Rollup . OutputOptions > {
159
161
const outputOpts : Rollup . OutputOptions = { ...rollupOutputOptsObj } ;
160
162
const opts = qwikPlugin . getOptions ( ) ;
161
163
const optimizer = qwikPlugin . getOptimizer ( ) ;
@@ -253,6 +255,34 @@ export function normalizeRollupOutputOptionsObject(
253
255
*/
254
256
outputOpts . hoistTransitiveImports = false ;
255
257
258
+ // V2 official release TODO: remove below checks and just keep `outputOpts.onlyExplicitManualChunks = true;`
259
+ const userPkgJsonPath = await findDepPkgJsonPath ( optimizer . sys , 'rollup' , optimizer . sys . cwd ( ) ) ;
260
+ if ( userPkgJsonPath ) {
261
+ try {
262
+ const fs : typeof import ( 'fs' ) = await optimizer . sys . dynamicImport ( 'node:fs' ) ;
263
+ const pkgJsonStr = await fs . promises . readFile ( userPkgJsonPath , 'utf-8' ) ;
264
+ const pkgJson = JSON . parse ( pkgJsonStr ) ;
265
+ const version = String ( pkgJson ?. version || '' ) ;
266
+ const [ major , minor , patch ] = version . split ( '.' ) . map ( ( n : string ) => parseInt ( n , 10 ) ) as [
267
+ number ,
268
+ number ,
269
+ number ,
270
+ ] ;
271
+ const isGte452 =
272
+ Number . isFinite ( major ) &&
273
+ ( major > 4 || ( major === 4 && ( minor > 52 || ( minor === 52 && ( patch || 0 ) >= 0 ) ) ) ) ;
274
+ if ( isGte452 ) {
275
+ outputOpts . onlyExplicitManualChunks = true ;
276
+ } else {
277
+ console . warn (
278
+ `⚠️ We detected that you're using a Rollup version prior to 4.52.0 (${ version } ). For the latest and greatest, we recommend to let Vite install the latest version for you, or manually install the latest version of Rollup in your project if that doesn't work. It will enable the new Rollup \`outputOpts.onlyExplicitManualChunks\` feature flag, which improves preloading performance and reduces cache invalidation for a snappier user experience.`
279
+ ) ;
280
+ }
281
+ } catch {
282
+ // If we cannot determine the installed Rollup version, avoid warning
283
+ }
284
+ }
285
+
256
286
return outputOpts ;
257
287
}
258
288
0 commit comments