Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
asyncFlatten,
bareImportRE,
combineSourcemaps,
deepImportRE,
extractHostnamesFromSubjectAltName,
flattenId,
generateCodeFrame,
Expand Down Expand Up @@ -44,6 +45,34 @@ describe('bareImportRE', () => {
})
})

describe('deepImportRE', () => {
test('should match regular package with subpath', () => {
const match = deepImportRE.exec('my-lib/schemas')
expect(match?.[1]).toBe('my-lib')
expect(match?.[2]).toBeUndefined()
})

test('should match scoped package with subpath', () => {
expect(deepImportRE.test('@vitejs/plugin-vue/dist/index')).toBe(true)
const match = deepImportRE.exec('@vitejs/plugin-vue/dist/index')
expect(match?.[1]).toBeUndefined()
expect(match?.[2]).toBe('@vitejs/plugin-vue')
})

test('should match @/ scoped package with subpath', () => {
expect(deepImportRE.test('@/lib/schemas')).toBe(true)
const match = deepImportRE.exec('@/lib/schemas')
expect(match?.[1]).toBeUndefined()
expect(match?.[2]).toBe('@/lib')
})

test('should not match package without subpath', () => {
expect(deepImportRE.test('my-lib')).toBe(false)
expect(deepImportRE.test('@vitejs/plugin-vue')).toBe(false)
expect(deepImportRE.test('@/lib')).toBe(false)
})
})

describe('injectQuery', () => {
if (isWindows) {
// this test will work incorrectly on unix systems
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export function isOptimizable(
}

export const bareImportRE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/
export const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//
export const deepImportRE = /^([^@][^/]*)\/|^(@[^/]*\/[^/]+)\//

// TODO: use import()
const _require = createRequire(/** #__KEEP__ */ import.meta.url)
Expand Down