From debc6e2eaafedbf737e79d03023a5f4b9f307cc5 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 22 Mar 2023 19:03:48 +0100 Subject: [PATCH] perf: regex to startsWith/slice in utils (#12532) --- packages/vite/src/node/utils.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index d13205bb007402..34ec07fb6993e4 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -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( @@ -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) @@ -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 {