Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/playground/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
15 changes: 15 additions & 0 deletions packages/playground/legacy/vite.config-custom-filename.js
Original file line number Diff line number Diff line change
@@ -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`
}
}
}
}
39 changes: 31 additions & 8 deletions packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
}
}

Expand Down