mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
fix(kit): de-default nuxt2 imports and add sync try/catch (#658)
This commit is contained in:
parent
485c97645a
commit
b13e20f317
@ -28,6 +28,7 @@
|
|||||||
"hookable": "^5.0.0",
|
"hookable": "^5.0.0",
|
||||||
"jiti": "^1.12.6",
|
"jiti": "^1.12.6",
|
||||||
"lodash.template": "^4.5.0",
|
"lodash.template": "^4.5.0",
|
||||||
|
"mlly": "^0.2.5",
|
||||||
"pathe": "^0.2.0",
|
"pathe": "^0.2.0",
|
||||||
"rc9": "^1.2.0",
|
"rc9": "^1.2.0",
|
||||||
"scule": "^0.2.1",
|
"scule": "^0.2.1",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { getContext } from 'unctx'
|
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 { Nuxt } from './types/nuxt'
|
||||||
import type { NuxtConfig } from './types/config'
|
import type { NuxtConfig } from './types/config'
|
||||||
import type { LoadNuxtConfigOptions } from './config/load'
|
import type { LoadNuxtConfigOptions } from './config/load'
|
||||||
@ -41,7 +41,7 @@ export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||||
const resolveOpts = { paths: opts.rootDir }
|
const resolveOpts: RequireModuleOptions = { paths: opts.rootDir }
|
||||||
|
|
||||||
// Detect version
|
// Detect version
|
||||||
if (!opts.version) {
|
if (!opts.version) {
|
||||||
@ -56,7 +56,6 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nuxt 2
|
// Nuxt 2
|
||||||
// @ts-ignore
|
|
||||||
const { loadNuxt } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts)
|
const { loadNuxt } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts)
|
||||||
const nuxt = await loadNuxt({
|
const nuxt = await loadNuxt({
|
||||||
rootDir: opts.rootDir,
|
rootDir: opts.rootDir,
|
||||||
@ -69,7 +68,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
|
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
|
||||||
const resolveOpts = { paths: nuxt.options.rootDir }
|
const resolveOpts: RequireModuleOptions = { paths: nuxt.options.rootDir }
|
||||||
|
|
||||||
// Nuxt 3
|
// Nuxt 3
|
||||||
if (nuxt.options._majorVersion === 3) {
|
if (nuxt.options._majorVersion === 3) {
|
||||||
@ -78,7 +77,6 @@ export async function buildNuxt (nuxt: Nuxt): Promise<any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nuxt 2
|
// Nuxt 2
|
||||||
// @ts-ignore
|
const { build } = await tryImportModule('nuxt-edge', resolveOpts) || await tryImportModule('nuxt', resolveOpts)
|
||||||
const { build } = tryImportModule('nuxt-edge', resolveOpts) || tryImportModule('nuxt', resolveOpts)
|
|
||||||
return build(nuxt)
|
return build(nuxt)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { pathToFileURL } from 'url'
|
import { pathToFileURL } from 'url'
|
||||||
import { join, normalize } from 'pathe'
|
import { join, normalize } from 'pathe'
|
||||||
|
import { interopDefault } from 'mlly'
|
||||||
import jiti from 'jiti'
|
import jiti from 'jiti'
|
||||||
|
|
||||||
// TODO: use create-require for jest environment
|
// TODO: use create-require for jest environment
|
||||||
@ -116,23 +117,23 @@ export function requireModule (id: string, opts: RequireModuleOptions = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to require
|
// Try to require
|
||||||
let requiredModule = _require(resolvedPath)
|
const requiredModule = _require(resolvedPath)
|
||||||
|
|
||||||
// Interop default
|
|
||||||
if (opts.interopDefault !== false && requiredModule && requiredModule.default) {
|
|
||||||
requiredModule = requiredModule.default
|
|
||||||
}
|
|
||||||
|
|
||||||
return requiredModule
|
return requiredModule
|
||||||
}
|
}
|
||||||
|
|
||||||
export function importModule (id: string, opts: RequireModuleOptions = {}) {
|
export function importModule (id: string, opts: RequireModuleOptions = {}) {
|
||||||
const resolvedPath = resolveModule(id, opts)
|
const resolvedPath = resolveModule(id, opts)
|
||||||
|
if (opts.interopDefault !== false) {
|
||||||
|
return import(pathToFileURL(resolvedPath).href).then(interopDefault)
|
||||||
|
}
|
||||||
return import(pathToFileURL(resolvedPath).href)
|
return import(pathToFileURL(resolvedPath).href)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tryImportModule (id: string, opts: RequireModuleOptions = {}) {
|
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. */
|
/** Try to require a module, but don't emit an error if the module can't be required. */
|
||||||
|
10
yarn.lock
10
yarn.lock
@ -1488,6 +1488,7 @@ __metadata:
|
|||||||
hookable: ^5.0.0
|
hookable: ^5.0.0
|
||||||
jiti: ^1.12.6
|
jiti: ^1.12.6
|
||||||
lodash.template: ^4.5.0
|
lodash.template: ^4.5.0
|
||||||
|
mlly: ^0.2.5
|
||||||
pathe: ^0.2.0
|
pathe: ^0.2.0
|
||||||
rc9: ^1.2.0
|
rc9: ^1.2.0
|
||||||
scule: ^0.2.1
|
scule: ^0.2.1
|
||||||
@ -9369,6 +9370,15 @@ fsevents@~2.3.2:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
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":
|
"mocha@npm:^9.1.2":
|
||||||
version: 9.1.2
|
version: 9.1.2
|
||||||
resolution: "mocha@npm:9.1.2"
|
resolution: "mocha@npm:9.1.2"
|
||||||
|
Loading…
Reference in New Issue
Block a user