Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reuse regex in plugins #12518

Merged
merged 1 commit into from Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -1074,6 +1074,8 @@ export async function preprocessCSS(
return await compileCSS(filename, code, config)
}

const postcssReturnsVirtualFilesRE = /^<.+>$/

export async function formatPostcssSourceMap(
rawMap: ExistingRawSourceMap,
file: string,
Expand All @@ -1083,8 +1085,7 @@ export async function formatPostcssSourceMap(
const sources = rawMap.sources.map((source) => {
const cleanSource = cleanUrl(decodeURIComponent(source))

// postcss returns virtual files
if (/^<.+>$/.test(cleanSource)) {
if (postcssReturnsVirtualFilesRE.test(cleanSource)) {
return `\0${cleanSource}`
}

Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/dynamicImportVars.ts
Expand Up @@ -19,6 +19,7 @@ import { toAbsoluteGlob } from './importMetaGlob'

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

const relativePathRE = /^\.{1,2}\//
interface DynamicImportRequest {
as?: keyof KnownAsTypeMap
}
Expand Down Expand Up @@ -122,7 +123,7 @@ export async function transformDynamicImport(
await toAbsoluteGlob(rawPattern, root, importer, resolve),
)

if (!/^\.{1,2}\//.test(newRawPattern)) {
if (!relativePathRE.test(newRawPattern)) {
newRawPattern = `./${newRawPattern}`
}

Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -30,6 +30,9 @@ const INJECT_HELPERS_IIFE_RE =
const INJECT_HELPERS_UMD_RE =
/^(.*?)(\(function\([^)]*\)\s*\{.+?amd.+?function\([^)]*\)\s*\{.*?"use strict";)/s

const validExtensionRE = /\.\w+$/
const jsxExtensionsRE = /\.(?:j|t)sx\b/

let server: ViteDevServer

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

if (ext === 'cjs' || ext === 'mjs') {
Expand Down Expand Up @@ -241,7 +244,7 @@ export function esbuildPlugin(options: ESBuildOptions): Plugin {
this.warn(prettifyMessage(m, code))
})
}
if (jsxInject && /\.(?:j|t)sx\b/.test(id)) {
if (jsxInject && jsxExtensionsRE.test(id)) {
result.code = jsxInject + ';' + result.code
}
return {
Expand Down
17 changes: 11 additions & 6 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -78,6 +78,13 @@ export const canSkipImportAnalysis = (id: string): boolean =>
const optimizedDepChunkRE = /\/chunk-[A-Z\d]{8}\.js/
const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/

const hasImportInQueryParamsRE = /[?&]import=?\b/

const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//

const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/

export function isExplicitImportRequired(url: string): boolean {
return !isJSRequest(cleanUrl(url)) && !isCSSRequest(url)
}
Expand Down Expand Up @@ -373,7 +380,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// query can break 3rd party plugin's extension checks.
if (
(isRelative || isSelfImport) &&
!/[?&]import=?\b/.test(url) &&
!hasImportInQueryParamsRE.test(url) &&
!url.match(DEP_VERSION_RE)
) {
const versionMatch = importer.match(DEP_VERSION_RE)
Expand Down Expand Up @@ -613,7 +620,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
} else if (!importer.startsWith(clientDir)) {
if (!importer.includes('node_modules')) {
// check @vite-ignore which suppresses dynamic import warning
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(
const hasViteIgnore = hasViteIgnoreRE.test(
// complete expression inside parens
source.slice(dynamicIndex + 1, end),
)
Expand All @@ -637,11 +644,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}

if (!ssr) {
const url = rawUrl
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '')
.trim()
const url = rawUrl.replace(cleanUpRawUrlRE, '').trim()
if (
!/^(?:'.*'|".*"|`.*`)$/.test(url) ||
!urlIsStringRE.test(url) ||
isExplicitImportRequired(url.slice(1, -1))
) {
needQueryInjectHelper = true
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -57,6 +57,8 @@ export const optionalPeerDepId = '__vite-optional-peer-dep'
const nodeModulesInPathRE = /(?:^|\/)node_modules\//
const subpathImportsPrefix = '#'

const startsWithWordCharRE = /^\w/

const isDebug = process.env.DEBUG
const debug = createDebugger('vite:resolve-details', {
onlyWhenFocused: true,
Expand Down Expand Up @@ -277,7 +279,8 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
// relative
if (
id.startsWith('.') ||
((preferRelative || importer?.endsWith('.html')) && /^\w/.test(id))
((preferRelative || importer?.endsWith('.html')) &&
startsWithWordCharRE.test(id))
) {
const basedir = importer ? path.dirname(importer) : process.cwd()
const fsPath = path.resolve(basedir, id)
Expand Down