diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 2ee69665c49ae7..4b80e20814b82f 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -3,6 +3,7 @@ import path from 'node:path' import { describe, expect, test } from 'vitest' import { asyncFlatten, + bareImportRE, getHash, getLocalhostAddressIfDiffersFromDNS, injectQuery, @@ -13,6 +14,25 @@ import { resolveHostname, } from '../utils' +describe('bareImportRE', () => { + test('should work with normal package name', () => { + expect(bareImportRE.test('vite')).toBe(true) + }) + test('should work with scoped package name', () => { + expect(bareImportRE.test('@vitejs/plugin-vue')).toBe(true) + }) + + test('should work with absolute paths', () => { + expect(bareImportRE.test('/foo')).toBe(false) + expect(bareImportRE.test('C:/foo')).toBe(false) + expect(bareImportRE.test('C:\\foo')).toBe(false) + }) + test('should work with relative path', () => { + expect(bareImportRE.test('./foo')).toBe(false) + expect(bareImportRE.test('.\\foo')).toBe(false) + }) +}) + describe('injectQuery', () => { if (isWindows) { // this test will work incorrectly on unix systems diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index effea7fb34a126..990cc6aad92cb3 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -137,7 +137,7 @@ export function isOptimizable( ) } -export const bareImportRE = /^[\w@](?!.*:\/\/)/ +export const bareImportRE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/ export const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\// // TODO: use import()