Skip to content

Commit

Permalink
fix(legacy)!: should rename x.[hash].js to x-legacy.[hash].js (#1…
Browse files Browse the repository at this point in the history
…1599)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
aleen42 and bluwy authored Oct 6, 2023
1 parent a2e9fb5 commit e7d7a6f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions playground/legacy/__tests__/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/custom0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
1 change: 1 addition & 0 deletions playground/legacy/custom1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
1 change: 1 addition & 0 deletions playground/legacy/custom2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
3 changes: 3 additions & 0 deletions playground/legacy/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions playground/legacy/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
},
Expand Down

0 comments on commit e7d7a6f

Please sign in to comment.