From 7eb52ec5141ad8b2276ef53d3cee953f8269cca5 Mon Sep 17 00:00:00 2001 From: sun0day Date: Thu, 23 Mar 2023 20:34:09 +0800 Subject: [PATCH] perf: replace endsWith with === (#12539) --- .../vite/src/node/server/middlewares/static.ts | 7 +++++-- packages/vite/src/node/utils.ts | 14 +++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/static.ts b/packages/vite/src/node/server/middlewares/static.ts index 27ac7cbceecab5..f59961085e923f 100644 --- a/packages/vite/src/node/server/middlewares/static.ts +++ b/packages/vite/src/node/server/middlewares/static.ts @@ -92,7 +92,7 @@ export function serveStaticMiddleware( // also skip internal requests `/@fs/ /@vite-client` etc... const cleanedUrl = cleanUrl(req.url!) if ( - cleanedUrl.endsWith('/') || + cleanedUrl[cleanedUrl.length - 1] === '/' || path.extname(cleanedUrl) === '.html' || isInternalRequest(req.url!) ) { @@ -123,7 +123,10 @@ export function serveStaticMiddleware( const resolvedPathname = redirectedPathname || pathname let fileUrl = path.resolve(dir, removeLeadingSlash(resolvedPathname)) - if (resolvedPathname.endsWith('/') && !fileUrl.endsWith('/')) { + if ( + resolvedPathname[resolvedPathname.length - 1] === '/' && + fileUrl[fileUrl.length - 1] !== '/' + ) { fileUrl = fileUrl + '/' } if (!ensureServingAccess(fileUrl, server, res, next)) { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 34ec07fb6993e4..4eb5bef7ae87f3 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -242,7 +242,7 @@ export function fsPathFromUrl(url: string): string { * @returns true if dir is a parent of file */ export function isParentDirectory(dir: string, file: string): boolean { - if (!dir.endsWith('/')) { + if (dir[dir.length - 1] !== '/') { dir = `${dir}/` } return ( @@ -276,7 +276,7 @@ export const isJSRequest = (url: string): boolean => { if (knownJsSrcRE.test(url)) { return true } - if (!path.extname(url) && !url.endsWith('/')) { + if (!path.extname(url) && url[url.length - 1] !== '/') { return true } return false @@ -292,7 +292,7 @@ const splitFilePathAndQueryRE = /(\.(?:[cm]?js|jsx))(\?.*)?$/ export function getPotentialTsSrcPaths(filePath: string): string[] { const [name, type, query = ''] = filePath.split(splitFilePathAndQueryRE) const paths = [name + type.replace('js', 'ts') + query] - if (!type.endsWith('x')) { + if (type[type.length - 1] !== 'x') { paths.push(name + type.replace('js', 'tsx') + query) } return paths @@ -1146,8 +1146,8 @@ function normalizeSingleAlias({ }: Alias): Alias { if ( typeof find === 'string' && - find.endsWith('/') && - replacement.endsWith('/') + find[find.length - 1] === '/' && + replacement[replacement.length - 1] === '/' ) { find = find.slice(0, find.length - 1) replacement = replacement.slice(0, replacement.length - 1) @@ -1239,7 +1239,7 @@ export function joinUrlSegments(a: string, b: string): string { if (!a || !b) { return a || b || '' } - if (a.endsWith('/')) { + if (a[a.length - 1] === '/') { a = a.substring(0, a.length - 1) } if (b[0] !== '/') { @@ -1256,7 +1256,7 @@ export function stripBase(path: string, base: string): string { if (path === base) { return '/' } - const devBase = base.endsWith('/') ? base : base + '/' + const devBase = base[base.length - 1] === '/' ? base : base + '/' return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path }