diff --git a/packages/playground/resolve/__tests__/resolve.spec.ts b/packages/playground/resolve/__tests__/resolve.spec.ts
index 0bb7c238e77893..0b0f1c7fdc8896 100644
--- a/packages/playground/resolve/__tests__/resolve.spec.ts
+++ b/packages/playground/resolve/__tests__/resolve.spec.ts
@@ -1,5 +1,9 @@
import { isBuild } from '../../testUtils'
+test('bom import', async () => {
+ expect(await page.textContent('.utf8-bom')).toMatch('[success]')
+})
+
test('deep import', async () => {
expect(await page.textContent('.deep-import')).toMatch('[2,4]')
})
diff --git a/packages/playground/resolve/index.html b/packages/playground/resolve/index.html
index 722d66de5340ff..aada0bd1bdd972 100644
--- a/packages/playground/resolve/index.html
+++ b/packages/playground/resolve/index.html
@@ -1,5 +1,8 @@
Resolve
+Utf8-bom import
+fail
+
Deep import
Should show [2,4]:fail
@@ -62,6 +65,10 @@ resolve.conditions
document.querySelector(selector).textContent = text
}
+ // import from a utf-8 bom file
+ import { msg as bomMsg } from './utf8-bom/main.js'
+ text('.utf8-bom', bomMsg)
+
// deep import
import slicedToArray from '@babel/runtime/helpers/esm/slicedToArray'
diff --git a/packages/playground/resolve/utf8-bom/main.js b/packages/playground/resolve/utf8-bom/main.js
new file mode 100644
index 00000000000000..6273e5ac04e016
--- /dev/null
+++ b/packages/playground/resolve/utf8-bom/main.js
@@ -0,0 +1,3 @@
+import '@babel/runtime/helpers/esm/slicedToArray'
+
+export const msg = '[success]'
diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts
index 040558b9f79645..cf83bdb6419e2d 100644
--- a/packages/vite/src/node/plugins/importAnalysis.ts
+++ b/packages/vite/src/node/plugins/importAnalysis.ts
@@ -112,6 +112,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const rewriteStart = Date.now()
await init
let imports: readonly ImportSpecifier[] = []
+ // strip UTF-8 BOM
+ if (source.charCodeAt(0) === 0xfeff) {
+ source = source.slice(1)
+ }
try {
imports = parseImports(source)[0]
} catch (e) {