Skip to content

Commit e7d7a6f

Browse files
aleen42bluwy
andauthored
fix(legacy)!: should rename x.[hash].js to x-legacy.[hash].js (#11599)
Co-authored-by: bluwy <[email protected]>
1 parent a2e9fb5 commit e7d7a6f

File tree

7 files changed

+22
-0
lines changed

7 files changed

+22
-0
lines changed

packages/plugin-legacy/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
337337
if (fileName.includes('[name]')) {
338338
// [name]-[hash].[format] -> [name]-legacy-[hash].[format]
339339
fileName = fileName.replace('[name]', '[name]-legacy')
340+
} else if (fileName.includes('[hash]')) {
341+
// custom[hash].[format] -> [name]-legacy[hash].[format]
342+
// custom-[hash].[format] -> [name]-legacy-[hash].[format]
343+
// custom.[hash].[format] -> [name]-legacy.[hash].[format]
344+
fileName = fileName.replace(/[.-]?\[hash\]/, '-legacy$&')
340345
} else {
341346
// entry.js -> entry-legacy.js
342347
fileName = fileName.replace(/(.+)\.(.+)/, '$1-legacy.$2')

playground/legacy/__tests__/legacy.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ describe.runIf(isBuild)('build', () => {
9797
expect(manifest['../../vite/legacy-polyfills-legacy'].src).toBe(
9898
'../../vite/legacy-polyfills-legacy',
9999
)
100+
expect(manifest['custom0-legacy.js'].file).toMatch(
101+
/chunk-X-legacy.\w{8}.js/,
102+
)
103+
expect(manifest['custom1-legacy.js'].file).toMatch(
104+
/chunk-X-legacy-\w{8}.js/,
105+
)
106+
expect(manifest['custom2-legacy.js'].file).toMatch(/chunk-X-legacy\w{8}.js/)
100107
// modern polyfill
101108
expect(manifest['../../vite/legacy-polyfills']).toBeDefined()
102109
expect(manifest['../../vite/legacy-polyfills'].src).toBe(

playground/legacy/custom0.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar'

playground/legacy/custom1.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar'

playground/legacy/custom2.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar'

playground/legacy/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import viteSvgPath from './vite.svg'
33
import MyWorker from './worker?worker'
44

55
async function run() {
6+
await import('./custom0.js')
7+
await import('./custom1.js')
8+
await import('./custom2.js')
69
const { fn } = await import('./async.js')
710
fn()
811
}

playground/legacy/vite.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export default defineConfig({
2525
chunkFileNames(chunkInfo) {
2626
if (chunkInfo.name === 'immutable-chunk') {
2727
return `assets/${chunkInfo.name}.js`
28+
} else if (/custom\d/.test(chunkInfo.name)) {
29+
return `assets/chunk-X${
30+
['.', '-', ''][/custom(\d)/.exec(chunkInfo.name)[1]]
31+
}[hash].js`
2832
}
2933
return `assets/chunk-[name].[hash].js`
3034
},

0 commit comments

Comments
 (0)