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
#8561)

Co-authored-by: patak-dev <matias.capeletto@gmail.com>
  • Loading branch information
inkyMountain and patak-dev committed Jun 15, 2022
1 parent 5151e74 commit 4e6c26f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
23 changes: 23 additions & 0 deletions packages/vite/src/node/config.ts
Expand Up @@ -569,6 +569,29 @@ export async function resolveConfig(
)
}

// Check if all assetFileNames have the same reference.
// If not, display a warn for user.
const outputOption = config.build?.rollupOptions?.output ?? []
// Use isArray to narrow its type to array
if (Array.isArray(outputOption)) {
const assetFileNamesList = outputOption.map(
(output) => output.assetFileNames
)
if (assetFileNamesList.length > 1) {
const firstAssetFileNames = assetFileNamesList[0]
const hasDifferentReference = assetFileNamesList.some(
(assetFileNames) => assetFileNames !== firstAssetFileNames
)
if (hasDifferentReference) {
resolved.logger.warn(
colors.yellow(`
assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
`)
)
}
}
}

return resolved
}

Expand Down
19 changes: 15 additions & 4 deletions packages/vite/src/node/plugins/asset.ts
Expand Up @@ -365,11 +365,22 @@ async function fileToBuiltUrl(
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')
const output = config.build?.rollupOptions?.output
const assetFileNames =

const defaultAssetFileNames = path.posix.join(
config.build.assetsDir,
'[name].[hash][extname]'
)
// Steps to determine which assetFileNames will be actually used.
// First, if output is an object or string, 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
}

const fileName = assetFileNamesToFileName(
assetFileNames,
file,
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/main.js
@@ -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
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
28 changes: 28 additions & 0 deletions playground/legacy/vite.config-multiple-output.js
@@ -0,0 +1,28 @@
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() {
return 'assets/subdir/[name].[hash][extname]'
},
entryFileNames: `assets/subdir/[name].js`,
chunkFileNames: `assets/subdir/[name].js`
},
{
assetFileNames() {
return 'assets/subdir/[name].[hash][extname]'
},
entryFileNames: `assets/anotherSubdir/[name].js`,
chunkFileNames: `assets/anotherSubdir/[name].js`
}
]
}
}
})
1 change: 1 addition & 0 deletions playground/legacy/vite.svg
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 4e6c26f

Please sign in to comment.