Skip to content

Commit cc2adb3

Browse files
authored
feat: align default chunk and asset file names with rollup (#10927)
1 parent 9f54c6a commit cc2adb3

34 files changed

+133
-139
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"typescript": "^4.6.4",
9090
"unbuild": "^0.9.4",
9191
"vite": "workspace:*",
92-
"vitepress": "^1.0.0-alpha.28",
92+
"vitepress": "^1.0.0-alpha.29",
9393
"vitest": "^0.25.1",
9494
"vue": "^3.2.45"
9595
},

packages/plugin-legacy/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
298298
| string
299299
| ((chunkInfo: PreRenderedChunk) => string)
300300
| undefined,
301-
defaultFileName = '[name]-legacy.[hash].js'
301+
defaultFileName = '[name]-legacy-[hash].js'
302302
): string | ((chunkInfo: PreRenderedChunk) => string) => {
303303
if (!fileNames) {
304304
return path.posix.join(config.build.assetsDir, defaultFileName)

packages/vite/src/node/build.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,13 @@ async function doBuild(
576576
: libOptions
577577
? ({ name }) =>
578578
resolveLibFilename(libOptions, format, name, config.root, jsExt)
579-
: path.posix.join(options.assetsDir, `[name].[hash].${jsExt}`),
579+
: path.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
580580
chunkFileNames: libOptions
581-
? `[name].[hash].${jsExt}`
582-
: path.posix.join(options.assetsDir, `[name].[hash].${jsExt}`),
581+
? `[name]-[hash].${jsExt}`
582+
: path.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
583583
assetFileNames: libOptions
584584
? `[name].[ext]`
585-
: path.posix.join(options.assetsDir, `[name].[hash].[ext]`),
585+
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
586586
inlineDynamicImports:
587587
output.format === 'umd' ||
588588
output.format === 'iife' ||

packages/vite/src/node/plugins/worker.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ export async function bundleWorkerEntry(
7979
} = await bundle.generate({
8080
entryFileNames: path.posix.join(
8181
config.build.assetsDir,
82-
'[name].[hash].js'
82+
'[name]-[hash].js'
8383
),
8484
chunkFileNames: path.posix.join(
8585
config.build.assetsDir,
86-
'[name].[hash].js'
86+
'[name]-[hash].js'
8787
),
8888
assetFileNames: path.posix.join(
8989
config.build.assetsDir,
90-
'[name].[hash].[ext]'
90+
'[name]-[hash].[ext]'
9191
),
9292
...workerConfig,
9393
format,

playground/assets/__tests__/assets.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from '~utils'
2020

2121
const assetMatch = isBuild
22-
? /\/foo\/assets\/asset\.\w{8}\.png/
22+
? /\/foo\/assets\/asset-\w{8}\.png/
2323
: '/foo/nested/asset.png'
2424

2525
const iconMatch = `/foo/icon.png`
@@ -209,8 +209,8 @@ describe('image', () => {
209209
srcset.split(', ').forEach((s) => {
210210
expect(s).toMatch(
211211
isBuild
212-
? /\/foo\/assets\/asset\.\w{8}\.png \dx/
213-
: /\/foo\/nested\/asset\.png \dx/
212+
? /\/foo\/assets\/asset-\w{8}\.png \dx/
213+
: /\/foo\/nested\/asset.png \dx/
214214
)
215215
})
216216
})
@@ -338,7 +338,7 @@ describe.runIf(isBuild)('css and assets in css in build watch', () => {
338338
test('css will not be lost and css does not contain undefined', async () => {
339339
editFile('index.html', (code) => code.replace('Assets', 'assets'), true)
340340
await notifyRebuildComplete(watcher)
341-
const cssFile = findAssetFile(/index\.\w+\.css$/, 'foo')
341+
const cssFile = findAssetFile(/index-\w+\.css$/, 'foo')
342342
expect(cssFile).not.toBe('')
343343
expect(cssFile).not.toMatch(/undefined/)
344344
})

playground/assets/__tests__/relative-base/relative-base-assets.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '~utils'
1111

1212
const absoluteAssetMatch = isBuild
13-
? /http.*\/other-assets\/asset\.\w{8}\.png/
13+
? /http.*\/other-assets\/asset-\w{8}\.png/
1414
: '/nested/asset.png'
1515

1616
// Asset URLs in CSS are relative to the same dir, the computed
@@ -20,7 +20,7 @@ const cssBgAssetMatch = absoluteAssetMatch
2020
const iconMatch = `/icon.png`
2121

2222
const absoluteIconMatch = isBuild
23-
? /http.*\/icon\.\w{8}\.png/
23+
? /http.*\/icon-\w{8}\.png/
2424
: '/nested/icon.png'
2525

2626
const absolutePublicIconMatch = isBuild ? /http.*\/icon\.png/ : '/icon.png'
@@ -143,7 +143,7 @@ describe.runIf(isBuild)('index.css URLs', () => {
143143
})
144144

145145
test('relative asset URL', () => {
146-
expect(css).toMatch(`./asset.`)
146+
expect(css).toMatch(`./asset-`)
147147
})
148148

149149
test('preserve postfix query/hash', () => {
@@ -158,7 +158,7 @@ describe('image', () => {
158158
srcset.split(', ').forEach((s) => {
159159
expect(s).toMatch(
160160
isBuild
161-
? /other-assets\/asset\.\w{8}\.png \dx/
161+
? /other-assets\/asset-\w{8}\.png \dx/
162162
: /\.\/nested\/asset\.png \dx/
163163
)
164164
})
@@ -191,14 +191,14 @@ test('?raw import', async () => {
191191

192192
test('?url import', async () => {
193193
expect(await page.textContent('.url')).toMatch(
194-
isBuild ? /http.*\/other-assets\/foo\.\w{8}\.js/ : `/foo.js`
194+
isBuild ? /http.*\/other-assets\/foo-\w{8}\.js/ : `/foo.js`
195195
)
196196
})
197197

198198
test('?url import on css', async () => {
199199
const txt = await page.textContent('.url-css')
200200
expect(txt).toMatch(
201-
isBuild ? /http.*\/other-assets\/icons\.\w{8}\.css/ : '/css/icons.css'
201+
isBuild ? /http.*\/other-assets\/icons-\w{8}\.css/ : '/css/icons.css'
202202
)
203203
})
204204

playground/assets/__tests__/runtime-base/runtime-base-assets.spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '~utils'
1010

1111
const absoluteAssetMatch = isBuild
12-
? /\/other-assets\/asset\.\w{8}\.png/
12+
? /\/other-assets\/asset-\w{8}\.png/
1313
: '/nested/asset.png'
1414

1515
// Asset URLs in CSS are relative to the same dir, the computed
@@ -19,7 +19,7 @@ const cssBgAssetMatch = absoluteAssetMatch
1919
const iconMatch = `/icon.png`
2020

2121
const absoluteIconMatch = isBuild
22-
? /\/other-assets\/icon\.\w{8}\.png/
22+
? /\/other-assets\/icon-\w{8}\.png/
2323
: '/nested/icon.png'
2424

2525
const absolutePublicIconMatch = isBuild ? /\/icon\.png/ : '/icon.png'
@@ -137,11 +137,11 @@ describe('css url() references', () => {
137137
describe.runIf(isBuild)('index.css URLs', () => {
138138
let css: string
139139
beforeAll(() => {
140-
css = findAssetFile(/index.*\.css$/, '', 'other-assets')
140+
css = findAssetFile(/index-\w{8}\.css$/, '', 'other-assets')
141141
})
142142

143143
test('relative asset URL', () => {
144-
expect(css).toMatch(`./asset.`)
144+
expect(css).toMatch(`./asset-`)
145145
})
146146

147147
test('preserve postfix query/hash', () => {
@@ -156,7 +156,7 @@ describe('image', () => {
156156
srcset.split(', ').forEach((s) => {
157157
expect(s).toMatch(
158158
isBuild
159-
? /other-assets\/asset\.\w{8}\.png \dx/
159+
? /other-assets\/asset-\w{8}\.png \dx/
160160
: /\.\/nested\/asset\.png \dx/
161161
)
162162
})
@@ -189,14 +189,14 @@ test('?raw import', async () => {
189189

190190
test('?url import', async () => {
191191
expect(await page.textContent('.url')).toMatch(
192-
isBuild ? /\/other-assets\/foo\.\w{8}\.js/ : `/foo.js`
192+
isBuild ? /\/other-assets\/foo-\w{8}\.js/ : `/foo.js`
193193
)
194194
})
195195

196196
test('?url import on css', async () => {
197197
const txt = await page.textContent('.url-css')
198198
expect(txt).toMatch(
199-
isBuild ? /\/other-assets\/icons\.\w{8}\.css/ : '/css/icons.css'
199+
isBuild ? /\/other-assets\/icons-\w{8}\.css/ : '/css/icons.css'
200200
)
201201
})
202202

playground/assets/vite.config-relative-base.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module.exports = {
1515
rollupOptions: {
1616
output: {
1717
entryFileNames: 'entries/[name].js',
18-
chunkFileNames: 'chunks/[name].[hash].js',
19-
assetFileNames: 'other-assets/[name].[hash][extname]'
18+
chunkFileNames: 'chunks/[name]-[hash].js',
19+
assetFileNames: 'other-assets/[name]-[hash][extname]'
2020
}
2121
}
2222
},

playground/assets/vite.config-runtime-base.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ module.exports = {
2222
rollupOptions: {
2323
output: {
2424
entryFileNames: 'entries/[name].js',
25-
chunkFileNames: 'chunks/[name].[hash].js',
26-
assetFileNames: 'other-assets/[name].[hash][extname]'
25+
chunkFileNames: 'chunks/[name]-[hash].js',
26+
assetFileNames: 'other-assets/[name]-[hash][extname]'
2727
}
2828
}
2929
},

playground/backend-integration/__tests__/backend-integration.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '~utils'
1313

1414
const outerAssetMatch = isBuild
15-
? /\/dev\/assets\/logo\.\w{8}\.png/
15+
? /\/dev\/assets\/logo-\w{8}\.png/
1616
: /\/dev\/@fs\/.+?\/images\/logo\.png/
1717

1818
test('should have no 404s', () => {

playground/css-dynamic-import/__tests__/css-dynamic-import.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,32 @@ baseOptions.forEach(({ base, label }) => {
6666
expect(await getColor('.css-dynamic-import')).toBe('green')
6767
expect(await getLinks()).toEqual([
6868
{
69-
pathname: expect.stringMatching(/^\/assets\/index\..+\.css$/),
69+
pathname: expect.stringMatching(/^\/assets\/index-.+\.css$/),
7070
rel: 'stylesheet',
7171
as: ''
7272
},
7373
{
74-
pathname: expect.stringMatching(/^\/assets\/dynamic\..+\.css$/),
74+
pathname: expect.stringMatching(/^\/assets\/dynamic-.+\.css$/),
7575
rel: 'preload',
7676
as: 'style'
7777
},
7878
{
79-
pathname: expect.stringMatching(/^\/assets\/dynamic\..+\.js$/),
79+
pathname: expect.stringMatching(/^\/assets\/dynamic-.+\.js$/),
8080
rel: 'modulepreload',
8181
as: 'script'
8282
},
8383
{
84-
pathname: expect.stringMatching(/^\/assets\/dynamic\..+\.css$/),
84+
pathname: expect.stringMatching(/^\/assets\/dynamic-.+\.css$/),
8585
rel: 'stylesheet',
8686
as: ''
8787
},
8888
{
89-
pathname: expect.stringMatching(/^\/assets\/static\..+\.js$/),
89+
pathname: expect.stringMatching(/^\/assets\/static-.+\.js$/),
9090
rel: 'modulepreload',
9191
as: 'script'
9292
},
9393
{
94-
pathname: expect.stringMatching(/^\/assets\/index\..+\.js$/),
94+
pathname: expect.stringMatching(/^\/assets\/index-.+\.js$/),
9595
rel: 'modulepreload',
9696
as: 'script'
9797
}

playground/css/__tests__/css.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ test('css import from js', async () => {
6464
test('css import asset with space', async () => {
6565
const importedWithSpace = await page.$('.import-with-space')
6666

67-
expect(await getBg(importedWithSpace)).toMatch(/.*ok\..*png/)
67+
expect(await getBg(importedWithSpace)).toMatch(/.*\/ok.*\.png/)
6868
})
6969

7070
test('postcss config', async () => {
@@ -90,7 +90,7 @@ test('sass', async () => {
9090
isBuild ? /base64/ : '/nested/icon.png'
9191
)
9292
expect(await getBg(urlStartsWithVariable)).toMatch(
93-
isBuild ? /ok\.\w+\.png/ : `${viteTestUrl}/ok.png`
93+
isBuild ? /ok-\w+\.png/ : `${viteTestUrl}/ok.png`
9494
)
9595
expect(await getColor(partialImport)).toBe('orchid')
9696

@@ -124,7 +124,7 @@ test('less', async () => {
124124
isBuild ? /base64/ : '/nested/icon.png'
125125
)
126126
expect(await getBg(urlStartsWithVariable)).toMatch(
127-
isBuild ? /ok\.\w+\.png/ : `${viteTestUrl}/ok.png`
127+
isBuild ? /ok-\w+\.png/ : `${viteTestUrl}/ok.png`
128128
)
129129

130130
editFile('less.less', (code) => code.replace('@color: blue', '@color: red'))
@@ -297,8 +297,8 @@ test('async chunk', async () => {
297297
if (isBuild) {
298298
// assert that the css is extracted into its own file instead of in the
299299
// main css file
300-
expect(findAssetFile(/index\.\w+\.css$/)).not.toMatch('teal')
301-
expect(findAssetFile(/async\.\w+\.css$/)).toMatch('.async{color:teal}')
300+
expect(findAssetFile(/index-\w+\.css$/)).not.toMatch('teal')
301+
expect(findAssetFile(/async-\w+\.css$/)).toMatch('.async{color:teal}')
302302
} else {
303303
// test hmr
304304
editFile('async.css', (code) => code.replace('color: teal', 'color: blue'))
@@ -316,8 +316,8 @@ test('treeshaken async chunk', async () => {
316316
).toBeNull()
317317
// assert that the css is not present anywhere
318318
expect(findAssetFile(/\.css$/)).not.toMatch('plum')
319-
expect(findAssetFile(/index\.\w+\.js$/)).not.toMatch('.async{color:plum}')
320-
expect(findAssetFile(/async\.\w+\.js$/)).not.toMatch('.async{color:plum}')
319+
expect(findAssetFile(/index-\w+\.js$/)).not.toMatch('.async{color:plum}')
320+
expect(findAssetFile(/async-\w+\.js$/)).not.toMatch('.async{color:plum}')
321321
// should have no chunk!
322322
expect(findAssetFile(/async-treeshaken/)).toBe('')
323323
} else {
@@ -416,7 +416,7 @@ test('minify css', async () => {
416416
}
417417

418418
// should keep the rgba() syntax
419-
const cssFile = findAssetFile(/index\.\w+\.css$/)
419+
const cssFile = findAssetFile(/index-\w+\.css$/)
420420
expect(cssFile).toMatch('rgba(')
421421
expect(cssFile).not.toMatch('#ffff00b3')
422422
})

playground/css/vite.config-relative-base.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module.exports = {
1515
rollupOptions: {
1616
output: {
1717
entryFileNames: 'entries/[name].js',
18-
chunkFileNames: 'chunks/[name].[hash].js',
19-
assetFileNames: 'other-assets/[name].[hash][extname]'
18+
chunkFileNames: 'chunks/[name]-[hash].js',
19+
assetFileNames: 'other-assets/[name]-[hash][extname]'
2020
}
2121
}
2222
},

playground/glob-import/__tests__/glob-import.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ test('tree-shake eager css', async () => {
185185
)
186186

187187
if (isBuild) {
188-
const content = findAssetFile(/index\.\w+\.js/)
188+
const content = findAssetFile(/index-\w+\.js/)
189189
expect(content).not.toMatch('.tree-shake-eager-css')
190190
}
191191
})

playground/legacy/__tests__/legacy.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test('should load dynamic import with css', async () => {
7676

7777
test('asset url', async () => {
7878
expect(await page.textContent('#asset-path')).toMatch(
79-
isBuild ? /\/assets\/vite\.\w+\.svg/ : '/vite.svg'
79+
isBuild ? /\/assets\/vite-\w+\.svg/ : '/vite.svg'
8080
)
8181
})
8282

@@ -116,6 +116,6 @@ describe.runIf(isBuild)('build', () => {
116116

117117
test('includes structuredClone polyfill which is supported after core-js v3', () => {
118118
expect(findAssetFile(/polyfills-legacy/)).toMatch('"structuredClone"')
119-
expect(findAssetFile(/polyfills\./)).toMatch('"structuredClone"')
119+
expect(findAssetFile(/polyfills-\w{8}\./)).toMatch('"structuredClone"')
120120
})
121121
})

playground/legacy/vite.config-multiple-output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ export default defineConfig({
1010
output: [
1111
{
1212
assetFileNames() {
13-
return 'assets/subdir/[name].[hash][extname]'
13+
return 'assets/subdir/[name]-[hash][extname]'
1414
},
1515
entryFileNames: `assets/subdir/[name].js`,
1616
chunkFileNames: `assets/subdir/[name].js`
1717
},
1818
{
1919
assetFileNames() {
20-
return 'assets/subdir/[name].[hash][extname]'
20+
return 'assets/subdir/[name]-[hash][extname]'
2121
},
2222
entryFileNames: `assets/anotherSubdir/[name].js`,
2323
chunkFileNames: `assets/anotherSubdir/[name].js`

playground/lib/__tests__/lib.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe.runIf(isBuild)('build', () => {
3636
expect(code).not.toMatch('__vitePreload')
3737

3838
// Test that library chunks are hashed
39-
expect(code).toMatch(/await import\("\.\/message.[a-z\d]{8}.mjs"\)/)
39+
expect(code).toMatch(/await import\("\.\/message-[a-z\d]{8}.mjs"\)/)
4040
})
4141

4242
test('@import hoist', async () => {

playground/preload/__tests__/preload.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ describe.runIf(isBuild)('build', () => {
1717
await page.goto(viteTestUrl + '/#/hello')
1818
const html = await page.content()
1919
expect(html).toMatch(
20-
/link rel="modulepreload".*?href=".*?\/assets\/Hello\.\w{8}\.js"/
20+
/link rel="modulepreload".*?href=".*?\/assets\/Hello-\w{8}\.js"/
2121
)
2222
expect(html).toMatch(
23-
/link rel="stylesheet".*?href=".*?\/assets\/Hello\.\w{8}\.css"/
23+
/link rel="stylesheet".*?href=".*?\/assets\/Hello-\w{8}\.css"/
2424
)
2525
})
2626
})

0 commit comments

Comments
 (0)