Skip to content

Commit

Permalink
fix(asset): respect assetFileNames if rollupOptions.output is an array (
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyitao committed Jun 13, 2022
1 parent c6f43dd commit 0996a1b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
47 changes: 43 additions & 4 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import fs, { promises as fsp } from 'fs'
import * as mrmime from 'mrmime'
import type { OutputOptions, PluginContext } from 'rollup'
import MagicString from 'magic-string'
import colors from 'picocolors'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
import { cleanUrl, getHash, isRelativeBase, normalizePath } from '../utils'
import { FS_PREFIX } from '../constants'
import { createLogger } from '../logger'

export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g

Expand Down Expand Up @@ -353,11 +355,48 @@ async function fileToBuiltUrl(
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')
const output = config.build?.rollupOptions?.output
const assetFileNames =

// Steps to determine which assetFileNames will be actually used.
const defaultAssetFileNames = path.posix.join(
config.build.assetsDir,
'[name].[hash][extname]'
)
// First, if output is an object, use assetFileNames in it.
// And a default assetFileNames as fallback.
let assetFileNames: Exclude<OutputOptions['assetFileNames'], undefined> =
(output && !Array.isArray(output) ? output.assetFileNames : undefined) ??
// defaults to '<assetsDir>/[name].[hash][extname]'
// slightly different from rollup's one ('assets/[name]-[hash][extname]')
path.posix.join(config.build.assetsDir, '[name].[hash][extname]')
defaultAssetFileNames
if (output && Array.isArray(output)) {
// Second, if output is an array, adopt assetFileNames in the first object.
assetFileNames = output[0].assetFileNames ?? assetFileNames
// Third, check if assetFileNames among output array return the same result.
// If not, log a warn and keep using the first assetFileNames.
const filenameSet = output.reduce(
(cumulate, { assetFileNames = defaultAssetFileNames }) => {
const filename =
typeof assetFileNames === 'string'
? assetFileNames
: assetFileNames({
type: 'asset',
name: file,
source: content
})
cumulate.add(filename)
return cumulate
},
new Set<string>()
)
if (filenameSet.size > 1) {
createLogger('warn').warn(
colors.yellow(`
Found that you've configure multiple assetFileNames in build.rollupOptions.output,
and their return values are inconsistent. Vite has adopted the first assetFileNames in the array.
If you expecting a different asset file name, change the first assetFileNames in the array.
`)
)
}
}

const fileName = assetFileNamesToFileName(
assetFileNames,
file,
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './style.css'
import './vite.svg'

async function run() {
const { fn } = await import('./async.js')
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "vite build --debug legacy",
"build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy",
"build:dynamic-css": "vite --config ./vite.config-dynamic-css.js build --debug legacy",
"build:multiple-output": "vite --config ./vite.config-multiple-output.js build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
Expand Down
24 changes: 24 additions & 0 deletions playground/legacy/vite.config-multiple-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [legacy({ modernPolyfills: true })],
build: {
manifest: true,
minify: false,
rollupOptions: {
output: [
{
assetFileNames: 'assets/subdir/[name].[hash][extname]',
entryFileNames: `assets/subdir/[name].js`,
chunkFileNames: `assets/subdir/[name].js`
},
{
assetFileNames: 'assets/anotherSubdir/[name].[hash][extname]',
entryFileNames: `assets/anotherSubdir/[name].js`,
chunkFileNames: `assets/anotherSubdir/[name].js`
}
]
}
}
})
1 change: 1 addition & 0 deletions playground/legacy/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0996a1b

Please sign in to comment.