Skip to content

Commit 4e6c26f

Browse files
inkyMountainpatak-dev
andauthoredJun 15, 2022
fix(asset): respect assetFileNames if rollupOptions.output is an array (#8561)
Co-authored-by: patak-dev <matias.capeletto@gmail.com>
1 parent 5151e74 commit 4e6c26f

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed
 

‎packages/vite/src/node/config.ts

+23
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,29 @@ export async function resolveConfig(
569569
)
570570
}
571571

572+
// Check if all assetFileNames have the same reference.
573+
// If not, display a warn for user.
574+
const outputOption = config.build?.rollupOptions?.output ?? []
575+
// Use isArray to narrow its type to array
576+
if (Array.isArray(outputOption)) {
577+
const assetFileNamesList = outputOption.map(
578+
(output) => output.assetFileNames
579+
)
580+
if (assetFileNamesList.length > 1) {
581+
const firstAssetFileNames = assetFileNamesList[0]
582+
const hasDifferentReference = assetFileNamesList.some(
583+
(assetFileNames) => assetFileNames !== firstAssetFileNames
584+
)
585+
if (hasDifferentReference) {
586+
resolved.logger.warn(
587+
colors.yellow(`
588+
assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
589+
`)
590+
)
591+
}
592+
}
593+
}
594+
572595
return resolved
573596
}
574597

‎packages/vite/src/node/plugins/asset.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,22 @@ async function fileToBuiltUrl(
365365
const { search, hash } = parseUrl(id)
366366
const postfix = (search || '') + (hash || '')
367367
const output = config.build?.rollupOptions?.output
368-
const assetFileNames =
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> =
369377
(output && !Array.isArray(output) ? output.assetFileNames : undefined) ??
370-
// defaults to '<assetsDir>/[name].[hash][extname]'
371-
// slightly different from rollup's one ('assets/[name]-[hash][extname]')
372-
path.posix.join(config.build.assetsDir, '[name].[hash][extname]')
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+
373384
const fileName = assetFileNamesToFileName(
374385
assetFileNames,
375386
file,

‎playground/legacy/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import './style.css'
2+
import './vite.svg'
23

34
async function run() {
45
const { fn } = await import('./async.js')

‎playground/legacy/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build": "vite build --debug legacy",
88
"build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy",
99
"build:dynamic-css": "vite --config ./vite.config-dynamic-css.js build --debug legacy",
10+
"build:multiple-output": "vite --config ./vite.config-multiple-output.js build",
1011
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
1112
"preview": "vite preview"
1213
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import legacy from '@vitejs/plugin-legacy'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
plugins: [legacy({ modernPolyfills: true })],
6+
build: {
7+
manifest: true,
8+
minify: false,
9+
rollupOptions: {
10+
output: [
11+
{
12+
assetFileNames() {
13+
return 'assets/subdir/[name].[hash][extname]'
14+
},
15+
entryFileNames: `assets/subdir/[name].js`,
16+
chunkFileNames: `assets/subdir/[name].js`
17+
},
18+
{
19+
assetFileNames() {
20+
return 'assets/subdir/[name].[hash][extname]'
21+
},
22+
entryFileNames: `assets/anotherSubdir/[name].js`,
23+
chunkFileNames: `assets/anotherSubdir/[name].js`
24+
}
25+
]
26+
}
27+
}
28+
})

‎playground/legacy/vite.svg

+1
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.