From c2b94d4a169510a39947a92f17ac787c9b147f35 Mon Sep 17 00:00:00 2001 From: Michael Brevard Date: Fri, 29 Dec 2023 10:47:11 +0200 Subject: [PATCH] perf(kit,schema,nuxt): refactor `startsWith` to array access (#24744) --- packages/kit/src/compatibility.ts | 6 ++++-- packages/kit/src/ignore.ts | 5 +++-- packages/nuxt/src/app/composables/fetch.ts | 4 ++-- packages/nuxt/src/app/plugins/chunk-reload.client.ts | 2 +- packages/nuxt/src/core/plugins/import-protection.ts | 2 +- packages/nuxt/src/core/templates.ts | 2 +- packages/nuxt/src/pages/utils.ts | 2 +- packages/schema/src/config/webpack.ts | 4 ++-- 8 files changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/kit/src/compatibility.ts b/packages/kit/src/compatibility.ts index 431e3453d1..d1d7e50fda 100644 --- a/packages/kit/src/compatibility.ts +++ b/packages/kit/src/compatibility.ts @@ -73,14 +73,16 @@ export async function hasNuxtCompatibility (constraints: NuxtCompatibility, nuxt * Check if current nuxt instance is version 2 legacy */ export function isNuxt2 (nuxt: Nuxt = useNuxt()) { - return getNuxtVersion(nuxt).startsWith('2.') + const version = getNuxtVersion(nuxt) + return version[0] === '2' && version[1] === '.' } /** * Check if current nuxt instance is version 3 */ export function isNuxt3 (nuxt: Nuxt = useNuxt()) { - return getNuxtVersion(nuxt).startsWith('3.') + const version = getNuxtVersion(nuxt) + return version[0] === '3' && version[1] === '.' } /** diff --git a/packages/kit/src/ignore.ts b/packages/kit/src/ignore.ts index 8c42d3a051..bc93e8cd7a 100644 --- a/packages/kit/src/ignore.ts +++ b/packages/kit/src/ignore.ts @@ -22,7 +22,7 @@ export function isIgnored (pathname: string): boolean { const cwds = nuxt.options._layers?.map(layer => layer.cwd).sort((a, b) => b.length - a.length) const layer = cwds?.find(cwd => pathname.startsWith(cwd)) const relativePath = relative(layer ?? nuxt.options.rootDir, pathname) - if (relativePath.startsWith('..')) { + if (relativePath[0] === '.' && relativePath[1] === '.') { return false } return !!(relativePath && nuxt._ignore.ignores(relativePath)) @@ -47,7 +47,8 @@ export function resolveIgnorePatterns (relativePath?: string): string[] { } if (relativePath) { - return nuxt._ignorePatterns.map(p => p.startsWith('*') || p.startsWith('!*') ? p : relative(relativePath, resolve(nuxt.options.rootDir, p))) + // Map ignore patterns based on if they start with * or !* + return nuxt._ignorePatterns.map(p => p[0] === '*' || (p[0] === '!' && p[1] === '*') ? p : relative(relativePath, resolve(nuxt.options.rootDir, p))) } return nuxt._ignorePatterns diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index d97f0a111e..90d830c0a5 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -99,7 +99,7 @@ export function useFetch< const key = _key === autoKey ? '$f' + _key : _key - if (!opts.baseURL && typeof _request.value === 'string' && _request.value.startsWith('//')) { + if (!opts.baseURL && typeof _request.value === 'string' && (_request.value[0] === '/' && _request.value[1] === '/')) { throw new Error('[nuxt] [useFetch] the request URL must not start with "//".') } @@ -155,7 +155,7 @@ export function useFetch< // Use fetch with request context and headers for server direct API calls if (import.meta.server && !opts.$fetch) { - const isLocalFetch = typeof _request.value === 'string' && _request.value.startsWith('/') && (!toValue(opts.baseURL) || toValue(opts.baseURL)!.startsWith('/')) + const isLocalFetch = typeof _request.value === 'string' && _request.value[0] === '/' && (!toValue(opts.baseURL) || toValue(opts.baseURL)![0] === '/') if (isLocalFetch) { _$fetch = useRequestFetch() } diff --git a/packages/nuxt/src/app/plugins/chunk-reload.client.ts b/packages/nuxt/src/app/plugins/chunk-reload.client.ts index 9c141e8171..7b802c57f0 100644 --- a/packages/nuxt/src/app/plugins/chunk-reload.client.ts +++ b/packages/nuxt/src/app/plugins/chunk-reload.client.ts @@ -16,7 +16,7 @@ export default defineNuxtPlugin({ nuxtApp.hook('app:chunkError', ({ error }) => { chunkErrors.add(error) }) function reloadAppAtPath (to: RouteLocationNormalized) { - const isHash = 'href' in to && (to.href as string).startsWith('#') + const isHash = 'href' in to && (to.href as string)[0] === '#' const path = isHash ? config.app.baseURL + (to as any).href : joinURL(config.app.baseURL, to.fullPath) reloadNuxtApp({ path, persistState: true }) } diff --git a/packages/nuxt/src/core/plugins/import-protection.ts b/packages/nuxt/src/core/plugins/import-protection.ts index f8250a374c..4c00981199 100644 --- a/packages/nuxt/src/core/plugins/import-protection.ts +++ b/packages/nuxt/src/core/plugins/import-protection.ts @@ -32,7 +32,7 @@ export const ImportProtectionPlugin = createUnplugin(function (options: ImportPr enforce: 'pre', resolveId (id, importer) { if (!importer) { return } - if (id.startsWith('.')) { + if (id[0] === '.') { id = join(importer, '..', id) } if (isAbsolute(id)) { diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 1130230b84..d6f31e4327 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -152,7 +152,7 @@ export const schemaTemplate: NuxtTemplate = { })).filter(m => m.configKey && m.name && !adHocModules.includes(m.name)) const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir) - const getImportName = (name: string) => (name.startsWith('.') ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '') + const getImportName = (name: string) => (name[0] === '.' ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '') const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)]) return [ diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 15c3505220..94dcf70e21 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -279,7 +279,7 @@ function prepareRoutes (routes: NuxtPage[], parent?: NuxtPage, names = new Set !url.startsWith('/') + filter: (url: string, _resourcePath: string) => url[0] !== '/' }, esModule: false }, @@ -219,7 +219,7 @@ export default defineUntypedSchema({ cssModules: { importLoaders: 0, url: { - filter: (url: string, _resourcePath: string) => !url.startsWith('/') + filter: (url: string, _resourcePath: string) => url[0] !== '/' }, esModule: false, modules: {