From ebcb5131558f8ac10f76123daee1ee0ddf531164 Mon Sep 17 00:00:00 2001 From: Aleen Date: Thu, 5 Jan 2023 19:35:36 +0800 Subject: [PATCH 1/3] fix: should rename `x.[hash].js` to `x-legacy.[hash].js` --- packages/plugin-legacy/src/index.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 3e8b86eee48888..1ead162f17904f 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -314,6 +314,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') From 1c3b5bc96286d7c9e2eceb01d00e2ba071c6cf3a Mon Sep 17 00:00:00 2001 From: Aleen Date: Fri, 6 Jan 2023 10:42:53 +0800 Subject: [PATCH 2/3] test: use a case to introduce the case when there is no `[name]` --- playground/legacy/__tests__/legacy.spec.ts | 7 +++++++ playground/legacy/custom0.js | 0 playground/legacy/custom1.js | 0 playground/legacy/custom2.js | 0 playground/legacy/main.js | 3 +++ playground/legacy/vite.config.js | 4 ++++ 6 files changed, 14 insertions(+) create mode 100644 playground/legacy/custom0.js create mode 100644 playground/legacy/custom1.js create mode 100644 playground/legacy/custom2.js diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index 290f6b69ac1cf9..2ec5c56715ec7e 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -88,6 +88,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..e69de29bb2d1d6 diff --git a/playground/legacy/custom1.js b/playground/legacy/custom1.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/playground/legacy/custom2.js b/playground/legacy/custom2.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/playground/legacy/main.js b/playground/legacy/main.js index 91ba0b038104c4..aee9bda9594354 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 7adf0f784eeeb2..c15a8b14b40cd2 100644 --- a/playground/legacy/vite.config.js +++ b/playground/legacy/vite.config.js @@ -23,6 +23,10 @@ module.exports = { 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` }, From d77ada3b9088efeb9d34ec11524cc1568890abdd Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 6 Oct 2023 14:49:20 +0800 Subject: [PATCH 3/3] chore: fix test --- playground/legacy/custom0.js | 1 + playground/legacy/custom1.js | 1 + playground/legacy/custom2.js | 1 + 3 files changed, 3 insertions(+) diff --git a/playground/legacy/custom0.js b/playground/legacy/custom0.js index e69de29bb2d1d6..21ec276fc7f825 100644 --- a/playground/legacy/custom0.js +++ b/playground/legacy/custom0.js @@ -0,0 +1 @@ +export const foo = 'bar' diff --git a/playground/legacy/custom1.js b/playground/legacy/custom1.js index e69de29bb2d1d6..21ec276fc7f825 100644 --- a/playground/legacy/custom1.js +++ b/playground/legacy/custom1.js @@ -0,0 +1 @@ +export const foo = 'bar' diff --git a/playground/legacy/custom2.js b/playground/legacy/custom2.js index e69de29bb2d1d6..21ec276fc7f825 100644 --- a/playground/legacy/custom2.js +++ b/playground/legacy/custom2.js @@ -0,0 +1 @@ +export const foo = 'bar'