fix(nuxt): use resolvePath to handle edge cases for modules (#20975)

This commit is contained in:
Daniel Roe 2023-05-20 23:29:32 +01:00 committed by GitHub
parent 2d680455ae
commit 980728275a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 8 deletions

View File

@ -1,11 +1,11 @@
import { lstatSync } from 'node:fs'
import type { Nuxt, NuxtModule } from '@nuxt/schema'
import { dirname, isAbsolute, normalize, resolve } from 'pathe'
import { dirname, isAbsolute } from 'pathe'
import { isNuxt2 } from '../compatibility'
import { useNuxt } from '../context'
import { requireModule } from '../internal/cjs'
import { importModule } from '../internal/esm'
import { resolveAlias } from '../resolve'
import { resolveAlias, resolvePath } from '../resolve'
/** Installs a module on a Nuxt instance. */
export async function installModule (moduleToInstall: string | NuxtModule, _inlineOptions?: any, _nuxt?: Nuxt) {
@ -53,11 +53,7 @@ async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?:
// Import if input is string
if (typeof nuxtModule === 'string') {
let src = resolveAlias(nuxtModule)
if (src.match(/^\.{1,2}\//)) {
src = resolve(nuxt.options.rootDir, src)
}
src = normalize(src)
const src = await resolvePath(nuxtModule)
try {
// Prefer ESM resolution if possible
nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir })

View File

@ -80,7 +80,7 @@ export default defineNuxtConfig({
}
},
modules: [
'./modules/test/index',
'./modules/test',
[
'~/modules/example',
{