perf: unify (j/t)sx regex

This commit is contained in:
tbitw2549 2024-10-20 21:33:03 +03:00
parent dc4d032246
commit ee6f91b7e3
3 changed files with 5 additions and 6 deletions

View File

@ -1,13 +1,12 @@
import { createUnplugin } from 'unplugin' import { createUnplugin } from 'unplugin'
import MagicString from 'magic-string' import MagicString from 'magic-string'
import type { Component } from 'nuxt/schema' import type { Component } from 'nuxt/schema'
import { isVue } from '../../core/utils' import { SX_RE, isVue } from '../../core/utils'
interface NameDevPluginOptions { interface NameDevPluginOptions {
sourcemap: boolean sourcemap: boolean
getComponents: () => Component[] getComponents: () => Component[]
} }
const NOT_SX_RE = /\.[tj]sx$/
const FILENAME_RE = /([^/\\]+)\.\w+$/ const FILENAME_RE = /([^/\\]+)\.\w+$/
/** /**
* Set the default name of components to their PascalCase name * Set the default name of components to their PascalCase name
@ -17,7 +16,7 @@ export const ComponentNamePlugin = (options: NameDevPluginOptions) => createUnpl
name: 'nuxt:component-name-plugin', name: 'nuxt:component-name-plugin',
enforce: 'post', enforce: 'post',
transformInclude (id) { transformInclude (id) {
return isVue(id) || !!id.match(NOT_SX_RE) return isVue(id) || !!id.match(SX_RE)
}, },
transform (code, id) { transform (code, id) {
const filename = id.match(FILENAME_RE)?.[1] const filename = id.match(FILENAME_RE)?.[1]

View File

@ -6,7 +6,7 @@ import { relative } from 'pathe'
import type { Component, ComponentsOptions } from 'nuxt/schema' import type { Component, ComponentsOptions } from 'nuxt/schema'
import { logger, tryUseNuxt } from '@nuxt/kit' import { logger, tryUseNuxt } from '@nuxt/kit'
import { isVue, QUOTE_RE } from '../../core/utils' import { SX_RE, QUOTE_RE, isVue } from '../../core/utils'
interface LoaderOptions { interface LoaderOptions {
getComponents (): Component[] getComponents (): Component[]
@ -18,7 +18,6 @@ interface LoaderOptions {
} }
const REPLACE_COMPONENT_TO_DIRECT_IMPORT_RE = /(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy(?=[A-Z]))?([^'"]*)["'][^)]*\)/g const REPLACE_COMPONENT_TO_DIRECT_IMPORT_RE = /(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy(?=[A-Z]))?([^'"]*)["'][^)]*\)/g
const NOT_SX_RE = /\.[tj]sx$/
export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => { export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => {
const exclude = options.transform?.exclude || [] const exclude = options.transform?.exclude || []
const include = options.transform?.include || [] const include = options.transform?.include || []
@ -34,7 +33,7 @@ export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => {
if (include.some(pattern => pattern.test(id))) { if (include.some(pattern => pattern.test(id))) {
return true return true
} }
return isVue(id, { type: ['template', 'script'] }) || !!id.match(IS_VUE_RE) return isVue(id, { type: ['template', 'script'] }) || !!id.match(SX_RE)
}, },
transform (code, id) { transform (code, id) {
const components = options.getComponents() const components = options.getComponents()

View File

@ -17,3 +17,4 @@ export function uniqueBy<T, K extends keyof T> (arr: T[], key: K) {
export const QUOTE_RE = /["']/g export const QUOTE_RE = /["']/g
export const EXTENSION_RE = /\b\.\w+$/g export const EXTENSION_RE = /\b\.\w+$/g
export const SX_RE = /\.[tj]sx$/