Skip to content

Commit eb12604

Browse files
authored
refactor: match import glob common base by path segment correctly (#22558)
1 parent 1298951 commit eb12604

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

packages/vite/src/node/__tests__/plugins/importGlob/utils.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ describe('getCommonBase()', async () => {
88
it('common base', () => {
99
expect(getCommonBase(['/a/b/**/*.vue', '/a/b/**/*.jsx'])).toBe('/a/b')
1010
})
11+
it('does not treat a path prefix as a common directory', () => {
12+
expect(getCommonBase(['/a/foo/*.js', '/a/foobar/*.js'])).toBe('/a')
13+
})
1114
it('static file', () => {
1215
expect(
1316
getCommonBase(['/a/b/**/*.vue', '/a/b/**/*.jsx', '/a/b/foo.js']),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,11 @@ export function getCommonBase(globsResolved: string[]): null | string {
702702
const dirS = bases[0].split('/')
703703
for (let i = 0; i < dirS.length; i++) {
704704
const candidate = dirS.slice(0, i + 1).join('/')
705-
if (bases.every((base) => base.startsWith(candidate)))
705+
if (
706+
bases.every(
707+
(base) => base === candidate || base.startsWith(`${candidate}/`),
708+
)
709+
)
706710
commonAncestor = candidate
707711
else break
708712
}

0 commit comments

Comments
 (0)