@@ -205,11 +205,29 @@ function compression<T extends UserCompressionOptions, A extends Algorithm>(
205
205
staticOutputs : new Set ( )
206
206
}
207
207
208
- return {
208
+ let viteAnalyzerPlugin = null
209
+
210
+ const plugin = < Plugin > {
209
211
name : VITE_COMPRESSION_PLUGIN ,
210
212
apply : 'build' ,
211
213
enforce : 'post' ,
212
214
api : pluginContext ,
215
+ options ( ) {
216
+ const { rollupVersion } = this . meta
217
+ // issue #63
218
+ // more and more plugin use are starting specify plugin order. So we should do a check for vite's version.
219
+ const [ major , minor ] = rollupVersion . split ( '.' )
220
+ // rollup support object hook at 2.78.0
221
+ if ( + major < 2 && + minor < 78 ) {
222
+ hijackGenerateBundle ( viteAnalyzerPlugin , generateBundle )
223
+ return
224
+ }
225
+
226
+ plugin . generateBundle = {
227
+ order : 'post' ,
228
+ handler : generateBundle
229
+ }
230
+ } ,
213
231
async configResolved ( config ) {
214
232
// hijack vite's internal `vite:build-import-analysis` plugin.So we won't need process us chunks at closeBundle anymore.
215
233
// issue #26
@@ -223,9 +241,8 @@ function compression<T extends UserCompressionOptions, A extends Algorithm>(
223
241
await handleStaticFiles ( config , async ( file ) => {
224
242
statics . push ( file )
225
243
} )
226
- const plugin = config . plugins . find ( p => p . name === VITE_INTERNAL_ANALYSIS_PLUGIN )
227
- if ( ! plugin ) throw new Error ( "[vite-plugin-compression] Can't be work in versions lower than vite at 2.0.0" )
228
- hijackGenerateBundle ( plugin , generateBundle )
244
+ viteAnalyzerPlugin = config . plugins . find ( p => p . name === VITE_INTERNAL_ANALYSIS_PLUGIN )
245
+ if ( ! viteAnalyzerPlugin ) throw new Error ( "[vite-plugin-compression] Can't be work in versions lower than vite at 2.0.0" )
229
246
} ,
230
247
async closeBundle ( ) {
231
248
// parallel run
@@ -260,6 +277,8 @@ function compression<T extends UserCompressionOptions, A extends Algorithm>(
260
277
await queue . wait ( ) . catch ( e => e )
261
278
}
262
279
}
280
+
281
+ return plugin
263
282
}
264
283
265
284
compression . getPluginAPI = ( plugins : readonly Plugin [ ] ) : CompressionPluginAPI | undefined =>
0 commit comments