Skip to content

Commit 902567a

Browse files
authored
fix(resolve): handle package.json with UTF-8 BOM (#19000)
1 parent 9f10261 commit 902567a

File tree

7 files changed

+48
-2
lines changed

7 files changed

+48
-2
lines changed

packages/vite/src/node/packages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isInNodeModules,
77
normalizePath,
88
safeRealpathSync,
9+
stripBomTag,
910
tryStatSync,
1011
} from './utils'
1112
import type { Plugin } from './plugin'
@@ -175,7 +176,7 @@ export function findNearestMainPackageData(
175176
}
176177

177178
export function loadPackageData(pkgPath: string): PackageData {
178-
const data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
179+
const data = JSON.parse(stripBomTag(fs.readFileSync(pkgPath, 'utf-8')))
179180
const pkgDir = normalizePath(path.dirname(pkgPath))
180181
const { sideEffects } = data
181182
let hasSideEffects: (id: string) => boolean | null

playground/resolve/__tests__/resolve.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,7 @@ test.runIf(isBuild)('public dir is not copied', async () => {
248248
fs.existsSync(path.resolve(testDir, 'dist/should-not-be-copied')),
249249
).toBe(false)
250250
})
251+
252+
test('import utf8-bom package', async () => {
253+
expect(await page.textContent('.utf8-bom-package')).toMatch('[success]')
254+
})

playground/resolve/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ <h2>resolve package that contains # in path</h2>
176176
<h2>resolve non normalized absolute path</h2>
177177
<p class="non-normalized"></p>
178178

179+
<h2>utf8-bom-package</h2>
180+
<p class="utf8-bom-package">fail</p>
181+
179182
<script type="module">
180183
import '@generated-content-virtual-file'
181184
function text(selector, text) {
@@ -399,6 +402,9 @@ <h2>resolve non normalized absolute path</h2>
399402

400403
import nonNormalizedAbsolute from '@non-normalized'
401404
text('.non-normalized', nonNormalizedAbsolute)
405+
406+
import { msg as utf8BomPackage } from '@vitejs/test-utf8-bom-package'
407+
text('.utf8-bom-package', utf8BomPackage)
402408
</script>
403409

404410
<style>

playground/resolve/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@vitejs/test-resolve-exports-with-module-condition-required": "link:./exports-with-module-condition-required",
4141
"@vitejs/test-resolve-linked": "workspace:*",
4242
"@vitejs/test-resolve-imports-pkg": "link:./imports-path/other-pkg",
43-
"@vitejs/test-resolve-sharp-dir": "link:./sharp-dir"
43+
"@vitejs/test-resolve-sharp-dir": "link:./sharp-dir",
44+
"@vitejs/test-utf8-bom-package": "link:./utf8-bom-package"
4445
}
4546
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msg = '[success]'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@vitejs/test-utf8-bom-package",
3+
"private": true,
4+
"version": "1.0.0",
5+
"exports": {
6+
".": "./index.mjs"
7+
}
8+
}

pnpm-lock.yaml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)