Skip to content

Commit 339f300

Browse files
bluwysapphi-red
andauthored
refactor!: remove resolvePackageEntry and resolvePackageData APIs (#14584)
Co-authored-by: 翠 / green <[email protected]>
1 parent cd82648 commit 339f300

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

docs/guide/migration.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ CLI shortcuts, like `r` to restart the dev server, now require an additional `En
4040

4141
This change prevents Vite from swallowing and controlling OS-specific shortcuts, allowing better compatibility when combining the Vite dev server with other processes, and avoids the [previous caveats](https://github.com/vitejs/vite/pull/14342).
4242

43+
### Remove `resolvePackageEntry` and `resolvePackageData` APIs
44+
45+
The `resolvePackageEntry` and `resolvePackageData` APIs are removed as they exposed Vite's internals and blocked potential Vite 4.3 optimizations in the past. These APIs can be replaced with third-party packages, for example:
46+
47+
- `resolvePackageEntry`: [`import.meta.resolve`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve) or the [`import-meta-resolve`](https://github.com/wooorm/import-meta-resolve) package.
48+
- `resolvePackageData`: Same as above, and crawl up the package directory to get the root `package.json`. Or use the community [`vitefu`](https://github.com/svitejs/vitefu) package.
49+
50+
```js
51+
import { resolve } from 'import-meta-env'
52+
import { findDepPkgJsonPath } from 'vitefu'
53+
import fs from 'node:fs'
54+
55+
const pkg = 'my-lib'
56+
const basedir = process.cwd()
57+
58+
// `resolvePackageEntry`:
59+
const packageEntry = resolve(pkg, basedir)
60+
61+
// `resolvePackageData`:
62+
const packageJsonPath = findDepPkgJsonPath(pkg, basedir)
63+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
64+
```
65+
4366
## Removed Deprecated APIs
4467

4568
- Default exports of CSS files (e.g `import style from './foo.css'`): Use the `?inline` query instead

packages/vite/index.cjs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ asyncFunctions.forEach((name) => {
2525
import('./dist/node/index.js').then((i) => i[name](...args))
2626
})
2727

28-
// some sync functions are marked not supported due to their complexity and uncommon usage
29-
const unsupportedCJS = ['resolvePackageEntry', 'resolvePackageData']
30-
unsupportedCJS.forEach((name) => {
31-
module.exports[name] = () => {
32-
throw new Error(
33-
`"${name}" is not supported in CJS build of Vite 4.\nPlease use ESM or dynamic imports \`const { ${name} } = await import('vite')\`.`,
34-
)
35-
}
36-
})
37-
3828
function warnCjsUsage() {
3929
if (process.env.VITE_CJS_IGNORE_WARNING) return
4030
globalThis.__vite_cjs_skip_clear_screen = true

packages/vite/src/node/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export { build } from './build'
88
export { optimizeDeps } from './optimizer'
99
export { formatPostcssSourceMap, preprocessCSS } from './plugins/css'
1010
export { transformWithEsbuild } from './plugins/esbuild'
11-
export { resolvePackageEntry } from './plugins/resolve'
12-
export { resolvePackageData } from './packages'
1311
export { buildErrorMessage } from './server/middlewares/error'
1412
export * from './publicUtils'
1513

@@ -54,7 +52,6 @@ export type {
5452
SSRTarget,
5553
} from './ssr'
5654
export type { Plugin, HookHandler } from './plugin'
57-
export type { PackageCache, PackageData } from './packages'
5855
export type {
5956
Logger,
6057
LogOptions,

packages/vite/src/node/optimizer/esbuildDepPlugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import path from 'node:path'
22
import type { ImportKind, Plugin } from 'esbuild'
33
import { KNOWN_ASSET_TYPES } from '../constants'
4+
import type { PackageCache } from '../packages'
45
import { getDepOptimizationConfig } from '..'
5-
import type { PackageCache, ResolvedConfig } from '..'
6+
import type { ResolvedConfig } from '..'
67
import {
78
escapeRegex,
89
flattenId,

0 commit comments

Comments
 (0)