11import type { Rollup } from 'vite' ;
2-
32import type {
43 Diagnostic ,
54 EntryStrategy ,
@@ -18,6 +17,7 @@ import {
1817 type QwikPlugin ,
1918 type QwikPluginOptions ,
2019} from './plugin' ;
20+ import { findDepPkgJsonPath } from './utils' ;
2121
2222type QwikRollupPluginApi = {
2323 getOptimizer : ( ) => Optimizer ;
@@ -76,7 +76,7 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
7676 } ,
7777
7878 outputOptions ( rollupOutputOpts ) {
79- return normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , false ) ;
79+ return normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , false ) as any ;
8080 } ,
8181
8282 async buildStart ( ) {
@@ -127,35 +127,37 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any {
127127 return rollupPlugin ;
128128}
129129
130- export function normalizeRollupOutputOptions (
130+ export async function normalizeRollupOutputOptions (
131131 qwikPlugin : QwikPlugin ,
132132 rollupOutputOpts : Rollup . OutputOptions | Rollup . OutputOptions [ ] | undefined ,
133133 useAssetsDir : boolean ,
134134 outDir ?: string
135- ) : Rollup . OutputOptions | Rollup . OutputOptions [ ] {
135+ ) : Promise < Rollup . OutputOptions | Rollup . OutputOptions [ ] > {
136136 if ( Array . isArray ( rollupOutputOpts ) ) {
137137 // make sure at least one output is present in every case
138138 if ( ! rollupOutputOpts . length ) {
139139 rollupOutputOpts . push ( { } ) ;
140140 }
141141
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+ ) ;
146148 }
147149
148150 return {
149- ...normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , useAssetsDir ) ,
151+ ...( await normalizeRollupOutputOptionsObject ( qwikPlugin , rollupOutputOpts , useAssetsDir ) ) ,
150152 dir : outDir || rollupOutputOpts ?. dir ,
151153 } ;
152154}
153155
154- export function normalizeRollupOutputOptionsObject (
156+ export async function normalizeRollupOutputOptionsObject (
155157 qwikPlugin : QwikPlugin ,
156158 rollupOutputOptsObj : Rollup . OutputOptions | undefined ,
157159 useAssetsDir : boolean
158- ) : Rollup . OutputOptions {
160+ ) : Promise < Rollup . OutputOptions > {
159161 const outputOpts : Rollup . OutputOptions = { ...rollupOutputOptsObj } ;
160162 const opts = qwikPlugin . getOptions ( ) ;
161163 const optimizer = qwikPlugin . getOptimizer ( ) ;
@@ -253,6 +255,34 @@ export function normalizeRollupOutputOptionsObject(
253255 */
254256 outputOpts . hoistTransitiveImports = false ;
255257
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+
256286 return outputOpts ;
257287}
258288
0 commit comments