Skip to content

Commit d852731

Browse files
anncwbShinigami92
andauthored
fix(plugin-legacy): respect custom filenames formats, fix #2356 (#2641)
Co-authored-by: Shinigami <[email protected]>
1 parent 05bd96e commit d852731

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

packages/playground/legacy/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"dev": "vite",
77
"build": "vite build --debug legacy",
8+
"build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy",
89
"debug": "node --inspect-brk ../../vite/bin/vite",
910
"serve": "vite preview"
1011
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const legacy = require('@vitejs/plugin-legacy').default
2+
3+
module.exports = {
4+
plugins: [legacy()],
5+
build: {
6+
manifest: true,
7+
minify: false,
8+
rollupOptions: {
9+
output: {
10+
entryFileNames: `assets/[name].js`,
11+
chunkFileNames: `assets/[name].js`
12+
}
13+
}
14+
}
15+
}

packages/plugin-legacy/index.js

+31-8
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,35 @@ function viteLegacyPlugin(options = {}) {
165165
return
166166
}
167167

168+
/**
169+
* @param {string|((chunkInfo: import('rollup').PreRenderedChunk)=>string)} fileNames
170+
* @param {string?} defaultFileName
171+
*/
172+
const getLegacyOutputFileName = (
173+
fileNames,
174+
defaultFileName = '[name]-legacy.[hash].js'
175+
) => {
176+
if (!fileNames) {
177+
return path.posix.join(config.build.assetsDir, defaultFileName)
178+
}
179+
180+
// does not support custom functions.
181+
if (typeof fileNames === 'function') {
182+
throw new Error(
183+
`@vitejs/plugin-legacy rollupOptions.output.entryFileNames and rollupOptions.output.chunkFileNames` +
184+
` does not support the function format.`
185+
)
186+
}
187+
188+
let fileName = defaultFileName
189+
// Custom string file return format.
190+
if (fileNames && typeof fileNames === 'string') {
191+
fileName = fileNames.replace(/\[name\]/, '[name]-legacy')
192+
}
193+
194+
return fileName
195+
}
196+
168197
/**
169198
* @param {import('rollup').OutputOptions} options
170199
* @returns {import('rollup').OutputOptions}
@@ -173,14 +202,8 @@ function viteLegacyPlugin(options = {}) {
173202
return {
174203
...options,
175204
format: 'system',
176-
entryFileNames: path.posix.join(
177-
config.build.assetsDir,
178-
`[name]-legacy.[hash].js`
179-
),
180-
chunkFileNames: path.posix.join(
181-
config.build.assetsDir,
182-
`[name]-legacy.[hash].js`
183-
)
205+
entryFileNames: getLegacyOutputFileName(options.entryFileNames),
206+
chunkFileNames: getLegacyOutputFileName(options.chunkFileNames)
184207
}
185208
}
186209

0 commit comments

Comments
 (0)