diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 9e1e8f134b6f64..5e954c1ba07eb1 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -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') diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index 59cdc5a3999504..cfa6615fdcb1f0 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -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( diff --git a/playground/legacy/custom0.js b/playground/legacy/custom0.js new file mode 100644 index 00000000000000..21ec276fc7f825 --- /dev/null +++ b/playground/legacy/custom0.js @@ -0,0 +1 @@ +export const foo = 'bar' diff --git a/playground/legacy/custom1.js b/playground/legacy/custom1.js new file mode 100644 index 00000000000000..21ec276fc7f825 --- /dev/null +++ b/playground/legacy/custom1.js @@ -0,0 +1 @@ +export const foo = 'bar' diff --git a/playground/legacy/custom2.js b/playground/legacy/custom2.js new file mode 100644 index 00000000000000..21ec276fc7f825 --- /dev/null +++ b/playground/legacy/custom2.js @@ -0,0 +1 @@ +export const foo = 'bar' diff --git a/playground/legacy/main.js b/playground/legacy/main.js index 7a42c3cf23a138..e34b5835c82b9d 100644 --- a/playground/legacy/main.js +++ b/playground/legacy/main.js @@ -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() } diff --git a/playground/legacy/vite.config.js b/playground/legacy/vite.config.js index 3c4ec787809fee..4f319e250eb729 100644 --- a/playground/legacy/vite.config.js +++ b/playground/legacy/vite.config.js @@ -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` },