diff --git a/packages/vite/src/node/packages.ts b/packages/vite/src/node/packages.ts index 468fc4cbb0595b..5d50f285ee3fa5 100644 --- a/packages/vite/src/node/packages.ts +++ b/packages/vite/src/node/packages.ts @@ -6,6 +6,7 @@ import { isInNodeModules, normalizePath, safeRealpathSync, + stripBomTag, tryStatSync, } from './utils' import type { Plugin } from './plugin' @@ -175,7 +176,7 @@ export function findNearestMainPackageData( } export function loadPackageData(pkgPath: string): PackageData { - const data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')) + const data = JSON.parse(stripBomTag(fs.readFileSync(pkgPath, 'utf-8'))) const pkgDir = normalizePath(path.dirname(pkgPath)) const { sideEffects } = data let hasSideEffects: (id: string) => boolean | null diff --git a/playground/resolve/__tests__/resolve.spec.ts b/playground/resolve/__tests__/resolve.spec.ts index e12ddecedb5c0d..ba5e6441dc7edb 100644 --- a/playground/resolve/__tests__/resolve.spec.ts +++ b/playground/resolve/__tests__/resolve.spec.ts @@ -248,3 +248,7 @@ test.runIf(isBuild)('public dir is not copied', async () => { fs.existsSync(path.resolve(testDir, 'dist/should-not-be-copied')), ).toBe(false) }) + +test('import utf8-bom package', async () => { + expect(await page.textContent('.utf8-bom-package')).toMatch('[success]') +}) diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 8c315f74dcd2c1..f1480b0a9b3e52 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -176,6 +176,9 @@
fail
+