@@ -99,7 +99,7 @@ function viteLegacyPlugin(options = {}) {
99
99
} ,
100
100
101
101
async generateBundle ( opts , bundle ) {
102
- if ( ! isLegacyOutput ( opts ) ) {
102
+ if ( ! isLegacyBundle ( bundle , opts ) ) {
103
103
if ( ! modernPolyfills . size ) {
104
104
return
105
105
}
@@ -168,8 +168,9 @@ function viteLegacyPlugin(options = {}) {
168
168
}
169
169
170
170
/**
171
- * @param {string| ((chunkInfo: import('rollup').PreRenderedChunk)=> string) } fileNames
171
+ * @param {string | ((chunkInfo: import('rollup').PreRenderedChunk) => string) } fileNames
172
172
* @param {string? } defaultFileName
173
+ * @returns {(chunkInfo: import('rollup').PreRenderedChunk) => string) }
173
174
*/
174
175
const getLegacyOutputFileName = (
175
176
fileNames ,
@@ -179,21 +180,20 @@ function viteLegacyPlugin(options = {}) {
179
180
return path . posix . join ( config . build . assetsDir , defaultFileName )
180
181
}
181
182
182
- // does not support custom functions.
183
- if ( typeof fileNames === 'function' ) {
184
- throw new Error (
185
- `@vitejs/plugin-legacy rollupOptions.output.entryFileNames and rollupOptions.output.chunkFileNames` +
186
- ` does not support the function format.`
187
- )
188
- }
183
+ return ( chunkInfo ) => {
184
+ let fileName =
185
+ typeof fileNames === 'function' ? fileNames ( chunkInfo ) : fileNames
189
186
190
- let fileName = defaultFileName
191
- // Custom string file return format.
192
- if ( fileNames && typeof fileNames === 'string' ) {
193
- fileName = fileNames . replace ( / \[ n a m e \] / , '[name]-legacy' )
194
- }
187
+ if ( fileName . includes ( '[name]' ) ) {
188
+ // [name]-[hash].[format] -> [name]-legacy-[hash].[format]
189
+ fileName = fileName . replace ( '[name]' , '[name]-legacy' )
190
+ } else {
191
+ // entry.js -> entry-legacy.js
192
+ fileName = fileName . replace ( / ( .+ ) \. ( .+ ) / , '$1-legacy.$2' )
193
+ }
195
194
196
- return fileName
195
+ return fileName
196
+ }
197
197
}
198
198
199
199
/**
@@ -219,7 +219,7 @@ function viteLegacyPlugin(options = {}) {
219
219
} ,
220
220
221
221
renderChunk ( raw , chunk , opts ) {
222
- if ( ! isLegacyOutput ( opts ) ) {
222
+ if ( ! isLegacyChunk ( chunk , opts ) ) {
223
223
if (
224
224
options . modernPolyfills &&
225
225
! Array . isArray ( options . modernPolyfills )
@@ -399,7 +399,7 @@ function viteLegacyPlugin(options = {}) {
399
399
} ,
400
400
401
401
generateBundle ( opts , bundle ) {
402
- if ( isLegacyOutput ( opts ) ) {
402
+ if ( isLegacyBundle ( bundle , opts ) ) {
403
403
// avoid emitting duplicate assets
404
404
for ( const name in bundle ) {
405
405
if ( bundle [ name ] . type === 'asset' ) {
@@ -564,14 +564,27 @@ function polyfillsPlugin(imports) {
564
564
}
565
565
566
566
/**
567
+ * @param {import('rollup').RenderedChunk } chunk
567
568
* @param {import('rollup').NormalizedOutputOptions } options
568
569
*/
569
- function isLegacyOutput ( options ) {
570
- return (
571
- options . format === 'system' &&
572
- typeof options . entryFileNames === 'string' &&
573
- options . entryFileNames . includes ( '-legacy' )
574
- )
570
+ function isLegacyChunk ( chunk , options ) {
571
+ return options . format === 'system' && chunk . fileName . includes ( '-legacy' )
572
+ }
573
+
574
+ /**
575
+ * @param {import('rollup').OutputBundle } bundle
576
+ * @param {import('rollup').NormalizedOutputOptions } options
577
+ */
578
+ function isLegacyBundle ( bundle , options ) {
579
+ if ( options . format === 'system' ) {
580
+ const entryChunk = Object . values ( bundle ) . find (
581
+ ( output ) => output . type === 'chunk' && output . isEntry
582
+ )
583
+
584
+ return ! ! entryChunk && entryChunk . fileName . includes ( '-legacy' )
585
+ }
586
+
587
+ return false
575
588
}
576
589
577
590
/**
0 commit comments