Skip to content

Commit

Permalink
fix(plugin-legacy): respect custom filenames formats, fix #2356 (#2641)
Browse files Browse the repository at this point in the history
Co-authored-by: Shinigami <[email protected]>
  • Loading branch information
anncwb and Shinigami92 committed May 21, 2021
1 parent 05bd96e commit d852731
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
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

0 comments on commit d852731

Please sign in to comment.