Skip to content

Commit b96b62e

Browse files
authored
refactor: improve regexp performance (nuxt#27207)
1 parent 7189daf commit b96b62e

36 files changed

+113
-61
lines changed

eslint.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import noOnlyTests from 'eslint-plugin-no-only-tests'
55
import typegen from 'eslint-typegen'
66
// @ts-expect-error missing types
77
import perfectionist from 'eslint-plugin-perfectionist'
8+
import regex from 'eslint-plugin-regexp'
89

910
export default createConfigForNuxt({
1011
features: {
@@ -216,6 +217,9 @@ export default createConfigForNuxt({
216217
'vue/multi-word-component-names': 'off',
217218
},
218219
},
220+
221+
// @ts-ignore types misaligned
222+
regex.configs['flat/recommended'],
219223
)
220224

221225
// Generate type definitions for the eslint config

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"eslint": "9.2.0",
6565
"eslint-plugin-no-only-tests": "3.1.0",
6666
"eslint-plugin-perfectionist": "2.10.0",
67+
"eslint-plugin-regexp": "^2.5.0",
6768
"eslint-typegen": "0.2.4",
6869
"execa": "9.1.0",
6970
"fs-extra": "11.2.0",

packages/kit/src/compatibility.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Nuxt, NuxtCompatibility, NuxtCompatibilityIssues } from '@nuxt/sch
44
import { useNuxt } from './context'
55

66
export function normalizeSemanticVersion (version: string) {
7-
return version.replace(/-[0-9]+\.[0-9a-f]+/, '') // Remove edge prefix
7+
return version.replace(/-\d+\.[0-9a-f]+/, '') // Remove edge prefix
88
}
99

1010
const builderMap = {

packages/kit/src/internal/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function compileTemplate<T> (template: NuxtTemplate<T>, ctx: any) {
2727
}
2828

2929
/** @deprecated */
30-
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"{(.+)}"(?=,?$)/gm, r => JSON.parse(r).replace(/^{(.*)}$/, '$1'))
30+
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"\{(.+)\}"(?=,?$)/gm, r => JSON.parse(r).replace(/^\{(.*)\}$/, '$1'))
3131

3232
/** @deprecated */
3333
const importSources = (sources: string | string[], { lazy = false } = {}) => {

packages/kit/src/runtime-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function applyEnv (
9494
return obj
9595
}
9696

97-
const envExpandRx = /{{(.*?)}}/g
97+
const envExpandRx = /\{\{(.*?)\}\}/g
9898

9999
function _expandFromEnv (value: string, env: Record<string, any> = process.env) {
100100
return value.replace(envExpandRx, (match, key) => {

packages/kit/src/template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export async function _generateTypes (nuxt: Nuxt) {
202202
} else {
203203
const path = stats?.isFile()
204204
// remove extension
205-
? relativePath.replace(/(?<=\w)\.\w+$/g, '')
205+
? relativePath.replace(/\b\.\w+$/g, '')
206206
// non-existent file probably shouldn't be resolved
207207
: aliases[alias]
208208

@@ -230,7 +230,7 @@ export async function _generateTypes (nuxt: Nuxt) {
230230
tsConfig.compilerOptions!.paths[alias] = await Promise.all(paths.map(async (path: string) => {
231231
if (!isAbsolute(path)) { return path }
232232
const stats = await fsp.stat(path).catch(() => null /* file does not exist */)
233-
return relativeWithDot(nuxt.options.buildDir, stats?.isFile() ? path.replace(/(?<=\w)\.\w+$/g, '') /* remove extension */ : path)
233+
return relativeWithDot(nuxt.options.buildDir, stats?.isFile() ? path.replace(/\b\.\w+$/g, '') /* remove extension */ : path)
234234
}))
235235
}
236236

packages/nuxt/src/components/client-fallback-auto-id.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface LoaderOptions {
1010
transform?: ComponentsOptions['transform']
1111
rootDir: string
1212
}
13-
const CLIENT_FALLBACK_RE = /<(NuxtClientFallback|nuxt-client-fallback)( [^>]*)?>/
13+
const CLIENT_FALLBACK_RE = /<(?:NuxtClientFallback|nuxt-client-fallback)(?: [^>]*)?>/
1414
const CLIENT_FALLBACK_GLOBAL_RE = /<(NuxtClientFallback|nuxt-client-fallback)( [^>]*)?>/g
1515
export const clientFallbackAutoIdPlugin = createUnplugin((options: LoaderOptions) => {
1616
const exclude = options.transform?.exclude || []
@@ -37,7 +37,7 @@ export const clientFallbackAutoIdPlugin = createUnplugin((options: LoaderOptions
3737

3838
s.replace(CLIENT_FALLBACK_GLOBAL_RE, (full, name, attrs) => {
3939
count++
40-
if (/ :?uid=/g.test(attrs)) { return full }
40+
if (/ :?uid=/.test(attrs)) { return full }
4141
return `<${name} :uid="'${hash(relativeID)}' + JSON.stringify($props) + '${count}'" ${attrs ?? ''}>`
4242
})
4343

packages/nuxt/src/components/islandsTransform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface ComponentChunkOptions {
2525
}
2626

2727
const SCRIPT_RE = /<script[^>]*>/g
28-
const HAS_SLOT_OR_CLIENT_RE = /(<slot[^>]*>)|(nuxt-client)/
28+
const HAS_SLOT_OR_CLIENT_RE = /<slot[^>]*>|nuxt-client/
2929
const TEMPLATE_RE = /<template>([\s\S]*)<\/template>/
3030
const NUXTCLIENT_ATTR_RE = /\s:?nuxt-client(="[^"]*")?/g
3131
const IMPORT_CODE = '\nimport { vforToArray as __vforToArray } from \'#app/components/utils\'' + '\nimport NuxtTeleportIslandComponent from \'#app/components/nuxt-teleport-island-component\'' + '\nimport NuxtTeleportSsrSlot from \'#app/components/nuxt-teleport-island-slot\''

packages/nuxt/src/components/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const loaderPlugin = createUnplugin((options: LoaderOptions) => {
4343
const s = new MagicString(code)
4444

4545
// replace `_resolveComponent("...")` to direct import
46-
s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy)?([^'"]*?)["'][\s,]*[^)]*\)/g, (full: string, lazy: string, name: string) => {
46+
s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy)?([^'"]*)["'][^)]*\)/g, (full: string, lazy: string, name: string) => {
4747
const component = findComponent(components, name, options.mode)
4848
if (component) {
4949
// @ts-expect-error TODO: refactor to nuxi

packages/nuxt/src/components/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function compareDirByPathLength ({ path: pathA }: { path: string }, { path: path
1818
return pathB.split(/[\\/]/).filter(Boolean).length - pathA.split(/[\\/]/).filter(Boolean).length
1919
}
2020

21-
const DEFAULT_COMPONENTS_DIRS_RE = /\/components(\/global|\/islands)?$/
21+
const DEFAULT_COMPONENTS_DIRS_RE = /\/components\/(?:global|islands)?$/
2222

2323
export type getComponentsT = (mode?: 'client' | 'server' | 'all') => Component[]
2424

0 commit comments

Comments
 (0)