Skip to content

Commit da43936

Browse files
authored
perf: reuse regex in plugins (#12518)
1 parent abc2b9c commit da43936

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

packages/vite/src/node/plugins/css.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,8 @@ export async function preprocessCSS(
10741074
return await compileCSS(filename, code, config)
10751075
}
10761076

1077+
const postcssReturnsVirtualFilesRE = /^<.+>$/
1078+
10771079
export async function formatPostcssSourceMap(
10781080
rawMap: ExistingRawSourceMap,
10791081
file: string,
@@ -1083,8 +1085,7 @@ export async function formatPostcssSourceMap(
10831085
const sources = rawMap.sources.map((source) => {
10841086
const cleanSource = cleanUrl(decodeURIComponent(source))
10851087

1086-
// postcss returns virtual files
1087-
if (/^<.+>$/.test(cleanSource)) {
1088+
if (postcssReturnsVirtualFilesRE.test(cleanSource)) {
10881089
return `\0${cleanSource}`
10891090
}
10901091

packages/vite/src/node/plugins/dynamicImportVars.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { toAbsoluteGlob } from './importMetaGlob'
1919

2020
export const dynamicImportHelperId = '\0vite/dynamic-import-helper'
2121

22+
const relativePathRE = /^\.{1,2}\//
2223
interface DynamicImportRequest {
2324
as?: keyof KnownAsTypeMap
2425
}
@@ -122,7 +123,7 @@ export async function transformDynamicImport(
122123
await toAbsoluteGlob(rawPattern, root, importer, resolve),
123124
)
124125

125-
if (!/^\.{1,2}\//.test(newRawPattern)) {
126+
if (!relativePathRE.test(newRawPattern)) {
126127
newRawPattern = `./${newRawPattern}`
127128
}
128129

packages/vite/src/node/plugins/esbuild.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const INJECT_HELPERS_IIFE_RE =
3030
const INJECT_HELPERS_UMD_RE =
3131
/^(.*?)(\(function\([^)]*\)\s*\{.+?amd.+?function\([^)]*\)\s*\{.*?"use strict";)/s
3232

33+
const validExtensionRE = /\.\w+$/
34+
const jsxExtensionsRE = /\.(?:j|t)sx\b/
35+
3336
let server: ViteDevServer
3437

3538
export interface ESBuildOptions extends TransformOptions {
@@ -75,7 +78,7 @@ export async function transformWithEsbuild(
7578
// if the id ends with a valid ext, use it (e.g. vue blocks)
7679
// otherwise, cleanup the query before checking the ext
7780
const ext = path
78-
.extname(/\.\w+$/.test(filename) ? filename : cleanUrl(filename))
81+
.extname(validExtensionRE.test(filename) ? filename : cleanUrl(filename))
7982
.slice(1)
8083

8184
if (ext === 'cjs' || ext === 'mjs') {
@@ -247,7 +250,7 @@ export function esbuildPlugin(options: ESBuildOptions): Plugin {
247250
this.warn(prettifyMessage(m, code))
248251
})
249252
}
250-
if (jsxInject && /\.(?:j|t)sx\b/.test(id)) {
253+
if (jsxInject && jsxExtensionsRE.test(id)) {
251254
result.code = jsxInject + ';' + result.code
252255
}
253256
return {

packages/vite/src/node/plugins/importAnalysis.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ export const canSkipImportAnalysis = (id: string): boolean =>
7878
const optimizedDepChunkRE = /\/chunk-[A-Z\d]{8}\.js/
7979
const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/
8080

81+
const hasImportInQueryParamsRE = /[?&]import=?\b/
82+
83+
const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//
84+
85+
const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
86+
const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/
87+
8188
export function isExplicitImportRequired(url: string): boolean {
8289
return !isJSRequest(cleanUrl(url)) && !isCSSRequest(url)
8390
}
@@ -373,7 +380,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
373380
// query can break 3rd party plugin's extension checks.
374381
if (
375382
(isRelative || isSelfImport) &&
376-
!/[?&]import=?\b/.test(url) &&
383+
!hasImportInQueryParamsRE.test(url) &&
377384
!url.match(DEP_VERSION_RE)
378385
) {
379386
const versionMatch = importer.match(DEP_VERSION_RE)
@@ -613,7 +620,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
613620
} else if (!importer.startsWith(clientDir)) {
614621
if (!importer.includes('node_modules')) {
615622
// check @vite-ignore which suppresses dynamic import warning
616-
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(
623+
const hasViteIgnore = hasViteIgnoreRE.test(
617624
// complete expression inside parens
618625
source.slice(dynamicIndex + 1, end),
619626
)
@@ -637,11 +644,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
637644
}
638645

639646
if (!ssr) {
640-
const url = rawUrl
641-
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '')
642-
.trim()
647+
const url = rawUrl.replace(cleanUpRawUrlRE, '').trim()
643648
if (
644-
!/^(?:'.*'|".*"|`.*`)$/.test(url) ||
649+
!urlIsStringRE.test(url) ||
645650
isExplicitImportRequired(url.slice(1, -1))
646651
) {
647652
needQueryInjectHelper = true

packages/vite/src/node/plugins/resolve.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export const optionalPeerDepId = '__vite-optional-peer-dep'
6262
const nodeModulesInPathRE = /(?:^|\/)node_modules\//
6363
const subpathImportsPrefix = '#'
6464

65+
const startsWithWordCharRE = /^\w/
66+
6567
const isDebug = process.env.DEBUG
6668
const debug = createDebugger('vite:resolve-details', {
6769
onlyWhenFocused: true,
@@ -279,7 +281,8 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
279281
// relative
280282
if (
281283
id.startsWith('.') ||
282-
((preferRelative || importer?.endsWith('.html')) && /^\w/.test(id))
284+
((preferRelative || importer?.endsWith('.html')) &&
285+
startsWithWordCharRE.test(id))
283286
) {
284287
const basedir = importer ? path.dirname(importer) : process.cwd()
285288
const fsPath = path.resolve(basedir, id)

0 commit comments

Comments
 (0)