From b96b62ecd2daf412ddc9b02cdc15f82a34a231ab Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 14 May 2024 19:54:37 +0200 Subject: [PATCH] refactor: improve regexp performance (#27207) --- eslint.config.mjs | 4 ++ package.json | 1 + packages/kit/src/compatibility.ts | 2 +- packages/kit/src/internal/template.ts | 2 +- packages/kit/src/runtime-config.ts | 2 +- packages/kit/src/template.ts | 4 +- .../src/components/client-fallback-auto-id.ts | 4 +- .../nuxt/src/components/islandsTransform.ts | 2 +- packages/nuxt/src/components/loader.ts | 2 +- packages/nuxt/src/components/module.ts | 2 +- packages/nuxt/src/components/scan.ts | 4 +- packages/nuxt/src/components/templates.ts | 4 +- packages/nuxt/src/components/tree-shake.ts | 2 +- packages/nuxt/src/core/builder.ts | 2 +- packages/nuxt/src/core/nitro.ts | 2 +- packages/nuxt/src/core/nuxt.ts | 2 +- .../src/core/plugins/import-protection.ts | 2 +- .../src/core/runtime/nitro/dev-server-logs.ts | 4 +- .../nuxt/src/core/runtime/nitro/renderer.ts | 2 +- packages/nuxt/src/core/templates.ts | 2 +- packages/nuxt/src/core/utils/plugins.ts | 2 +- .../pages/runtime/plugins/prerender.server.ts | 2 +- packages/nuxt/src/pages/utils.ts | 8 ++-- packages/nuxt/test/treeshake-client.test.ts | 2 +- packages/ui-templates/lib/dev.ts | 2 +- packages/ui-templates/lib/render.ts | 22 ++++----- packages/vite/src/plugins/composable-keys.ts | 6 +-- packages/vite/src/plugins/ssr-styles.ts | 2 +- packages/vite/src/utils/index.ts | 2 +- packages/webpack/src/configs/server.ts | 2 +- packages/webpack/src/plugins/vue/util.ts | 2 +- packages/webpack/src/utils/config.ts | 2 +- pnpm-lock.yaml | 47 +++++++++++++++++++ scripts/update-changelog.ts | 2 +- test/basic.test.ts | 18 +++---- test/utils.ts | 2 +- 36 files changed, 113 insertions(+), 61 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index c4e282b673..8f4fc6a467 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -5,6 +5,7 @@ import noOnlyTests from 'eslint-plugin-no-only-tests' import typegen from 'eslint-typegen' // @ts-expect-error missing types import perfectionist from 'eslint-plugin-perfectionist' +import regex from 'eslint-plugin-regexp' export default createConfigForNuxt({ features: { @@ -216,6 +217,9 @@ export default createConfigForNuxt({ 'vue/multi-word-component-names': 'off', }, }, + + // @ts-ignore types misaligned + regex.configs['flat/recommended'], ) // Generate type definitions for the eslint config diff --git a/package.json b/package.json index a3e094b228..0620a07d04 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "eslint": "9.2.0", "eslint-plugin-no-only-tests": "3.1.0", "eslint-plugin-perfectionist": "2.10.0", + "eslint-plugin-regexp": "^2.5.0", "eslint-typegen": "0.2.4", "execa": "9.1.0", "fs-extra": "11.2.0", diff --git a/packages/kit/src/compatibility.ts b/packages/kit/src/compatibility.ts index a03813dd48..b2720f05c9 100644 --- a/packages/kit/src/compatibility.ts +++ b/packages/kit/src/compatibility.ts @@ -4,7 +4,7 @@ import type { Nuxt, NuxtCompatibility, NuxtCompatibilityIssues } from '@nuxt/sch import { useNuxt } from './context' export function normalizeSemanticVersion (version: string) { - return version.replace(/-[0-9]+\.[0-9a-f]+/, '') // Remove edge prefix + return version.replace(/-\d+\.[0-9a-f]+/, '') // Remove edge prefix } const builderMap = { diff --git a/packages/kit/src/internal/template.ts b/packages/kit/src/internal/template.ts index 9349f704df..5b40a2a214 100644 --- a/packages/kit/src/internal/template.ts +++ b/packages/kit/src/internal/template.ts @@ -27,7 +27,7 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) { } /** @deprecated */ -const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"{(.+)}"(?=,?$)/gm, r => JSON.parse(r).replace(/^{(.*)}$/, '$1')) +const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"\{(.+)\}"(?=,?$)/gm, r => JSON.parse(r).replace(/^\{(.*)\}$/, '$1')) /** @deprecated */ const importSources = (sources: string | string[], { lazy = false } = {}) => { diff --git a/packages/kit/src/runtime-config.ts b/packages/kit/src/runtime-config.ts index a83358f11c..f6e014fd3e 100644 --- a/packages/kit/src/runtime-config.ts +++ b/packages/kit/src/runtime-config.ts @@ -94,7 +94,7 @@ function applyEnv ( return obj } -const envExpandRx = /{{(.*?)}}/g +const envExpandRx = /\{\{(.*?)\}\}/g function _expandFromEnv (value: string, env: Record = process.env) { return value.replace(envExpandRx, (match, key) => { diff --git a/packages/kit/src/template.ts b/packages/kit/src/template.ts index 9290e62fd5..4aa9ec147b 100644 --- a/packages/kit/src/template.ts +++ b/packages/kit/src/template.ts @@ -202,7 +202,7 @@ export async function _generateTypes (nuxt: Nuxt) { } else { const path = stats?.isFile() // remove extension - ? relativePath.replace(/(?<=\w)\.\w+$/g, '') + ? relativePath.replace(/\b\.\w+$/g, '') // non-existent file probably shouldn't be resolved : aliases[alias] @@ -230,7 +230,7 @@ export async function _generateTypes (nuxt: Nuxt) { tsConfig.compilerOptions!.paths[alias] = await Promise.all(paths.map(async (path: string) => { if (!isAbsolute(path)) { return path } const stats = await fsp.stat(path).catch(() => null /* file does not exist */) - return relativeWithDot(nuxt.options.buildDir, stats?.isFile() ? path.replace(/(?<=\w)\.\w+$/g, '') /* remove extension */ : path) + return relativeWithDot(nuxt.options.buildDir, stats?.isFile() ? path.replace(/\b\.\w+$/g, '') /* remove extension */ : path) })) } diff --git a/packages/nuxt/src/components/client-fallback-auto-id.ts b/packages/nuxt/src/components/client-fallback-auto-id.ts index 19b5a39984..7aa42f1d0a 100644 --- a/packages/nuxt/src/components/client-fallback-auto-id.ts +++ b/packages/nuxt/src/components/client-fallback-auto-id.ts @@ -10,7 +10,7 @@ interface LoaderOptions { transform?: ComponentsOptions['transform'] rootDir: string } -const CLIENT_FALLBACK_RE = /<(NuxtClientFallback|nuxt-client-fallback)( [^>]*)?>/ +const CLIENT_FALLBACK_RE = /<(?:NuxtClientFallback|nuxt-client-fallback)(?: [^>]*)?>/ const CLIENT_FALLBACK_GLOBAL_RE = /<(NuxtClientFallback|nuxt-client-fallback)( [^>]*)?>/g export const clientFallbackAutoIdPlugin = createUnplugin((options: LoaderOptions) => { const exclude = options.transform?.exclude || [] @@ -37,7 +37,7 @@ export const clientFallbackAutoIdPlugin = createUnplugin((options: LoaderOptions s.replace(CLIENT_FALLBACK_GLOBAL_RE, (full, name, attrs) => { count++ - if (/ :?uid=/g.test(attrs)) { return full } + if (/ :?uid=/.test(attrs)) { return full } return `<${name} :uid="'${hash(relativeID)}' + JSON.stringify($props) + '${count}'" ${attrs ?? ''}>` }) diff --git a/packages/nuxt/src/components/islandsTransform.ts b/packages/nuxt/src/components/islandsTransform.ts index 6e018dc167..ac5a954932 100644 --- a/packages/nuxt/src/components/islandsTransform.ts +++ b/packages/nuxt/src/components/islandsTransform.ts @@ -25,7 +25,7 @@ interface ComponentChunkOptions { } const SCRIPT_RE = /]*>/g -const HAS_SLOT_OR_CLIENT_RE = /(]*>)|(nuxt-client)/ +const HAS_SLOT_OR_CLIENT_RE = /]*>|nuxt-client/ const TEMPLATE_RE = /