From 5300196de05357dafee0c85b26ed7b0151fa3e5a Mon Sep 17 00:00:00 2001 From: sapenlei <2503404258gl@gmail.com> Date: Wed, 18 Dec 2024 16:27:14 +0800 Subject: [PATCH 1/3] fix(node): stripe bom fix #18911 --- packages/vite/src/node/packages.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/packages.ts b/packages/vite/src/node/packages.ts index 468fc4cbb0595b..afed911ef653e8 100644 --- a/packages/vite/src/node/packages.ts +++ b/packages/vite/src/node/packages.ts @@ -174,8 +174,15 @@ export function findNearestMainPackageData( ) } +function stripBOM(content: string) { + if (content.charCodeAt(0) === 0xfeff) { + content = content.slice(1) + } + return content +} + export function loadPackageData(pkgPath: string): PackageData { - const data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')) + const data = JSON.parse(stripBOM(fs.readFileSync(pkgPath, 'utf-8'))) const pkgDir = normalizePath(path.dirname(pkgPath)) const { sideEffects } = data let hasSideEffects: (id: string) => boolean | null From d6930cd83f5e27a3b3fb7f6d510ce9b5ba66290c Mon Sep 17 00:00:00 2001 From: sapenlei <2503404258gl@gmail.com> Date: Wed, 18 Dec 2024 19:52:27 +0800 Subject: [PATCH 2/3] refactor: use stripBomTag instead of stripBom --- packages/vite/src/node/packages.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/packages.ts b/packages/vite/src/node/packages.ts index afed911ef653e8..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' @@ -174,15 +175,8 @@ export function findNearestMainPackageData( ) } -function stripBOM(content: string) { - if (content.charCodeAt(0) === 0xfeff) { - content = content.slice(1) - } - return content -} - export function loadPackageData(pkgPath: string): PackageData { - const data = JSON.parse(stripBOM(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 From 4cc5c2562a756a1a7007dddfe0fb1cb5a642c480 Mon Sep 17 00:00:00 2001 From: sapenlei <2503404258gl@gmail.com> Date: Wed, 18 Dec 2024 19:56:16 +0800 Subject: [PATCH 3/3] test: add packageJson BOM processing test case --- playground/resolve/__tests__/resolve.spec.ts | 4 +++ playground/resolve/index.html | 6 +++++ playground/resolve/package.json | 3 ++- playground/resolve/utf8-bom-package/index.mjs | 1 + .../resolve/utf8-bom-package/package.json | 8 ++++++ pnpm-lock.yaml | 25 +++++++++++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 playground/resolve/utf8-bom-package/index.mjs create mode 100644 playground/resolve/utf8-bom-package/package.json 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
+