Skip to content

Commit 39f435d

Browse files
authored
feat(legacy): build file name optimization (#15115)
1 parent 26a613a commit 39f435d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

packages/plugin-legacy/src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ const legacyEnvVarMarker = `__VITE_IS_LEGACY__`
119119

120120
const _require = createRequire(import.meta.url)
121121

122+
const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/
123+
const prefixedHashInFileNameRE = /[.-]?\[hash(:\d+)?\]/
124+
122125
function viteLegacyPlugin(options: Options = {}): Plugin[] {
123126
let config: ResolvedConfig
124127
let targets: Options['targets']
@@ -337,11 +340,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
337340
if (fileName.includes('[name]')) {
338341
// [name]-[hash].[format] -> [name]-legacy-[hash].[format]
339342
fileName = fileName.replace('[name]', '[name]-legacy')
340-
} else if (fileName.includes('[hash]')) {
343+
} else if (nonLeadingHashInFileNameRE.test(fileName)) {
341344
// custom[hash].[format] -> [name]-legacy[hash].[format]
342345
// custom-[hash].[format] -> [name]-legacy-[hash].[format]
343346
// custom.[hash].[format] -> [name]-legacy.[hash].[format]
344-
fileName = fileName.replace(/[.-]?\[hash\]/, '-legacy$&')
347+
// custom.[hash:10].[format] -> custom-legacy.[hash:10].[format]
348+
fileName = fileName.replace(prefixedHashInFileNameRE, '-legacy$&')
345349
} else {
346350
// entry.js -> entry-legacy.js
347351
// entry.min.js -> entry-legacy.min.js

0 commit comments

Comments
 (0)