Skip to content

Commit

Permalink
refactor(plugin-legacy): build product file name optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jiadesen committed Nov 23, 2023
1 parent 0654d1b commit 1b50187
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,31 +324,23 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
| string
| ((chunkInfo: PreRenderedChunk) => string)
| undefined,
defaultFileName = '[name]-legacy-[hash].js',
defaultFileName = '[name]-[hash]-legacy.js',
): string | ((chunkInfo: PreRenderedChunk) => string) => {
if (!fileNames) {
return path.posix.join(config.build.assetsDir, defaultFileName)
}

return (chunkInfo) => {
let fileName =
const fileName =
typeof fileNames === 'function' ? fileNames(chunkInfo) : fileNames

if (fileName.includes('[name]')) {
// [name]-[hash].[format] -> [name]-legacy-[hash].[format]
fileName = fileName.replace('[name]', '[name]-legacy')
} else if (fileName.includes('[hash]')) {
// custom[hash].[format] -> [name]-legacy[hash].[format]
// custom-[hash].[format] -> [name]-legacy-[hash].[format]
// custom.[hash].[format] -> [name]-legacy.[hash].[format]
fileName = fileName.replace(/[.-]?\[hash\]/, '-legacy$&')
} else {
// entry.js -> entry-legacy.js
// entry.min.js -> entry-legacy.min.js
fileName = fileName.replace(/(.+?)\.(.+)/, '$1-legacy.$2')
}

return fileName
// [name]-[hash].[format] -> [name]-[hash]-legacy.[format]
// [hash].[format] -> [hash]-legacy.[format]
// custom[hash].[format] -> custom[hash]-legacy.[format]
// custom-[hash].[format] -> custom-[hash]-legacy.[format]
// custom.[hash].[format] -> custom.[hash]-legacy.[format]
// [hash].min.[format] -> [hash]-legacy.min.[format]
return fileName.replace(/(\.min\.|\.)[^.]*$/, "-legacy$&")
}
}

Expand Down

0 comments on commit 1b50187

Please sign in to comment.