perf(kit,schema,nuxt): refactor startsWith to array access (#24744)

This commit is contained in:
Michael Brevard 2023-12-29 10:47:11 +02:00 committed by GitHub
parent d99001832f
commit c2b94d4a16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 12 deletions

View File

@ -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] === '.'
}
/**

View File

@ -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

View File

@ -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()
}

View File

@ -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 })
}

View File

@ -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)) {

View File

@ -152,7 +152,7 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
})).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 [

View File

@ -279,7 +279,7 @@ function prepareRoutes (routes: NuxtPage[], parent?: NuxtPage, names = new Set<s
}
// Remove leading / if children route
if (parent && route.path.startsWith('/')) {
if (parent && route.path[0] === '/') {
route.path = route.path.slice(1)
}

View File

@ -211,7 +211,7 @@ export default defineUntypedSchema({
css: {
importLoaders: 0,
url: {
filter: (url: string, _resourcePath: string) => !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: {