Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve): handle package.json with UTF-8 BOM #19000

Merged
merged 3 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isInNodeModules,
normalizePath,
safeRealpathSync,
stripBomTag,
tryStatSync,
} from './utils'
import type { Plugin } from './plugin'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
})
6 changes: 6 additions & 0 deletions playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ <h2>resolve package that contains # in path</h2>
<h2>resolve non normalized absolute path</h2>
<p class="non-normalized"></p>

<h2>utf8-bom-package</h2>
<p class="utf8-bom-package">fail</p>

<script type="module">
import '@generated-content-virtual-file'
function text(selector, text) {
Expand Down Expand Up @@ -399,6 +402,9 @@ <h2>resolve non normalized absolute path</h2>

import nonNormalizedAbsolute from '@non-normalized'
text('.non-normalized', nonNormalizedAbsolute)

import { msg as utf8BomPackage } from '@vitejs/test-utf8-bom-package'
text('.utf8-bom-package', utf8BomPackage)
</script>

<style>
Expand Down
3 changes: 2 additions & 1 deletion playground/resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@vitejs/test-resolve-exports-with-module-condition-required": "link:./exports-with-module-condition-required",
"@vitejs/test-resolve-linked": "workspace:*",
"@vitejs/test-resolve-imports-pkg": "link:./imports-path/other-pkg",
"@vitejs/test-resolve-sharp-dir": "link:./sharp-dir"
"@vitejs/test-resolve-sharp-dir": "link:./sharp-dir",
"@vitejs/test-utf8-bom-package": "link:./utf8-bom-package"
}
}
1 change: 1 addition & 0 deletions playground/resolve/utf8-bom-package/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = '[success]'
8 changes: 8 additions & 0 deletions playground/resolve/utf8-bom-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@vitejs/test-utf8-bom-package",
"private": true,
"version": "1.0.0",
"exports": {
".": "./index.mjs"
}
}
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading