From aebaa05628325227366b55af3dfe9ab1b3843085 Mon Sep 17 00:00:00 2001 From: Vben Date: Mon, 22 Mar 2021 16:40:52 +0800 Subject: [PATCH 1/2] fix(plugin-legacy): respect custom filenames format output fix #2356 --- packages/plugin-legacy/index.js | 39 ++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/plugin-legacy/index.js b/packages/plugin-legacy/index.js index 3eb0e863bf7bb1..d7a634c2c02bbb 100644 --- a/packages/plugin-legacy/index.js +++ b/packages/plugin-legacy/index.js @@ -150,6 +150,35 @@ function viteLegacyPlugin(options = {}) { return } + /** + * @param {string|((chunkInfo: import('rollup').PreRenderedChunk)=>string)} fileNames + * @param {string?} defaultFileName + */ + const getLegacyOutputFileName = ( + fileNames, + defaultFileName = '[name]-legacy.[hash].js' + ) => { + if (!fileNames) { + return path.posix.join(config.build.assetsDir, defaultFileName) + } + + // does not support custom functions. + if (typeof fileNames === 'function') { + throw new Error( + `@vitejs/plugin-legacy rollupOptions.output.entryFileNames and rollupOptions.output.chunkFileNames` + + ` does not currently support the function format.` + ) + } + + let fileName = defaultFileName + // Custom string file return format. + if (fileNames && typeof fileNames === 'string') { + fileName = fileNames.replace(/\[name\]/, '[name]-legacy') + } + + return path.posix.join(config.build.assetsDir, fileName) + } + /** * @param {import('rollup').OutputOptions} options * @returns {import('rollup').OutputOptions} @@ -158,14 +187,8 @@ function viteLegacyPlugin(options = {}) { return { ...options, format: 'system', - entryFileNames: path.posix.join( - config.build.assetsDir, - `[name]-legacy.[hash].js` - ), - chunkFileNames: path.posix.join( - config.build.assetsDir, - `[name]-legacy.[hash].js` - ) + entryFileNames: getLegacyOutputFileName(options.entryFileNames), + chunkFileNames: getLegacyOutputFileName(options.chunkFileNames) } } From 971e3bcc47716de2317d956f44ef521bb3e7a0cc Mon Sep 17 00:00:00 2001 From: Vben Date: Mon, 22 Mar 2021 22:43:06 +0800 Subject: [PATCH 2/2] chore: supplement legacy playground example --- packages/playground/legacy/package.json | 1 + .../legacy/vite.config-custom-filename.js | 15 +++++++++++++++ packages/plugin-legacy/index.js | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 packages/playground/legacy/vite.config-custom-filename.js diff --git a/packages/playground/legacy/package.json b/packages/playground/legacy/package.json index 89a5130f8e9eb9..4c65846365eabe 100644 --- a/packages/playground/legacy/package.json +++ b/packages/playground/legacy/package.json @@ -5,6 +5,7 @@ "scripts": { "dev": "vite", "build": "vite build --debug legacy", + "build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy", "debug": "node --inspect-brk ../../vite/bin/vite", "serve": "vite preview" }, diff --git a/packages/playground/legacy/vite.config-custom-filename.js b/packages/playground/legacy/vite.config-custom-filename.js new file mode 100644 index 00000000000000..9a96133b015588 --- /dev/null +++ b/packages/playground/legacy/vite.config-custom-filename.js @@ -0,0 +1,15 @@ +const legacy = require('@vitejs/plugin-legacy').default + +module.exports = { + plugins: [legacy()], + build: { + manifest: true, + minify: false, + rollupOptions: { + output: { + entryFileNames: `assets/[name].js`, + chunkFileNames: `assets/[name].js` + } + } + } +} diff --git a/packages/plugin-legacy/index.js b/packages/plugin-legacy/index.js index d7a634c2c02bbb..b019d070001f0b 100644 --- a/packages/plugin-legacy/index.js +++ b/packages/plugin-legacy/index.js @@ -166,7 +166,7 @@ function viteLegacyPlugin(options = {}) { if (typeof fileNames === 'function') { throw new Error( `@vitejs/plugin-legacy rollupOptions.output.entryFileNames and rollupOptions.output.chunkFileNames` + - ` does not currently support the function format.` + ` does not support the function format.` ) } @@ -176,7 +176,7 @@ function viteLegacyPlugin(options = {}) { fileName = fileNames.replace(/\[name\]/, '[name]-legacy') } - return path.posix.join(config.build.assetsDir, fileName) + return fileName } /**