Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: regex to startsWith/slice in utils #12532

Merged
merged 3 commits into from Mar 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/vite/src/node/utils.ts
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 @@ -275,8 +280,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 @@ -1243,7 +1250,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