mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(kit): add back requireModule
and tryRequireModule
(#28013)
This commit is contained in:
parent
222305ce75
commit
19d7f93284
@ -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:
|
||||||
|
@ -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'
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user