Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build): respect rollup output.assetFileNames, fix #2944 #4352

Merged
merged 9 commits into from
Aug 2, 2021
80 changes: 73 additions & 7 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Plugin } from '../plugin'
import { ResolvedConfig } from '../config'
import { cleanUrl } from '../utils'
import { FS_PREFIX } from '../constants'
import { PluginContext, RenderedChunk } from 'rollup'
import { PluginContext, RenderedChunk, OutputOptions } from 'rollup'
import MagicString from 'magic-string'
import { createHash } from 'crypto'

Expand Down Expand Up @@ -228,12 +228,78 @@ async function fileToBuiltUrl(
const contentHash = getAssetHash(content)
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')
const basename = path.basename(file)
const ext = path.extname(basename)
const fileName = path.posix.join(
config.build.assetsDir,
`${basename.slice(0, -ext.length)}.${contentHash}${ext}`
)

// create fileName using rollupOptions.output.assetFileNames
let fileName: string
{
const basename = path.basename(file)

// placeholders for `assetFileNames`
// `hash` is slightly different from the rollup's one
const _extname = path.extname(basename)
const _ext = _extname.substr(1)
const _name = basename.slice(0, -_extname.length)
const _hash = contentHash

let assetFileNames: OutputOptions['assetFileNames']
const output = config.build?.rollupOptions?.output
// only the object format is currently considered here
if (output && !Array.isArray(output)) {
assetFileNames = output.assetFileNames
}
// defaults to '<assetsDir>/[name].[hash][extname]'
// slightly different from rollup's one ('assets/[name]-[hash][extname]')
if (assetFileNames == null) {
assetFileNames = path.posix.join(
config.build.assetsDir,
'[name].[hash][extname]'
)
}

switch (typeof assetFileNames) {
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
// e.g. assetFileNames: 'dir/[name].[ext]'
case 'string':
// see https://rollupjs.org/guide/en/#outputassetfilenames for available placeholders
fileName = assetFileNames.replace(
/\[\w+\]/g,
(placeholder: string): string => {
switch (placeholder) {
case '[ext]':
return _ext

case '[extname]':
return _extname

case '[hash]':
return _hash

case '[name]':
return _name
}
throw new Error(
`invalid placeholder ${placeholder} in assetFileNames "${assetFileNames}"`
)
}
)
break

// e.g. assetFileNames: () => 'name'
case 'function':
fileName = assetFileNames({
name: file,
source: content,
type: 'asset'
})
if (typeof fileName !== 'string') {
throw new TypeError('assetFileNames must return a string')
}
break

default:
throw new TypeError('assetFileNames must be a string or a function')
}
}

if (!map.has(contentHash)) {
map.set(contentHash, fileName)
}
Expand Down