Skip to content

Commit dcee6ef

Browse files
authored
perf: use URL.canParse (#14068)
1 parent 6cb397f commit dcee6ef

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

packages/vite/src/node/plugins/html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import {
1818
getHash,
1919
isDataUrl,
2020
isExternalUrl,
21-
isUrl,
2221
normalizePath,
2322
processSrcSet,
2423
removeLeadingSlash,
24+
urlCanParse,
2525
} from '../utils'
2626
import type { ResolvedConfig } from '../config'
2727
import { toOutputFilePathInHtml } from '../build'
@@ -823,7 +823,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
823823
getPublicAssetFilename(fileHash, config)!,
824824
)
825825

826-
return isUrl(publicAssetPath)
826+
return urlCanParse(publicAssetPath)
827827
? publicAssetPath
828828
: normalizePath(publicAssetPath)
829829
})

packages/vite/src/node/utils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,17 @@ function testCaseInsensitiveFS() {
191191
return fs.existsSync(CLIENT_ENTRY.replace('client.mjs', 'cLiEnT.mjs'))
192192
}
193193

194-
export function isUrl(path: string): boolean {
195-
try {
196-
new URL(path)
197-
return true
198-
} catch {
199-
return false
200-
}
201-
}
194+
export const urlCanParse =
195+
URL.canParse ??
196+
// URL.canParse is supported from Node.js 18.17.0+, 20.0.0+
197+
((path: string, base?: string | undefined): boolean => {
198+
try {
199+
new URL(path, base)
200+
return true
201+
} catch {
202+
return false
203+
}
204+
})
202205

203206
export const isCaseInsensitiveFS = testCaseInsensitiveFS()
204207

0 commit comments

Comments
 (0)