Skip to content

Commit debc6e2

Browse files
authoredMar 22, 2023
perf: regex to startsWith/slice in utils (#12532)
1 parent b6ea25a commit debc6e2

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed
 

‎packages/vite/src/node/utils.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ const builtins = new Set([
107107
'wasi',
108108
])
109109

110+
const NODE_BUILTIN_NAMESPACE = 'node:'
110111
export function isBuiltin(id: string): boolean {
111-
return builtins.has(id.replace(/^node:/, ''))
112+
return builtins.has(
113+
id.startsWith(NODE_BUILTIN_NAMESPACE)
114+
? id.slice(NODE_BUILTIN_NAMESPACE.length)
115+
: id,
116+
)
112117
}
113118

114119
export function moduleListContains(
@@ -282,8 +287,10 @@ const knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/
282287
export const isTsRequest = (url: string): boolean => knownTsRE.test(url)
283288
export const isPossibleTsOutput = (url: string): boolean =>
284289
knownTsOutputRE.test(cleanUrl(url))
290+
291+
const splitFilePathAndQueryRE = /(\.(?:[cm]?js|jsx))(\?.*)?$/
285292
export function getPotentialTsSrcPaths(filePath: string): string[] {
286-
const [name, type, query = ''] = filePath.split(/(\.(?:[cm]?js|jsx))(\?.*)?$/)
293+
const [name, type, query = ''] = filePath.split(splitFilePathAndQueryRE)
287294
const paths = [name + type.replace('js', 'ts') + query]
288295
if (!type.endsWith('x')) {
289296
paths.push(name + type.replace('js', 'tsx') + query)
@@ -1250,7 +1257,7 @@ export function stripBase(path: string, base: string): string {
12501257
return '/'
12511258
}
12521259
const devBase = base.endsWith('/') ? base : base + '/'
1253-
return path.replace(RegExp('^' + devBase), '/')
1260+
return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path
12541261
}
12551262

12561263
export function arrayEqual(a: any[], b: any[]): boolean {

0 commit comments

Comments
 (0)
Please sign in to comment.