Skip to content

Commit fc007df

Browse files
authored
perf: regexp perf issues, refactor regexp stylistic issues (#10905)
fix #10900
1 parent 92a206b commit fc007df

File tree

38 files changed

+156
-89
lines changed

38 files changed

+156
-89
lines changed

.eslintrc.cjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ module.exports = defineConfig({
77
extends: [
88
'eslint:recommended',
99
'plugin:node/recommended',
10-
'plugin:@typescript-eslint/recommended'
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:regexp/recommended'
1112
],
12-
plugins: ['import'],
13+
plugins: ['import', 'regexp'],
1314
parser: '@typescript-eslint/parser',
1415
parserOptions: {
1516
sourceType: 'module',
@@ -97,7 +98,9 @@ module.exports = defineConfig({
9798
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
9899
allowSeparatedGroups: false
99100
}
100-
]
101+
],
102+
103+
'regexp/no-contradiction-with-assertion': 'error'
101104
},
102105
overrides: [
103106
{

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"eslint-define-config": "^1.11.0",
6767
"eslint-plugin-import": "^2.26.0",
6868
"eslint-plugin-node": "^11.1.0",
69+
"eslint-plugin-regexp": "^1.10.0",
6970
"execa": "^6.1.0",
7071
"fast-glob": "^3.2.12",
7172
"fs-extra": "^10.1.0",

packages/create-vite/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ function copy(src: string, dest: string) {
388388
}
389389

390390
function isValidPackageName(projectName: string) {
391-
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
391+
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(
392392
projectName
393393
)
394394
}
@@ -399,7 +399,7 @@ function toValidPackageName(projectName: string) {
399399
.toLowerCase()
400400
.replace(/\s+/g, '-')
401401
.replace(/^[._]/, '')
402-
.replace(/[^a-z0-9-~]+/g, '-')
402+
.replace(/[^a-z\d\-~]+/g, '-')
403403
}
404404

405405
function copyDir(srcDir: string, destDir: string) {

packages/plugin-react/src/fast-refresh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ if (import.meta.hot) {
5656
RefreshRuntime.register(type, __SOURCE__ + " " + id)
5757
};
5858
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
59-
}`.replace(/[\n]+/gm, '')
59+
}`.replace(/\n+/g, '')
6060

6161
const timeout = `
6262
if (!window.__vite_plugin_react_timeout) {

packages/plugin-react/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
110110
// - import * as React from 'react';
111111
// - import React from 'react';
112112
// - import React, {useEffect} from 'react';
113-
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/
113+
const importReactRE = /(?:^|\n)import\s+(?:\*\s+as\s+)?React(?:,|\s+)/
114114

115115
// Any extension, including compound ones like '.bs.js'
116-
const fileExtensionRE = /\.[^\/\s\?]+$/
116+
const fileExtensionRE = /\.[^/\s?]+$/
117117

118118
const viteBabel: Plugin = {
119119
name: 'vite:react-babel',
@@ -202,7 +202,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
202202
filepath.match(fileExtensionRE) ||
203203
[]
204204

205-
if (/\.(mjs|[tj]sx?)$/.test(extension)) {
205+
if (/\.(?:mjs|[tj]sx?)$/.test(extension)) {
206206
const isJSX = extension.endsWith('x')
207207
const isNodeModules = id.includes('/node_modules/')
208208
const isProjectFile =

packages/plugin-vue/src/handleHotUpdate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { ResolvedOptions } from '.'
1111

1212
const debug = _debug('vite:hmr')
1313

14-
const directRequestRE = /(\?|&)direct\b/
14+
const directRequestRE = /(?:\?|&)direct\b/
1515

1616
/**
1717
* Vite-specific HMR handling
@@ -148,7 +148,7 @@ export async function handleHotUpdate(
148148
affectedModules.add(mainModule)
149149
} else if (mainModule && !affectedModules.has(mainModule)) {
150150
const styleImporters = [...mainModule.importers].filter((m) =>
151-
/\.css($|\?)/.test(m.url)
151+
/\.css(?:$|\?)/.test(m.url)
152152
)
153153
styleImporters.forEach((m) => affectedModules.add(m))
154154
}

packages/vite/src/node/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ export async function resolveConfig(
465465
)
466466

467467
const clientAlias = [
468-
{ find: /^[\/]?@vite\/env/, replacement: () => ENV_ENTRY },
469-
{ find: /^[\/]?@vite\/client/, replacement: () => CLIENT_ENTRY }
468+
{ find: /^\/?@vite\/env/, replacement: () => ENV_ENTRY },
469+
{ find: /^\/?@vite\/client/, replacement: () => CLIENT_ENTRY }
470470
]
471471

472472
// resolve alias with internal client alias

packages/vite/src/node/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export const DEFAULT_CONFIG_FILES = [
4646

4747
export const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/
4848

49-
export const OPTIMIZABLE_ENTRY_RE = /\.(?:[cm]?[jt]s)$/
49+
export const OPTIMIZABLE_ENTRY_RE = /\.[cm]?[jt]s$/
5050

51-
export const SPECIAL_QUERY_RE = /[\?&](?:worker|sharedworker|raw|url)\b/
51+
export const SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/
5252

5353
/**
5454
* Prefix for resolved fs paths, since windows paths may not be valid as URLs.
@@ -129,7 +129,7 @@ export const DEFAULT_ASSETS_RE = new RegExp(
129129
`\\.(` + KNOWN_ASSET_TYPES.join('|') + `)(\\?.*)?$`
130130
)
131131

132-
export const DEP_VERSION_RE = /[\?&](v=[\w\.-]+)\b/
132+
export const DEP_VERSION_RE = /[?&](v=[\w.-]+)\b/
133133

134134
export const loopbackHosts = new Set([
135135
'localhost',

packages/vite/src/node/optimizer/esbuildDepPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export function esbuildCjsExternalPlugin(externals: string[]): Plugin {
265265
name: 'cjs-external',
266266
setup(build) {
267267
const escape = (text: string) =>
268-
`^${text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}$`
268+
`^${text.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')}$`
269269
const filter = new RegExp(externals.map(escape).join('|'))
270270

271271
build.onResolve({ filter: /.*/, namespace: 'external' }, (args) => ({

packages/vite/src/node/optimizer/scan.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const htmlTypesRE = /\.(html|vue|svelte|astro|imba)$/
4040
// since even missed imports can be caught at runtime, and false positives will
4141
// simply be ignored.
4242
export const importsRE =
43-
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm
43+
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm
4444

4545
export async function scanImports(config: ResolvedConfig): Promise<{
4646
deps: Record<string, string>
@@ -149,13 +149,13 @@ function globEntries(pattern: string | string[], config: ResolvedConfig) {
149149
}
150150

151151
const scriptModuleRE =
152-
/(<script\b[^>]*type\s*=\s*(?:"module"|'module')[^>]*>)(.*?)<\/script>/gims
153-
export const scriptRE = /(<script\b(?:\s[^>]*>|>))(.*?)<\/script>/gims
152+
/(<script\b[^>]+type\s*=\s*(?:"module"|'module')[^>]*>)(.*?)<\/script>/gis
153+
export const scriptRE = /(<script(?:\s[^>]*>|>))(.*?)<\/script>/gis
154154
export const commentRE = /<!--.*?-->/gs
155-
const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
156-
const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
157-
const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
158-
const contextRE = /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
155+
const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
156+
const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
157+
const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
158+
const contextRE = /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i
159159

160160
function esbuildScanPlugin(
161161
config: ResolvedConfig,

0 commit comments

Comments
 (0)