Skip to content

Commit

Permalink
perf: regex to startsWith/slice in utils (#12532)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Mar 22, 2023
1 parent b6ea25a commit debc6e2
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,13 @@ const builtins = new Set([
'wasi',
])

const NODE_BUILTIN_NAMESPACE = 'node:'
export function isBuiltin(id: string): boolean {
return builtins.has(id.replace(/^node:/, ''))
return builtins.has(
id.startsWith(NODE_BUILTIN_NAMESPACE)
? id.slice(NODE_BUILTIN_NAMESPACE.length)
: id,
)
}

export function moduleListContains(
Expand Down Expand Up @@ -282,8 +287,10 @@ const knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/
export const isTsRequest = (url: string): boolean => knownTsRE.test(url)
export const isPossibleTsOutput = (url: string): boolean =>
knownTsOutputRE.test(cleanUrl(url))

const splitFilePathAndQueryRE = /(\.(?:[cm]?js|jsx))(\?.*)?$/
export function getPotentialTsSrcPaths(filePath: string): string[] {
const [name, type, query = ''] = filePath.split(/(\.(?:[cm]?js|jsx))(\?.*)?$/)
const [name, type, query = ''] = filePath.split(splitFilePathAndQueryRE)
const paths = [name + type.replace('js', 'ts') + query]
if (!type.endsWith('x')) {
paths.push(name + type.replace('js', 'tsx') + query)
Expand Down Expand Up @@ -1250,7 +1257,7 @@ export function stripBase(path: string, base: string): string {
return '/'
}
const devBase = base.endsWith('/') ? base : base + '/'
return path.replace(RegExp('^' + devBase), '/')
return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path
}

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

0 comments on commit debc6e2

Please sign in to comment.