fix(plugin-legacy): respect custom filenames formats, fix #2356 (#2641)

Co-authored-by: Shinigami <chrissi92@hotmail.de>
This commit is contained in:
Vben 2021-05-21 21:13:05 +08:00 committed by GitHub
parent 05bd96e21c
commit d852731622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 8 deletions

View File

@ -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"
},

View File

@ -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`
}
}
}
}

View File

@ -165,6 +165,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 support the function format.`
)
}
let fileName = defaultFileName
// Custom string file return format.
if (fileNames && typeof fileNames === 'string') {
fileName = fileNames.replace(/\[name\]/, '[name]-legacy')
}
return fileName
}
/**
* @param {import('rollup').OutputOptions} options
* @returns {import('rollup').OutputOptions}
@ -173,14 +202,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)
}
}