fix(kit): de-default nuxt2 imports and add sync try/catch (#658)

This commit is contained in:
Daniel Roe 2021-10-06 10:23:40 +02:00 committed by GitHub
parent 485c97645a
commit b13e20f317
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 13 deletions

View File

@ -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",

View File

@ -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<Nuxt> {
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> {
}
// 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<Nuxt> {
}
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
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<any> {
}
// 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)
}

View File

@ -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. */

View File

@ -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"