diff --git a/docs/guide/migration.md b/docs/guide/migration.md index f141f742b892a3..c2f17a70111837 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -40,6 +40,29 @@ CLI shortcuts, like `r` to restart the dev server, now require an additional `En 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). +### Remove `resolvePackageEntry` and `resolvePackageData` APIs + +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: + +- `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. +- `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. + +```js +import { resolve } from 'import-meta-env' +import { findDepPkgJsonPath } from 'vitefu' +import fs from 'node:fs' + +const pkg = 'my-lib' +const basedir = process.cwd() + +// `resolvePackageEntry`: +const packageEntry = resolve(pkg, basedir) + +// `resolvePackageData`: +const packageJsonPath = findDepPkgJsonPath(pkg, basedir) +const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) +``` + ## Removed Deprecated APIs - Default exports of CSS files (e.g `import style from './foo.css'`): Use the `?inline` query instead diff --git a/packages/vite/index.cjs b/packages/vite/index.cjs index 7132d113b0540a..a4fac25fb92c03 100644 --- a/packages/vite/index.cjs +++ b/packages/vite/index.cjs @@ -25,16 +25,6 @@ asyncFunctions.forEach((name) => { import('./dist/node/index.js').then((i) => i[name](...args)) }) -// some sync functions are marked not supported due to their complexity and uncommon usage -const unsupportedCJS = ['resolvePackageEntry', 'resolvePackageData'] -unsupportedCJS.forEach((name) => { - module.exports[name] = () => { - throw new Error( - `"${name}" is not supported in CJS build of Vite 4.\nPlease use ESM or dynamic imports \`const { ${name} } = await import('vite')\`.`, - ) - } -}) - function warnCjsUsage() { if (process.env.VITE_CJS_IGNORE_WARNING) return globalThis.__vite_cjs_skip_clear_screen = true diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 3d21243d0ab534..eff79be4441aec 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -8,8 +8,6 @@ export { build } from './build' export { optimizeDeps } from './optimizer' export { formatPostcssSourceMap, preprocessCSS } from './plugins/css' export { transformWithEsbuild } from './plugins/esbuild' -export { resolvePackageEntry } from './plugins/resolve' -export { resolvePackageData } from './packages' export { buildErrorMessage } from './server/middlewares/error' export * from './publicUtils' @@ -54,7 +52,6 @@ export type { SSRTarget, } from './ssr' export type { Plugin, HookHandler } from './plugin' -export type { PackageCache, PackageData } from './packages' export type { Logger, LogOptions, diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index b3069f43ae4b9e..7d1db4f57c87b0 100644 --- a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts +++ b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts @@ -1,8 +1,9 @@ import path from 'node:path' import type { ImportKind, Plugin } from 'esbuild' import { KNOWN_ASSET_TYPES } from '../constants' +import type { PackageCache } from '../packages' import { getDepOptimizationConfig } from '..' -import type { PackageCache, ResolvedConfig } from '..' +import type { ResolvedConfig } from '..' import { escapeRegex, flattenId,