mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(kit): load /module
or /nuxt
module subpath if it exists (#24707)
This commit is contained in:
parent
3991a10adf
commit
7827f1843b
@ -61,18 +61,28 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n
|
|||||||
let buildTimeModuleMeta: ModuleMeta = {}
|
let buildTimeModuleMeta: ModuleMeta = {}
|
||||||
// Import if input is string
|
// Import if input is string
|
||||||
if (typeof nuxtModule === 'string') {
|
if (typeof nuxtModule === 'string') {
|
||||||
const src = await resolvePath(nuxtModule)
|
const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule]
|
||||||
try {
|
let error: unknown
|
||||||
|
for (const path of paths) {
|
||||||
|
const src = await resolvePath(path)
|
||||||
// Prefer ESM resolution if possible
|
// Prefer ESM resolution if possible
|
||||||
|
try {
|
||||||
nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir })
|
nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir })
|
||||||
} catch (error: unknown) {
|
|
||||||
logger.error(`Error while requiring module \`${nuxtModule}\`: ${error}`)
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
// nuxt-module-builder generates a module.json with metadata including the version
|
// nuxt-module-builder generates a module.json with metadata including the version
|
||||||
if (existsSync(join(dirname(src), 'module.json'))) {
|
if (existsSync(join(dirname(src), 'module.json'))) {
|
||||||
buildTimeModuleMeta = JSON.parse(await fsp.readFile(join(dirname(src), 'module.json'), 'utf-8'))
|
buildTimeModuleMeta = JSON.parse(await fsp.readFile(join(dirname(src), 'module.json'), 'utf-8'))
|
||||||
}
|
}
|
||||||
|
break
|
||||||
|
} catch (_err: unknown) {
|
||||||
|
error = _err
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!nuxtModule && error) {
|
||||||
|
logger.error(`Error while requiring module \`${nuxtModule}\`: ${error}`)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Throw error if input is not a function
|
// Throw error if input is not a function
|
||||||
|
1
test/fixtures/basic/modules/subpath/index.ts
vendored
Normal file
1
test/fixtures/basic/modules/subpath/index.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const someUtil = () => {}
|
5
test/fixtures/basic/modules/subpath/module.ts
vendored
Normal file
5
test/fixtures/basic/modules/subpath/module.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { defineNuxtModule } from 'nuxt/kit'
|
||||||
|
|
||||||
|
export default defineNuxtModule({
|
||||||
|
meta: { name: 'subpath' },
|
||||||
|
})
|
1
test/fixtures/basic/nuxt.config.ts
vendored
1
test/fixtures/basic/nuxt.config.ts
vendored
@ -80,6 +80,7 @@ export default defineNuxtConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
modules: [
|
modules: [
|
||||||
|
'~/modules/subpath',
|
||||||
'./modules/test',
|
'./modules/test',
|
||||||
'~/modules/example',
|
'~/modules/example',
|
||||||
function (_, nuxt) {
|
function (_, nuxt) {
|
||||||
|
Loading…
Reference in New Issue
Block a user