fix(kit): add back requireModule and tryRequireModule (#28013)

This commit is contained in:
Daniel Roe 2024-07-04 10:04:10 +01:00 committed by GitHub
parent 222305ce75
commit 19d7f93284
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 22 deletions

View File

@ -509,27 +509,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) * `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+ ## Nuxt 2 vs Nuxt 3+
In the table below, there is a quick comparison between 3 versions of Nuxt: In the table below, there is a quick comparison between 3 versions of Nuxt:

View File

@ -32,5 +32,5 @@ export { addTemplate, addTypeTemplate, normalizeTemplate, updateTemplates, write
export { logger, useLogger } from './logger' export { logger, useLogger } from './logger'
// Internal Utils // 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 type { ImportModuleOptions, ResolveModuleOptions } from './internal/esm'

View File

@ -1,5 +1,6 @@
import { pathToFileURL } from 'node:url' import { pathToFileURL } from 'node:url'
import { interopDefault, resolvePath, resolvePathSync } from 'mlly' import { interopDefault, resolvePath, resolvePathSync } from 'mlly'
import { createJiti } from 'jiti'
export interface ResolveModuleOptions { export interface ResolveModuleOptions {
paths?: string | string[] paths?: string | string[]
@ -40,3 +41,32 @@ export function tryImportModule<T = unknown> (id: string, opts?: ImportModuleOpt
// intentionally empty as this is a `try-` function // intentionally empty as this is a `try-` function
} }
} }
const warnings = new Set<string>()
/**
* @deprecated Please use `importModule` instead.
*/
export function requireModule<T = unknown> (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<T = unknown> (id: string, opts?: ImportModuleOptions) {
try {
return requireModule<T>(id, opts)
} catch {
// intentionally empty as this is a `try-` function
}
}