diff --git a/packages/kit/package.json b/packages/kit/package.json index c4b6a6a8c9..7fa69a17fc 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -28,6 +28,7 @@ "hookable": "^5.0.0", "jiti": "^1.12.6", "lodash.template": "^4.5.0", + "mlly": "^0.2.5", "pathe": "^0.2.0", "rc9": "^1.2.0", "scule": "^0.2.1", diff --git a/packages/kit/src/nuxt.ts b/packages/kit/src/nuxt.ts index e36db067eb..d540665216 100644 --- a/packages/kit/src/nuxt.ts +++ b/packages/kit/src/nuxt.ts @@ -1,5 +1,5 @@ import { getContext } from 'unctx' -import { importModule, tryImportModule, tryResolveModule } from './utils/cjs' +import { importModule, tryImportModule, tryResolveModule, RequireModuleOptions } from './utils/cjs' import type { Nuxt } from './types/nuxt' import type { NuxtConfig } from './types/config' import type { LoadNuxtConfigOptions } from './config/load' @@ -41,7 +41,7 @@ export interface LoadNuxtOptions extends LoadNuxtConfigOptions { } export async function loadNuxt (opts: LoadNuxtOptions): Promise { - const resolveOpts = { paths: opts.rootDir } + const resolveOpts: RequireModuleOptions = { paths: opts.rootDir } // Detect version if (!opts.version) { @@ -56,7 +56,6 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { } // Nuxt 2 - // @ts-ignore const { loadNuxt } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts) const nuxt = await loadNuxt({ rootDir: opts.rootDir, @@ -69,7 +68,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { } export async function buildNuxt (nuxt: Nuxt): Promise { - const resolveOpts = { paths: nuxt.options.rootDir } + const resolveOpts: RequireModuleOptions = { paths: nuxt.options.rootDir } // Nuxt 3 if (nuxt.options._majorVersion === 3) { @@ -78,7 +77,6 @@ export async function buildNuxt (nuxt: Nuxt): Promise { } // Nuxt 2 - // @ts-ignore - const { build } = tryImportModule('nuxt-edge', resolveOpts) || tryImportModule('nuxt', resolveOpts) + const { build } = await tryImportModule('nuxt-edge', resolveOpts) || await tryImportModule('nuxt', resolveOpts) return build(nuxt) } diff --git a/packages/kit/src/utils/cjs.ts b/packages/kit/src/utils/cjs.ts index 3401bdbc5d..9b212189d7 100644 --- a/packages/kit/src/utils/cjs.ts +++ b/packages/kit/src/utils/cjs.ts @@ -1,5 +1,6 @@ import { pathToFileURL } from 'url' import { join, normalize } from 'pathe' +import { interopDefault } from 'mlly' import jiti from 'jiti' // TODO: use create-require for jest environment @@ -116,23 +117,23 @@ export function requireModule (id: string, opts: RequireModuleOptions = {}) { } // Try to require - let requiredModule = _require(resolvedPath) - - // Interop default - if (opts.interopDefault !== false && requiredModule && requiredModule.default) { - requiredModule = requiredModule.default - } + const requiredModule = _require(resolvedPath) return requiredModule } export function importModule (id: string, opts: RequireModuleOptions = {}) { const resolvedPath = resolveModule(id, opts) + if (opts.interopDefault !== false) { + return import(pathToFileURL(resolvedPath).href).then(interopDefault) + } return import(pathToFileURL(resolvedPath).href) } export function tryImportModule (id: string, opts: RequireModuleOptions = {}) { - return importModule(id, opts).catch(() => undefined) + try { + return importModule(id, opts).catch(() => undefined) + } catch { } } /** Try to require a module, but don't emit an error if the module can't be required. */ diff --git a/yarn.lock b/yarn.lock index 6496535136..8ee17b1a13 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1488,6 +1488,7 @@ __metadata: hookable: ^5.0.0 jiti: ^1.12.6 lodash.template: ^4.5.0 + mlly: ^0.2.5 pathe: ^0.2.0 rc9: ^1.2.0 scule: ^0.2.1 @@ -9369,6 +9370,15 @@ fsevents@~2.3.2: languageName: node linkType: hard +"mlly@npm:^0.2.5": + version: 0.2.5 + resolution: "mlly@npm:0.2.5" + dependencies: + import-meta-resolve: ^1.1.1 + checksum: bd111db12fc09c13ca6897b1dfb78501268f7d166cead7e806bafab905ca05134bd0b4eb7a5a5b1b05e9ecbbe2d9e8819dc95ffb62ba7e76e618a4d032236d3f + languageName: node + linkType: hard + "mocha@npm:^9.1.2": version: 9.1.2 resolution: "mocha@npm:9.1.2"