Skip to content

Commit 0910e76

Browse files
committed
perf: replace endsWith with ===
1 parent 46d1352 commit 0910e76

File tree

17 files changed

+205
-37
lines changed

17 files changed

+205
-37
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ import {
1717
createDebugger,
1818
dataUrlRE,
1919
externalRE,
20+
isAstroExt,
21+
isHtmlExt,
2022
isObject,
2123
isOptimizable,
24+
isSvelteExt,
25+
isVueExt,
2226
moduleListContains,
2327
multilineCommentsRE,
2428
normalizePath,
@@ -366,7 +370,7 @@ function esbuildScanPlugin(
366370
let raw = fs.readFileSync(path, 'utf-8')
367371
// Avoid matching the content of the comment
368372
raw = raw.replace(commentRE, '<!---->')
369-
const isHtml = path.endsWith('.html')
373+
const isHtml = isHtmlExt(path)
370374
const regex = isHtml ? scriptModuleRE : scriptRE
371375
regex.lastIndex = 0
372376
let js = ''
@@ -394,7 +398,7 @@ function esbuildScanPlugin(
394398
let loader: Loader = 'js'
395399
if (lang === 'ts' || lang === 'tsx' || lang === 'jsx') {
396400
loader = lang
397-
} else if (path.endsWith('.astro')) {
401+
} else if (isAstroExt(path)) {
398402
loader = 'ts'
399403
}
400404
const srcMatch = openTag.match(srcRE)
@@ -445,7 +449,7 @@ function esbuildScanPlugin(
445449
// Especially for Svelte files, exports in <script context="module"> means module exports,
446450
// exports in <script> means component props. To avoid having two same export name from the
447451
// star exports, we need to ignore exports in <script>
448-
if (path.endsWith('.svelte') && context !== 'module') {
452+
if (isSvelteExt(path) && context !== 'module') {
449453
js += `import ${virtualModulePath}\n`
450454
} else {
451455
js += `export * from ${virtualModulePath}\n`
@@ -457,7 +461,7 @@ function esbuildScanPlugin(
457461
// anywhere in a string. Svelte and Astro files can't have
458462
// `export default` as code so we know if it's encountered it's a
459463
// false positive (e.g. contained in a string)
460-
if (!path.endsWith('.vue') || !js.includes('export default')) {
464+
if (!isVueExt(path) || !js.includes('export default')) {
461465
js += '\nexport default {}'
462466
}
463467

packages/vite/src/node/packages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33
import { createRequire } from 'node:module'
4-
import { createFilter, safeRealpathSync } from './utils'
4+
import { createFilter, isJsonExt, safeRealpathSync } from './utils'
55
import type { ResolvedConfig } from './config'
66
import type { Plugin } from './plugin'
77

@@ -204,7 +204,7 @@ export function watchPackageDataPlugin(config: ResolvedConfig): Plugin {
204204
const { packageCache } = config
205205
const setPackageData = packageCache.set.bind(packageCache)
206206
packageCache.set = (id, pkg) => {
207-
if (id.endsWith('.json')) {
207+
if (isJsonExt(id)) {
208208
watchFile(id)
209209
}
210210
return setPackageData(id, pkg)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import type { ResolvedConfig } from '../config'
1919
import {
2020
cleanUrl,
2121
getHash,
22+
isHtmlExt,
23+
isSvgExt,
2224
joinUrlSegments,
2325
normalizePath,
2426
removeLeadingSlash,
@@ -333,8 +335,8 @@ async function fileToBuiltUrl(
333335
let url: string
334336
if (
335337
config.build.lib ||
336-
(!file.endsWith('.svg') &&
337-
!file.endsWith('.html') &&
338+
(!isSvgExt(file) &&
339+
!isHtmlExt(file) &&
338340
content.length < Number(config.build.assetsInlineLimit) &&
339341
!isGitLfsPlaceholder(content))
340342
) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
createFilter,
1919
ensureWatchedFile,
2020
generateCodeFrame,
21+
isJsonExt,
2122
} from '../utils'
2223
import type { ResolvedConfig, ViteDevServer } from '..'
2324
import type { Plugin } from '../plugin'
@@ -481,8 +482,7 @@ function reloadOnTsconfigChange(changedFile: string) {
481482
// any json file in the tsconfig cache could have been used to compile ts
482483
if (
483484
path.basename(changedFile) === 'tsconfig.json' ||
484-
(changedFile.endsWith('.json') &&
485-
tsconfckParseOptions?.cache?.has(changedFile))
485+
(isJsonExt(changedFile) && tsconfckParseOptions?.cache?.has(changedFile))
486486
) {
487487
server.config.logger.info(
488488
`changed tsconfig file detected: ${changedFile} - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.`,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getHash,
1919
isDataUrl,
2020
isExternalUrl,
21+
isHtmlExt,
2122
isUrl,
2223
normalizePath,
2324
processSrcSet,
@@ -303,7 +304,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
303304
name: 'vite:build-html',
304305

305306
async transform(html, id) {
306-
if (id.endsWith('.html')) {
307+
if (isHtmlExt(id)) {
307308
const relativeUrlPath = path.posix.relative(
308309
config.root,
309310
normalizePath(id),

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import {
3333
isDataUrl,
3434
isExternalUrl,
3535
isJSRequest,
36+
isJsonExt,
37+
isJsxExt,
38+
isTsxExt,
39+
isVueExt,
3640
joinUrlSegments,
3741
moduleListContains,
3842
normalizePath,
@@ -223,8 +227,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
223227
try {
224228
;[imports, exports] = parseImports(source)
225229
} catch (e: any) {
226-
const isVue = importer.endsWith('.vue')
227-
const isJsx = importer.endsWith('.jsx') || importer.endsWith('.tsx')
230+
const isVue = isVueExt(importer)
231+
const isJsx = isJsxExt(importer) || isTsxExt(importer)
228232
const maybeJSX = !isVue && isJSRequest(importer)
229233

230234
const msg = isVue
@@ -498,7 +502,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
498502
if (
499503
specifier[0] === '/' &&
500504
!config.assetsInclude(cleanUrl(specifier)) &&
501-
!specifier.endsWith('.json') &&
505+
!isJsonExt(specifier) &&
502506
checkPublicFile(specifier, config)
503507
) {
504508
throw new Error(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
bareImportRE,
1010
cleanUrl,
1111
combineSourcemaps,
12+
isCssExt,
1213
isDataUrl,
1314
isExternalUrl,
1415
moduleListContains,
@@ -527,7 +528,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
527528
const cssDeps: string[] = []
528529
const otherDeps: string[] = []
529530
for (const dep of depsArray) {
530-
;(dep.endsWith('.css') ? cssDeps : otherDeps).push(dep)
531+
;(isCssExt(dep) ? cssDeps : otherDeps).push(dep)
531532
}
532533
resolvedDeps = [
533534
...resolveDependencies(normalizedFile, otherDeps, {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { promisify } from 'node:util'
44
import colors from 'picocolors'
55
import type { Plugin } from 'rollup'
66
import type { ResolvedConfig } from '../config'
7-
import { isDefined, normalizePath } from '../utils'
7+
import { isCssExt, isDefined, isSourceMapExt, normalizePath } from '../utils'
88
import { LogLevels } from '../logger'
99

1010
const groups = [
@@ -142,8 +142,8 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
142142
mapSize: chunk.map ? chunk.map.toString().length : null,
143143
}
144144
} else {
145-
if (chunk.fileName.endsWith('.map')) return null
146-
const isCSS = chunk.fileName.endsWith('.css')
145+
if (isSourceMapExt(chunk.fileName)) return null
146+
const isCSS = isCssExt(chunk.fileName)
147147
return {
148148
name: chunk.fileName,
149149
group: isCSS ? 'CSS' : 'Assets',

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import {
2727
isBuiltin,
2828
isDataUrl,
2929
isExternalUrl,
30+
isHtmlExt,
31+
isMjsExt,
3032
isNonDriveRelativeAbsolutePath,
3133
isObject,
3234
isOptimizable,
@@ -281,7 +283,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
281283
// relative
282284
if (
283285
id[0] === '.' ||
284-
((preferRelative || importer?.endsWith('.html')) &&
286+
((preferRelative || isHtmlExt(importer)) &&
285287
startsWithWordCharRE.test(id))
286288
) {
287289
const basedir = importer ? path.dirname(importer) : process.cwd()
@@ -962,7 +964,7 @@ export function resolvePackageEntry(
962964
if (
963965
targetWeb &&
964966
options.browserField &&
965-
(!entryPoint || entryPoint.endsWith('.mjs'))
967+
(!entryPoint || isMjsExt(entryPoint))
966968
) {
967969
// check browser field
968970
// https://github.com/defunctzombie/package-browser-field-spec
@@ -1006,7 +1008,7 @@ export function resolvePackageEntry(
10061008

10071009
// fallback to mainFields if still not resolved
10081010
// TODO: review if `.mjs` check is still needed
1009-
if (!resolvedFromExports && (!entryPoint || entryPoint.endsWith('.mjs'))) {
1011+
if (!resolvedFromExports && (!entryPoint || isMjsExt(entryPoint))) {
10101012
for (const field of options.mainFields) {
10111013
if (field === 'browser') continue // already checked above
10121014
if (typeof data[field] === 'string') {

packages/vite/src/node/server/hmr.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import colors from 'picocolors'
55
import type { Update } from 'types/hmrPayload'
66
import type { RollupError } from 'rollup'
77
import { CLIENT_DIR } from '../constants'
8-
import { createDebugger, normalizePath, unique, wrapId } from '../utils'
8+
import {
9+
createDebugger,
10+
isHtmlExt,
11+
normalizePath,
12+
unique,
13+
wrapId,
14+
} from '../utils'
915
import type { ViteDevServer } from '..'
1016
import { isCSSRequest } from '../plugins/css'
1117
import { getAffectedGlobModules } from '../plugins/importMetaGlob'
@@ -110,7 +116,7 @@ export async function handleHMRUpdate(
110116

111117
if (!hmrContext.modules.length) {
112118
// html file cannot be hot updated
113-
if (file.endsWith('.html')) {
119+
if (isHtmlExt(file)) {
114120
config.logger.info(colors.green(`page reload `) + colors.dim(shortFile), {
115121
clear: true,
116122
timestamp: true,

0 commit comments

Comments
 (0)