perf(kit): unify mode regex

This commit is contained in:
tbitw2549 2024-10-20 21:39:35 +03:00
parent ee6f91b7e3
commit f2a9a568b0
3 changed files with 6 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import type { Component, ComponentsDir } from '@nuxt/schema'
import { useNuxt } from './context'
import { assertNuxtCompatibility } from './compatibility'
import { logger } from './logger'
import { MODE_RE } from './utils'
/**
* Register a directory to be scanned for components and imported only when used.
@ -19,7 +20,6 @@ export type AddComponentOptions = { name: string, filePath: string } & Partial<E
'shortPath' | 'async' | 'level' | 'import' | 'asyncImport'
>>
const COMPONENT_MODE_RE = /\.(server|client)(\.\w+)*$/
/**
* Register a component by its name and filePath.
*/
@ -29,7 +29,7 @@ export async function addComponent (opts: AddComponentOptions) {
nuxt.options.components = nuxt.options.components || []
if (!opts.mode) {
const [, mode = 'all'] = opts.filePath.match(COMPONENT_MODE_RE) || []
const [, mode = 'all'] = opts.filePath.match(MODE_RE) || []
opts.mode = mode as 'all' | 'client' | 'server'
}

View File

@ -3,8 +3,8 @@ import type { NuxtPlugin, NuxtPluginTemplate } from '@nuxt/schema'
import { useNuxt } from './context'
import { addTemplate } from './template'
import { resolveAlias } from './resolve'
import { MODE_RE } from './utils'
const PLUGIN_MODE_RE = /\.(server|client)(\.\w+)*$/
/**
* Normalize a nuxt plugin object
*/
@ -28,7 +28,7 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
plugin.mode = 'server'
}
if (!plugin.mode) {
const [, mode = 'all'] = plugin.src.match(PLUGIN_MODE_RE) || []
const [, mode = 'all'] = plugin.src.match(MODE_RE) || []
plugin.mode = mode as 'all' | 'client' | 'server'
}

View File

@ -2,3 +2,5 @@
export function toArray<T> (value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
}
export const MODE_RE = /\.(server|client)(\.\w+)*$/