Skip to content

Commit 6113a96

Browse files
authored
refactor: restore endsWith usage (#19554)
1 parent c0d3667 commit 6113a96

File tree

8 files changed

+12
-15
lines changed

8 files changed

+12
-15
lines changed

packages/plugin-legacy/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function joinUrlSegments(a: string, b: string): string {
9999
if (!a || !b) {
100100
return a || b || ''
101101
}
102-
if (a[a.length - 1] === '/') {
102+
if (a.endsWith('/')) {
103103
a = a.substring(0, a.length - 1)
104104
}
105105
if (b[0] !== '/') {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ export async function formatPostcssSourceMap(
17031703
const cleanSource = cleanUrl(decodeURIComponent(source))
17041704

17051705
// postcss virtual files
1706-
if (cleanSource[0] === '<' && cleanSource[cleanSource.length - 1] === '>') {
1706+
if (cleanSource[0] === '<' && cleanSource.endsWith('>')) {
17071707
return `\0${cleanSource}`
17081708
}
17091709

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
439439
let url = name
440440
if (!url) {
441441
const rawUrl = code.slice(start, end)
442-
if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
442+
if (rawUrl[0] === `"` && rawUrl.endsWith(`"`))
443443
url = rawUrl.slice(1, -1)
444444
}
445445
if (!url) continue
@@ -516,7 +516,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
516516
let url = name
517517
if (!url) {
518518
const rawUrl = code.slice(start, end)
519-
if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
519+
if (rawUrl[0] === `"` && rawUrl.endsWith(`"`))
520520
url = rawUrl.slice(1, -1)
521521
}
522522
const deps = new Set<string>()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ export function tryNodeResolve(
716716
importer &&
717717
path.isAbsolute(importer) &&
718718
// css processing appends `*` for importer
719-
(importer[importer.length - 1] === '*' || fs.existsSync(cleanUrl(importer)))
719+
(importer.endsWith('*') || fs.existsSync(cleanUrl(importer)))
720720
) {
721721
basedir = path.dirname(importer)
722722
} else {

packages/vite/src/node/server/middlewares/htmlFallback.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function htmlFallbackMiddleware(
4242
}
4343
}
4444
// trailing slash should check for fallback index.html
45-
else if (pathname[pathname.length - 1] === '/') {
45+
else if (pathname.endsWith('/')) {
4646
const filePath = path.join(root, pathname, 'index.html')
4747
if (fs.existsSync(filePath)) {
4848
const newUrl = url + 'index.html'

packages/vite/src/node/server/middlewares/static.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function serveStaticMiddleware(
117117
// also skip internal requests `/@fs/ /@vite-client` etc...
118118
const cleanedUrl = cleanUrl(req.url!)
119119
if (
120-
cleanedUrl[cleanedUrl.length - 1] === '/' ||
120+
cleanedUrl.endsWith('/') ||
121121
path.extname(cleanedUrl) === '.html' ||
122122
isInternalRequest(req.url!) ||
123123
// skip url starting with // as these will be interpreted as
@@ -151,10 +151,7 @@ export function serveStaticMiddleware(
151151

152152
const resolvedPathname = redirectedPathname || pathname
153153
let fileUrl = path.resolve(dir, removeLeadingSlash(resolvedPathname))
154-
if (
155-
resolvedPathname[resolvedPathname.length - 1] === '/' &&
156-
fileUrl[fileUrl.length - 1] !== '/'
157-
) {
154+
if (resolvedPathname.endsWith('/') && fileUrl[fileUrl.length - 1] !== '/') {
158155
fileUrl = withTrailingSlash(fileUrl)
159156
}
160157
if (!ensureServingAccess(fileUrl, server, res, next)) {

packages/vite/src/node/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1359,8 +1359,8 @@ function normalizeSingleAlias({
13591359
}: Alias): Alias {
13601360
if (
13611361
typeof find === 'string' &&
1362-
find[find.length - 1] === '/' &&
1363-
replacement[replacement.length - 1] === '/'
1362+
find.endsWith('/') &&
1363+
replacement.endsWith('/')
13641364
) {
13651365
find = find.slice(0, find.length - 1)
13661366
replacement = replacement.slice(0, replacement.length - 1)
@@ -1458,7 +1458,7 @@ export function joinUrlSegments(a: string, b: string): string {
14581458
if (!a || !b) {
14591459
return a || b || ''
14601460
}
1461-
if (a[a.length - 1] === '/') {
1461+
if (a.endsWith('/')) {
14621462
a = a.substring(0, a.length - 1)
14631463
}
14641464
if (b[0] !== '/') {

playground/fs-serve/root/src/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h2>Denied</h2>
5858
if (!a || !b) {
5959
return a || b || ''
6060
}
61-
if (a[a.length - 1] === '/') {
61+
if (a.endsWith('/')) {
6262
a = a.substring(0, a.length - 1)
6363
}
6464
if (b[0] !== '/') {

0 commit comments

Comments
 (0)