@@ -2,7 +2,7 @@ import path from 'path'
2
2
import { parse as parseUrl } from 'url'
3
3
import fs , { promises as fsp } from 'fs'
4
4
import * as mrmime from 'mrmime'
5
- import type { OutputOptions , PluginContext } from 'rollup'
5
+ import type { OutputOptions , PluginContext , PreRenderedAsset } from 'rollup'
6
6
import MagicString from 'magic-string'
7
7
import type { Plugin } from '../plugin'
8
8
import type { ResolvedConfig } from '../config'
@@ -217,6 +217,27 @@ export function getAssetFilename(
217
217
return assetHashToFilenameMap . get ( config ) ?. get ( hash )
218
218
}
219
219
220
+ export function resolveAssetFileNames (
221
+ config : ResolvedConfig
222
+ ) : string | ( ( chunkInfo : PreRenderedAsset ) => string ) {
223
+ const output = config . build ?. rollupOptions ?. output
224
+ const defaultAssetFileNames = path . posix . join (
225
+ config . build . assetsDir ,
226
+ '[name].[hash][extname]'
227
+ )
228
+ // Steps to determine which assetFileNames will be actually used.
229
+ // First, if output is an object or string, use assetFileNames in it.
230
+ // And a default assetFileNames as fallback.
231
+ let assetFileNames : Exclude < OutputOptions [ 'assetFileNames' ] , undefined > =
232
+ ( output && ! Array . isArray ( output ) ? output . assetFileNames : undefined ) ??
233
+ defaultAssetFileNames
234
+ if ( output && Array . isArray ( output ) ) {
235
+ // Second, if output is an array, adopt assetFileNames in the first object.
236
+ assetFileNames = output [ 0 ] . assetFileNames ?? assetFileNames
237
+ }
238
+ return assetFileNames
239
+ }
240
+
220
241
/**
221
242
* converts the source filepath of the asset to the output filename based on the assetFileNames option. \
222
243
* this function imitates the behavior of rollup.js. \
@@ -364,25 +385,9 @@ async function fileToBuiltUrl(
364
385
const contentHash = getHash ( content )
365
386
const { search, hash } = parseUrl ( id )
366
387
const postfix = ( search || '' ) + ( hash || '' )
367
- const output = config . build ?. rollupOptions ?. output
368
-
369
- const defaultAssetFileNames = path . posix . join (
370
- config . build . assetsDir ,
371
- '[name].[hash][extname]'
372
- )
373
- // Steps to determine which assetFileNames will be actually used.
374
- // First, if output is an object or string, use assetFileNames in it.
375
- // And a default assetFileNames as fallback.
376
- let assetFileNames : Exclude < OutputOptions [ 'assetFileNames' ] , undefined > =
377
- ( output && ! Array . isArray ( output ) ? output . assetFileNames : undefined ) ??
378
- defaultAssetFileNames
379
- if ( output && Array . isArray ( output ) ) {
380
- // Second, if output is an array, adopt assetFileNames in the first object.
381
- assetFileNames = output [ 0 ] . assetFileNames ?? assetFileNames
382
- }
383
388
384
389
const fileName = assetFileNamesToFileName (
385
- assetFileNames ,
390
+ resolveAssetFileNames ( config ) ,
386
391
file ,
387
392
contentHash ,
388
393
content
0 commit comments