diff --git a/docs/1.getting-started/12.upgrade.md b/docs/1.getting-started/12.upgrade.md index 9f768817cc..a778615970 100644 --- a/docs/1.getting-started/12.upgrade.md +++ b/docs/1.getting-started/12.upgrade.md @@ -577,27 +577,6 @@ These options have been set to their current values for some time and we do not * `respectNoSSRHeader`is implementable in user-land with [server middleware](https://github.com/nuxt/nuxt/blob/c660b39447f0d5b8790c0826092638d321cd6821/packages/nuxt/src/core/runtime/nitro/no-ssr.ts#L8-L9) -#### Removal of Deprecated Internal CJS Utils - -🚦 **Impact Level**: Minimal - -##### What Changed - -We have now removed the following utils exported from `@nuxt/kit`: - -* `requireModule` -* `tryRequireModule` - -They were previously marked as deprecated and relied on CJS resolutions. - -##### Reasons for Change - -We now use [jiti](https://github.com/unjs/jiti) to resolve modules and other imports internally. It supports native ESM resolution where possible and should be less buggy. - -##### Migration Steps - -You can use [jiti](https://github.com/unjs/jiti) or [mlly](https://github.com/unjs/mlly) to do the same job in your own projects if you were relying on these utilities. - ## Nuxt 2 vs Nuxt 3+ In the table below, there is a quick comparison between 3 versions of Nuxt: diff --git a/packages/kit/src/index.ts b/packages/kit/src/index.ts index 2429ed70ec..267b92e811 100644 --- a/packages/kit/src/index.ts +++ b/packages/kit/src/index.ts @@ -32,6 +32,6 @@ export { addTemplate, addTypeTemplate, normalizeTemplate, updateTemplates, write export { logger, useLogger } from './logger' // Internal Utils -export { resolveModule, tryResolveModule, importModule, tryImportModule } from './internal/esm' +export { resolveModule, tryResolveModule, importModule, tryImportModule, requireModule, tryRequireModule } from './internal/esm' export type { ImportModuleOptions, ResolveModuleOptions } from './internal/esm' export * from './internal/template' diff --git a/packages/kit/src/internal/esm.ts b/packages/kit/src/internal/esm.ts index c2c4d835e3..36f6843404 100644 --- a/packages/kit/src/internal/esm.ts +++ b/packages/kit/src/internal/esm.ts @@ -1,5 +1,6 @@ import { pathToFileURL } from 'node:url' import { interopDefault, resolvePath, resolvePathSync } from 'mlly' +import { createJiti } from 'jiti' export interface ResolveModuleOptions { paths?: string | string[] @@ -40,3 +41,32 @@ export function tryImportModule (id: string, opts?: ImportModuleOpt // intentionally empty as this is a `try-` function } } + +const warnings = new Set() + +/** + * @deprecated Please use `importModule` instead. + */ +export function requireModule (id: string, opts?: ImportModuleOptions) { + if (!warnings.has(id)) { + // TODO: add more information on stack trace + console.warn('[@nuxt/kit] `requireModule` is deprecated. Please use `importModule` instead.') + warnings.add(id) + } + const resolvedPath = resolveModule(id, opts) + const jiti = createJiti(import.meta.url, { + interopDefault: opts?.interopDefault !== false, + }) + return jiti(pathToFileURL(resolvedPath).href) as T +} + +/** + * @deprecated Please use `tryImportModule` instead. + */ +export function tryRequireModule (id: string, opts?: ImportModuleOptions) { + try { + return requireModule(id, opts) + } catch { + // intentionally empty as this is a `try-` function + } +}