From eac7ff90d389fecc09a331b4875ee04bb3782e29 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 22 Mar 2023 11:04:59 +0100 Subject: [PATCH 1/3] perf: regex to startsWidth/slice in utils --- 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 1b385c19bb6736..b0e28bf497e678 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( @@ -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) @@ -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(base) ? path.slice(devBase.length) : path } export function arrayEqual(a: any[], b: any[]): boolean { From 838cf57d2d5b87a5150ae5310d13a42437393cd7 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 22 Mar 2023 12:01:28 +0100 Subject: [PATCH 2/3] chore: update --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index b0e28bf497e678..66920e831b7a48 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1250,7 +1250,7 @@ export function stripBase(path: string, base: string): string { return '/' } const devBase = base.endsWith('/') ? base : base + '/' - return path.startsWith(base) ? path.slice(devBase.length) : path + return path.startsWith(devBase) ? path.slice(devBase.length) : path } export function arrayEqual(a: any[], b: any[]): boolean { From 9027ef80bf74b48f6e37e2cb747b21d85b224168 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 22 Mar 2023 12:02:26 +0100 Subject: [PATCH 3/3] chore: update --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 66920e831b7a48..7cebe70330b36d 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1250,7 +1250,7 @@ export function stripBase(path: string, base: string): string { return '/' } const devBase = base.endsWith('/') ? base : base + '/' - return path.startsWith(devBase) ? path.slice(devBase.length) : path + return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path } export function arrayEqual(a: any[], b: any[]): boolean {