Skip to content

Commit

Permalink
perf: use URL.canParse (#14068)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Aug 10, 2023
1 parent 6cb397f commit dcee6ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import {
getHash,
isDataUrl,
isExternalUrl,
isUrl,
normalizePath,
processSrcSet,
removeLeadingSlash,
urlCanParse,
} from '../utils'
import type { ResolvedConfig } from '../config'
import { toOutputFilePathInHtml } from '../build'
Expand Down Expand Up @@ -823,7 +823,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
getPublicAssetFilename(fileHash, config)!,
)

return isUrl(publicAssetPath)
return urlCanParse(publicAssetPath)
? publicAssetPath
: normalizePath(publicAssetPath)
})
Expand Down
19 changes: 11 additions & 8 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,17 @@ function testCaseInsensitiveFS() {
return fs.existsSync(CLIENT_ENTRY.replace('client.mjs', 'cLiEnT.mjs'))
}

export function isUrl(path: string): boolean {
try {
new URL(path)
return true
} catch {
return false
}
}
export const urlCanParse =
URL.canParse ??
// URL.canParse is supported from Node.js 18.17.0+, 20.0.0+
((path: string, base?: string | undefined): boolean => {
try {
new URL(path, base)
return true
} catch {
return false
}
})

export const isCaseInsensitiveFS = testCaseInsensitiveFS()

Expand Down

0 comments on commit dcee6ef

Please sign in to comment.