From 1b50187bba7e604ec397fa5b108191e5e8b231ff Mon Sep 17 00:00:00 2001 From: jiadesen Date: Thu, 23 Nov 2023 11:54:02 +0800 Subject: [PATCH 01/12] refactor(plugin-legacy): build product file name optimization --- packages/plugin-legacy/src/index.ts | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index a121693446c039..db95ab42acc792 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -324,31 +324,23 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { | string | ((chunkInfo: PreRenderedChunk) => string) | undefined, - defaultFileName = '[name]-legacy-[hash].js', + defaultFileName = '[name]-[hash]-legacy.js', ): string | ((chunkInfo: PreRenderedChunk) => string) => { if (!fileNames) { return path.posix.join(config.build.assetsDir, defaultFileName) } return (chunkInfo) => { - let fileName = + const fileName = typeof fileNames === 'function' ? fileNames(chunkInfo) : fileNames - 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 - // entry.min.js -> entry-legacy.min.js - fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') - } - - return fileName + // [name]-[hash].[format] -> [name]-[hash]-legacy.[format] + // [hash].[format] -> [hash]-legacy.[format] + // custom[hash].[format] -> custom[hash]-legacy.[format] + // custom-[hash].[format] -> custom-[hash]-legacy.[format] + // custom.[hash].[format] -> custom.[hash]-legacy.[format] + // [hash].min.[format] -> [hash]-legacy.min.[format] + return fileName.replace(/(\.min\.|\.)[^.]*$/, "-legacy$&") } } From 8ee38ddd626faf9637b4b4b4b5f70ead0fca1047 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Thu, 23 Nov 2023 15:17:28 +0800 Subject: [PATCH 02/12] test(playground/legacy): update case --- playground/legacy/__tests__/legacy.spec.ts | 32 ++++++++++++------- .../no-polyfills-no-systemjs.spec.ts | 2 +- .../no-polyfills/no-polyfills.spec.ts | 4 +-- .../watch/styles-only-entry-watch.spec.ts | 10 +++--- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index b0cc061b289d6c..bff7bb5302a6b7 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -98,13 +98,13 @@ describe.runIf(isBuild)('build', () => { '../../vite/legacy-polyfills-legacy', ) expect(manifest['custom0-legacy.js'].file).toMatch( - /chunk-X-legacy\.[-\w]{8}.js/, + /chunk-X\.[-\w]{8}-legacy\.js/, ) expect(manifest['custom1-legacy.js'].file).toMatch( - /chunk-X-legacy-[-\w]{8}.js/, + /chunk-X-[-\w]{8}-legacy\.js/, ) expect(manifest['custom2-legacy.js'].file).toMatch( - /chunk-X-legacy[-\w]{8}.js/, + /chunk-X[-\w]{8}-legacy\.js/, ) // modern polyfill expect(manifest['../../vite/legacy-polyfills']).toBeDefined() @@ -119,15 +119,19 @@ describe.runIf(isBuild)('build', () => { // esbuild-minified) does not! const terserPattern = /^(?:!function|System.register)/ - expect(findAssetFile(/chunk-async-legacy/)).toMatch(terserPattern) - expect(findAssetFile(/chunk-async(?!-legacy)/)).not.toMatch(terserPattern) + expect(findAssetFile(/chunk-async\.[-\w]{8}-legacy/)).toMatch(terserPattern) + expect(findAssetFile(/chunk-async\.[-\w]{8}(?!-legacy)/)).not.toMatch( + terserPattern, + ) expect(findAssetFile(/immutable-chunk-legacy/)).toMatch(terserPattern) expect(findAssetFile(/immutable-chunk(?!-legacy)/)).not.toMatch( terserPattern, ) - expect(findAssetFile(/index-legacy/)).toMatch(terserPattern) - expect(findAssetFile(/index(?!-legacy)/)).not.toMatch(terserPattern) - expect(findAssetFile(/polyfills-legacy/)).toMatch(terserPattern) + expect(findAssetFile(/index-[-\w]{8}-legacy/)).toMatch(terserPattern) + expect(findAssetFile(/index-[-\w]{8}(?!-legacy)/)).not.toMatch( + terserPattern, + ) + expect(findAssetFile(/polyfills-[-\w]{8}-legacy/)).toMatch(terserPattern) }) test('should emit css file', async () => { @@ -137,13 +141,17 @@ describe.runIf(isBuild)('build', () => { }) test('includes structuredClone polyfill which is supported after core-js v3', () => { - expect(findAssetFile(/polyfills-legacy/)).toMatch('"structuredClone"') + expect(findAssetFile(/polyfills-[-\w]{8}-legacy/)).toMatch( + '"structuredClone"', + ) expect(findAssetFile(/polyfills-[-\w]{8}\./)).toMatch('"structuredClone"') }) test('should generate legacy sourcemap file', async () => { expect( - listAssets().some((filename) => /index-legacy.+\.map$/.test(filename)), + listAssets().some((filename) => + /index-[-\w]{8}-legacy.+\.map$/.test(filename), + ), ).toBeTruthy() expect( listAssets().some((filename) => @@ -154,8 +162,8 @@ describe.runIf(isBuild)('build', () => { test('should have only modern entry files guarded', async () => { const guard = /(import\s*\()|(import.meta)|(async\s*function\*)/ - expect(findAssetFile(/index(?!-legacy)/)).toMatch(guard) - expect(findAssetFile(/polyfills(?!-legacy)/)).toMatch(guard) + expect(findAssetFile(/index-[-\w]{8}(?!-legacy)/)).toMatch(guard) + expect(findAssetFile(/polyfills-[-\w]{8}(?!-legacy)/)).toMatch(guard) expect(findAssetFile(/chunk-async(?!-legacy)/)).not.toMatch(guard) expect(findAssetFile(/index-legacy/)).not.toMatch(guard) diff --git a/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts b/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts index 609c45c0749b64..d5ae0f552c9021 100644 --- a/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts +++ b/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts @@ -6,7 +6,7 @@ test.runIf(isBuild)('includes only a single script tag', async () => { await untilUpdated( () => page.getAttribute('#vite-legacy-entry', 'data-src'), - /.\/assets\/index-legacy-(.+)\.js/, + /.\/assets\/index-[-\w]{8}-legacy\.js/, true, ) diff --git a/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts b/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts index 9563c569830c8f..d8b54dbbf8004c 100644 --- a/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts +++ b/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts @@ -9,12 +9,12 @@ test('should load and execute the JS file', async () => { test.runIf(isBuild)('includes a script tag for SystemJS', async () => { await untilUpdated( () => page.getAttribute('#vite-legacy-polyfill', 'src'), - /.\/assets\/polyfills-legacy-(.+)\.js/, + /.\/assets\/polyfills-[-\w]{8}-legacy\.js/, true, ) await untilUpdated( () => page.getAttribute('#vite-legacy-entry', 'data-src'), - /.\/assets\/index-legacy-(.+)\.js/, + /.\/assets\/index-[-\w]{8}-legacy\.js/, true, ) }) diff --git a/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts b/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts index 7fd7f9391316fa..4345ac0f5361dc 100644 --- a/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts +++ b/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts @@ -12,10 +12,10 @@ test.runIf(isBuild)('rebuilds styles only entry on change', async () => { expect(findAssetFile(/style-only-entry-.+\.css/, 'watch')).toContain( 'hotpink', ) - expect(findAssetFile(/style-only-entry-legacy-.+\.js/, 'watch')).toContain( - 'hotpink', - ) - expect(findAssetFile(/polyfills-legacy-.+\.js/, 'watch')).toBeTruthy() + expect( + findAssetFile(/style-only-entry-[-\w]{8}-legacy\.js/, 'watch'), + ).toContain('hotpink') + expect(findAssetFile(/polyfills-[-\w]{8}-legacy\.js/, 'watch')).toBeTruthy() const numberOfManifestEntries = Object.keys(readManifest('watch')).length expect(numberOfManifestEntries).toBe(3) @@ -43,5 +43,5 @@ test.runIf(isBuild)('rebuilds styles only entry on change', async () => { 'watch', ) expect(reRenderedCssLegacyFile).toContain('lightpink') - expect(findAssetFile(/polyfills-legacy-.+\.js/, 'watch')).toBeTruthy() + expect(findAssetFile(/polyfills-[-\w]{8}-legacy\.js/, 'watch')).toBeTruthy() }) From 4ab20369c26e88f6ae2cdb1ad1c0f8663631d85a Mon Sep 17 00:00:00 2001 From: jiadesen Date: Thu, 23 Nov 2023 15:23:17 +0800 Subject: [PATCH 03/12] style(plugin-legacy): prettier --- packages/plugin-legacy/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index db95ab42acc792..38a51539e6e366 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -340,7 +340,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { // custom-[hash].[format] -> custom-[hash]-legacy.[format] // custom.[hash].[format] -> custom.[hash]-legacy.[format] // [hash].min.[format] -> [hash]-legacy.min.[format] - return fileName.replace(/(\.min\.|\.)[^.]*$/, "-legacy$&") + return fileName.replace(/(\.min\.|\.)[^.]*$/, '-legacy$&') } } From edad9b874f110d2aa0163c5cd6647fee82b25f0d Mon Sep 17 00:00:00 2001 From: jiadesen Date: Thu, 23 Nov 2023 16:27:20 +0800 Subject: [PATCH 04/12] Revert "style(plugin-legacy): prettier" This reverts commit 4ab20369c26e88f6ae2cdb1ad1c0f8663631d85a. --- packages/plugin-legacy/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 38a51539e6e366..db95ab42acc792 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -340,7 +340,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { // custom-[hash].[format] -> custom-[hash]-legacy.[format] // custom.[hash].[format] -> custom.[hash]-legacy.[format] // [hash].min.[format] -> [hash]-legacy.min.[format] - return fileName.replace(/(\.min\.|\.)[^.]*$/, '-legacy$&') + return fileName.replace(/(\.min\.|\.)[^.]*$/, "-legacy$&") } } From e19c550b2e936689f2837204eab66f150b49ba93 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Thu, 23 Nov 2023 16:27:26 +0800 Subject: [PATCH 05/12] Revert "test(playground/legacy): update case" This reverts commit 8ee38ddd626faf9637b4b4b4b5f70ead0fca1047. --- playground/legacy/__tests__/legacy.spec.ts | 32 +++++++------------ .../no-polyfills-no-systemjs.spec.ts | 2 +- .../no-polyfills/no-polyfills.spec.ts | 4 +-- .../watch/styles-only-entry-watch.spec.ts | 10 +++--- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index bff7bb5302a6b7..b0cc061b289d6c 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -98,13 +98,13 @@ describe.runIf(isBuild)('build', () => { '../../vite/legacy-polyfills-legacy', ) expect(manifest['custom0-legacy.js'].file).toMatch( - /chunk-X\.[-\w]{8}-legacy\.js/, + /chunk-X-legacy\.[-\w]{8}.js/, ) expect(manifest['custom1-legacy.js'].file).toMatch( - /chunk-X-[-\w]{8}-legacy\.js/, + /chunk-X-legacy-[-\w]{8}.js/, ) expect(manifest['custom2-legacy.js'].file).toMatch( - /chunk-X[-\w]{8}-legacy\.js/, + /chunk-X-legacy[-\w]{8}.js/, ) // modern polyfill expect(manifest['../../vite/legacy-polyfills']).toBeDefined() @@ -119,19 +119,15 @@ describe.runIf(isBuild)('build', () => { // esbuild-minified) does not! const terserPattern = /^(?:!function|System.register)/ - expect(findAssetFile(/chunk-async\.[-\w]{8}-legacy/)).toMatch(terserPattern) - expect(findAssetFile(/chunk-async\.[-\w]{8}(?!-legacy)/)).not.toMatch( - terserPattern, - ) + expect(findAssetFile(/chunk-async-legacy/)).toMatch(terserPattern) + expect(findAssetFile(/chunk-async(?!-legacy)/)).not.toMatch(terserPattern) expect(findAssetFile(/immutable-chunk-legacy/)).toMatch(terserPattern) expect(findAssetFile(/immutable-chunk(?!-legacy)/)).not.toMatch( terserPattern, ) - expect(findAssetFile(/index-[-\w]{8}-legacy/)).toMatch(terserPattern) - expect(findAssetFile(/index-[-\w]{8}(?!-legacy)/)).not.toMatch( - terserPattern, - ) - expect(findAssetFile(/polyfills-[-\w]{8}-legacy/)).toMatch(terserPattern) + expect(findAssetFile(/index-legacy/)).toMatch(terserPattern) + expect(findAssetFile(/index(?!-legacy)/)).not.toMatch(terserPattern) + expect(findAssetFile(/polyfills-legacy/)).toMatch(terserPattern) }) test('should emit css file', async () => { @@ -141,17 +137,13 @@ describe.runIf(isBuild)('build', () => { }) test('includes structuredClone polyfill which is supported after core-js v3', () => { - expect(findAssetFile(/polyfills-[-\w]{8}-legacy/)).toMatch( - '"structuredClone"', - ) + expect(findAssetFile(/polyfills-legacy/)).toMatch('"structuredClone"') expect(findAssetFile(/polyfills-[-\w]{8}\./)).toMatch('"structuredClone"') }) test('should generate legacy sourcemap file', async () => { expect( - listAssets().some((filename) => - /index-[-\w]{8}-legacy.+\.map$/.test(filename), - ), + listAssets().some((filename) => /index-legacy.+\.map$/.test(filename)), ).toBeTruthy() expect( listAssets().some((filename) => @@ -162,8 +154,8 @@ describe.runIf(isBuild)('build', () => { test('should have only modern entry files guarded', async () => { const guard = /(import\s*\()|(import.meta)|(async\s*function\*)/ - expect(findAssetFile(/index-[-\w]{8}(?!-legacy)/)).toMatch(guard) - expect(findAssetFile(/polyfills-[-\w]{8}(?!-legacy)/)).toMatch(guard) + expect(findAssetFile(/index(?!-legacy)/)).toMatch(guard) + expect(findAssetFile(/polyfills(?!-legacy)/)).toMatch(guard) expect(findAssetFile(/chunk-async(?!-legacy)/)).not.toMatch(guard) expect(findAssetFile(/index-legacy/)).not.toMatch(guard) diff --git a/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts b/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts index d5ae0f552c9021..609c45c0749b64 100644 --- a/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts +++ b/playground/legacy/__tests__/no-polyfills-no-systemjs/no-polyfills-no-systemjs.spec.ts @@ -6,7 +6,7 @@ test.runIf(isBuild)('includes only a single script tag', async () => { await untilUpdated( () => page.getAttribute('#vite-legacy-entry', 'data-src'), - /.\/assets\/index-[-\w]{8}-legacy\.js/, + /.\/assets\/index-legacy-(.+)\.js/, true, ) diff --git a/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts b/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts index d8b54dbbf8004c..9563c569830c8f 100644 --- a/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts +++ b/playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts @@ -9,12 +9,12 @@ test('should load and execute the JS file', async () => { test.runIf(isBuild)('includes a script tag for SystemJS', async () => { await untilUpdated( () => page.getAttribute('#vite-legacy-polyfill', 'src'), - /.\/assets\/polyfills-[-\w]{8}-legacy\.js/, + /.\/assets\/polyfills-legacy-(.+)\.js/, true, ) await untilUpdated( () => page.getAttribute('#vite-legacy-entry', 'data-src'), - /.\/assets\/index-[-\w]{8}-legacy\.js/, + /.\/assets\/index-legacy-(.+)\.js/, true, ) }) diff --git a/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts b/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts index 4345ac0f5361dc..7fd7f9391316fa 100644 --- a/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts +++ b/playground/legacy/__tests__/watch/styles-only-entry-watch.spec.ts @@ -12,10 +12,10 @@ test.runIf(isBuild)('rebuilds styles only entry on change', async () => { expect(findAssetFile(/style-only-entry-.+\.css/, 'watch')).toContain( 'hotpink', ) - expect( - findAssetFile(/style-only-entry-[-\w]{8}-legacy\.js/, 'watch'), - ).toContain('hotpink') - expect(findAssetFile(/polyfills-[-\w]{8}-legacy\.js/, 'watch')).toBeTruthy() + expect(findAssetFile(/style-only-entry-legacy-.+\.js/, 'watch')).toContain( + 'hotpink', + ) + expect(findAssetFile(/polyfills-legacy-.+\.js/, 'watch')).toBeTruthy() const numberOfManifestEntries = Object.keys(readManifest('watch')).length expect(numberOfManifestEntries).toBe(3) @@ -43,5 +43,5 @@ test.runIf(isBuild)('rebuilds styles only entry on change', async () => { 'watch', ) expect(reRenderedCssLegacyFile).toContain('lightpink') - expect(findAssetFile(/polyfills-[-\w]{8}-legacy\.js/, 'watch')).toBeTruthy() + expect(findAssetFile(/polyfills-legacy-.+\.js/, 'watch')).toBeTruthy() }) From bdaf9e3362f4a9714b5f96f03ea6a0b1a0eb9675 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Thu, 23 Nov 2023 16:27:32 +0800 Subject: [PATCH 06/12] Revert "refactor(plugin-legacy): build product file name optimization" This reverts commit 1b50187bba7e604ec397fa5b108191e5e8b231ff. --- packages/plugin-legacy/src/index.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index db95ab42acc792..a121693446c039 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -324,23 +324,31 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { | string | ((chunkInfo: PreRenderedChunk) => string) | undefined, - defaultFileName = '[name]-[hash]-legacy.js', + defaultFileName = '[name]-legacy-[hash].js', ): string | ((chunkInfo: PreRenderedChunk) => string) => { if (!fileNames) { return path.posix.join(config.build.assetsDir, defaultFileName) } return (chunkInfo) => { - const fileName = + let fileName = typeof fileNames === 'function' ? fileNames(chunkInfo) : fileNames - // [name]-[hash].[format] -> [name]-[hash]-legacy.[format] - // [hash].[format] -> [hash]-legacy.[format] - // custom[hash].[format] -> custom[hash]-legacy.[format] - // custom-[hash].[format] -> custom-[hash]-legacy.[format] - // custom.[hash].[format] -> custom.[hash]-legacy.[format] - // [hash].min.[format] -> [hash]-legacy.min.[format] - return fileName.replace(/(\.min\.|\.)[^.]*$/, "-legacy$&") + 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 + // entry.min.js -> entry-legacy.min.js + fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') + } + + return fileName } } From 735fdeb49c0090951602f9b171eb59a20cbb62d4 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Fri, 24 Nov 2023 12:06:26 +0800 Subject: [PATCH 07/12] refactor(plugin-legacy): build product file name optimization --- packages/plugin-legacy/src/index.ts | 40 +++++++++++++++++++---------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index a121693446c039..3ed6ab50a51c10 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -331,24 +331,38 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { } return (chunkInfo) => { - let fileName = + const file = typeof fileNames === 'function' ? fileNames(chunkInfo) : fileNames + const fileArr = file.split('/') + let fileName = fileArr.pop() - 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$&') + if (!fileName) { + // user may pass in a path ending with `/` + fileName = defaultFileName } else { - // entry.js -> entry-legacy.js - // entry.min.js -> entry-legacy.min.js - fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') + if (fileName.includes('[name]')) { + // [name]-[hash].[format] -> [name]-legacy-[hash].[format] + fileName = fileName.replace('[name]', '[name]-legacy') + } else { + if ( + fileName.includes('[hash]') && + !fileName.startsWith('[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 + // entry.min.js -> entry-legacy.min.js + // [hash].[format] -> [hash]-legacy.[format] + // [hash]custom.[format] -> [hash]custom-legacy.[format] + fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') + } + } } - return fileName + return [...fileArr, fileName].join('/') } } From 61fa77777ce61893fe008ad786fdcb79d4754f26 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Fri, 24 Nov 2023 12:25:48 +0800 Subject: [PATCH 08/12] chore(plugin-legacy): remove unnecessary features --- packages/plugin-legacy/src/index.ts | 36 +++++++++++------------------ 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 3ed6ab50a51c10..5b29fb4f320974 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -334,31 +334,23 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { const file = typeof fileNames === 'function' ? fileNames(chunkInfo) : fileNames const fileArr = file.split('/') - let fileName = fileArr.pop() + let fileName = fileArr.pop()! - if (!fileName) { - // user may pass in a path ending with `/` - fileName = defaultFileName + if (fileName.includes('[name]')) { + // [name]-[hash].[format] -> [name]-legacy-[hash].[format] + fileName = fileName.replace('[name]', '[name]-legacy') } else { - if (fileName.includes('[name]')) { - // [name]-[hash].[format] -> [name]-legacy-[hash].[format] - fileName = fileName.replace('[name]', '[name]-legacy') + if (fileName.includes('[hash]') && !fileName.startsWith('[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 { - if ( - fileName.includes('[hash]') && - !fileName.startsWith('[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 - // entry.min.js -> entry-legacy.min.js - // [hash].[format] -> [hash]-legacy.[format] - // [hash]custom.[format] -> [hash]custom-legacy.[format] - fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') - } + // entry.js -> entry-legacy.js + // entry.min.js -> entry-legacy.min.js + // [hash].[format] -> [hash]-legacy.[format] + // [hash]custom.[format] -> [hash]custom-legacy.[format] + fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') } } From ee3f5f6ac3eeb27583014e1ea1436e34a2b228c6 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Sat, 25 Nov 2023 15:24:41 +0800 Subject: [PATCH 09/12] chore(plugin-legacy): simplify if branches --- packages/plugin-legacy/src/index.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 5b29fb4f320974..4b3d781dcd3e68 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -339,19 +339,20 @@ 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]') && + !fileName.startsWith('[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 { - if (fileName.includes('[hash]') && !fileName.startsWith('[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 - // entry.min.js -> entry-legacy.min.js - // [hash].[format] -> [hash]-legacy.[format] - // [hash]custom.[format] -> [hash]custom-legacy.[format] - fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') - } + // entry.js -> entry-legacy.js + // entry.min.js -> entry-legacy.min.js + // [hash].[format] -> [hash]-legacy.[format] + // [hash]custom.[format] -> [hash]custom-legacy.[format] + fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') } return [...fileArr, fileName].join('/') From 596f6a062ec13865321d3beaa26fdd81fd4a7623 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Sat, 25 Nov 2023 16:18:44 +0800 Subject: [PATCH 10/12] chore(plugin-legacy): compatible with `[hash:10]` --- packages/plugin-legacy/src/index.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 4b3d781dcd3e68..6878b904faf066 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -339,14 +339,12 @@ 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]') && - !fileName.startsWith('[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 if (/.+\[hash(?::\d+)?\]/.test(fileName)) { + // custom[hash].[format] -> custom-legacy[hash].[format] + // custom-[hash].[format] -> custom-legacy-[hash].[format] + // custom.[hash].[format] -> custom-legacy.[hash].[format] + // custom.[hash:10].[format] -> custom-legacy.[hash:10].[format] + fileName = fileName.replace(/[.-]?\[hash(:\d+)?\]/, '-legacy$&') } else { // entry.js -> entry-legacy.js // entry.min.js -> entry-legacy.min.js From a8ffc304a92b8f3858e0facaaa63e19c3b08a3c7 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Tue, 28 Nov 2023 12:14:10 +0800 Subject: [PATCH 11/12] refactor(plugin-legacy): build product file name optimization --- packages/plugin-legacy/src/index.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 6878b904faf066..16dfbf4bd32478 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -331,29 +331,25 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { } return (chunkInfo) => { - const file = + let fileName = typeof fileNames === 'function' ? fileNames(chunkInfo) : fileNames - const fileArr = file.split('/') - let fileName = fileArr.pop()! if (fileName.includes('[name]')) { // [name]-[hash].[format] -> [name]-legacy-[hash].[format] fileName = fileName.replace('[name]', '[name]-legacy') - } else if (/.+\[hash(?::\d+)?\]/.test(fileName)) { - // custom[hash].[format] -> custom-legacy[hash].[format] - // custom-[hash].[format] -> custom-legacy-[hash].[format] - // custom.[hash].[format] -> custom-legacy.[hash].[format] + } else if (/[^/]+\[hash(?::\d+)?\]/.test(fileName)) { + // custom[hash].[format] -> [name]-legacy[hash].[format] + // custom-[hash].[format] -> [name]-legacy-[hash].[format] + // custom.[hash].[format] -> [name]-legacy.[hash].[format] // custom.[hash:10].[format] -> custom-legacy.[hash:10].[format] fileName = fileName.replace(/[.-]?\[hash(:\d+)?\]/, '-legacy$&') } else { // entry.js -> entry-legacy.js // entry.min.js -> entry-legacy.min.js - // [hash].[format] -> [hash]-legacy.[format] - // [hash]custom.[format] -> [hash]custom-legacy.[format] fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2') } - return [...fileArr, fileName].join('/') + return fileName } } From 2a99167803141903b2fb67b242005ac839711494 Mon Sep 17 00:00:00 2001 From: jiadesen Date: Tue, 28 Nov 2023 19:18:17 +0800 Subject: [PATCH 12/12] chore(plugin-legacy): extract regexes --- packages/plugin-legacy/src/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 16dfbf4bd32478..1d964aa84f2792 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -119,6 +119,9 @@ const legacyEnvVarMarker = `__VITE_IS_LEGACY__` const _require = createRequire(import.meta.url) +const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/ +const prefixedHashInFileNameRE = /[.-]?\[hash(:\d+)?\]/ + function viteLegacyPlugin(options: Options = {}): Plugin[] { let config: ResolvedConfig let targets: Options['targets'] @@ -337,12 +340,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { if (fileName.includes('[name]')) { // [name]-[hash].[format] -> [name]-legacy-[hash].[format] fileName = fileName.replace('[name]', '[name]-legacy') - } else if (/[^/]+\[hash(?::\d+)?\]/.test(fileName)) { + } else if (nonLeadingHashInFileNameRE.test(fileName)) { // custom[hash].[format] -> [name]-legacy[hash].[format] // custom-[hash].[format] -> [name]-legacy-[hash].[format] // custom.[hash].[format] -> [name]-legacy.[hash].[format] // custom.[hash:10].[format] -> custom-legacy.[hash:10].[format] - fileName = fileName.replace(/[.-]?\[hash(:\d+)?\]/, '-legacy$&') + fileName = fileName.replace(prefixedHashInFileNameRE, '-legacy$&') } else { // entry.js -> entry-legacy.js // entry.min.js -> entry-legacy.min.js