Skip to content

Commit

Permalink
feat(legacy): build file name optimization (#15115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiadesen authored Nov 28, 2023
1 parent 26a613a commit 39f435d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ const legacyEnvVarMarker = `__VITE_IS_LEGACY__`

const _require = createRequire(import.meta.url)

const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/
const prefixedHashInFileNameRE = /[.-]?\[hash(:\d+)?\]/

function viteLegacyPlugin(options: Options = {}): Plugin[] {
let config: ResolvedConfig
let targets: Options['targets']
Expand Down Expand Up @@ -337,11 +340,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
if (fileName.includes('[name]')) {
// [name]-[hash].[format] -> [name]-legacy-[hash].[format]
fileName = fileName.replace('[name]', '[name]-legacy')
} else if (fileName.includes('[hash]')) {
} else if (nonLeadingHashInFileNameRE.test(fileName)) {
// 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$&')
// custom.[hash:10].[format] -> custom-legacy.[hash:10].[format]
fileName = fileName.replace(prefixedHashInFileNameRE, '-legacy$&')
} else {
// entry.js -> entry-legacy.js
// entry.min.js -> entry-legacy.min.js
Expand Down

0 comments on commit 39f435d

Please sign in to comment.