fix(legacy)!: should rename x.[hash].js to x-legacy.[hash].js (#11599)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
This commit is contained in:
Aleen 2023-10-06 17:55:09 +08:00 committed by GitHub
parent a2e9fb5243
commit e7d7a6f4ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 0 deletions

View File

@ -337,6 +337,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
if (fileName.includes('[name]')) {
// [name]-[hash].[format] -> [name]-legacy-[hash].[format]
fileName = fileName.replace('[name]', '[name]-legacy')
} else if (fileName.includes('[hash]')) {
// custom[hash].[format] -> [name]-legacy[hash].[format]
// custom-[hash].[format] -> [name]-legacy-[hash].[format]
// custom.[hash].[format] -> [name]-legacy.[hash].[format]
fileName = fileName.replace(/[.-]?\[hash\]/, '-legacy$&')
} else {
// entry.js -> entry-legacy.js
fileName = fileName.replace(/(.+)\.(.+)/, '$1-legacy.$2')

View File

@ -97,6 +97,13 @@ describe.runIf(isBuild)('build', () => {
expect(manifest['../../vite/legacy-polyfills-legacy'].src).toBe(
'../../vite/legacy-polyfills-legacy',
)
expect(manifest['custom0-legacy.js'].file).toMatch(
/chunk-X-legacy.\w{8}.js/,
)
expect(manifest['custom1-legacy.js'].file).toMatch(
/chunk-X-legacy-\w{8}.js/,
)
expect(manifest['custom2-legacy.js'].file).toMatch(/chunk-X-legacy\w{8}.js/)
// modern polyfill
expect(manifest['../../vite/legacy-polyfills']).toBeDefined()
expect(manifest['../../vite/legacy-polyfills'].src).toBe(

View File

@ -0,0 +1 @@
export const foo = 'bar'

View File

@ -0,0 +1 @@
export const foo = 'bar'

View File

@ -0,0 +1 @@
export const foo = 'bar'

View File

@ -3,6 +3,9 @@ import viteSvgPath from './vite.svg'
import MyWorker from './worker?worker'
async function run() {
await import('./custom0.js')
await import('./custom1.js')
await import('./custom2.js')
const { fn } = await import('./async.js')
fn()
}

View File

@ -25,6 +25,10 @@ export default defineConfig({
chunkFileNames(chunkInfo) {
if (chunkInfo.name === 'immutable-chunk') {
return `assets/${chunkInfo.name}.js`
} else if (/custom\d/.test(chunkInfo.name)) {
return `assets/chunk-X${
['.', '-', ''][/custom(\d)/.exec(chunkInfo.name)[1]]
}[hash].js`
}
return `assets/chunk-[name].[hash].js`
},