From 7827f1843b3552753de77328c29e098a3c0bc96e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 12 Dec 2023 17:55:21 +0000 Subject: [PATCH] feat(kit): load `/module` or `/nuxt` module subpath if it exists (#24707) --- packages/kit/src/module/install.ts | 26 +++++++++++++------ test/fixtures/basic/modules/subpath/index.ts | 1 + test/fixtures/basic/modules/subpath/module.ts | 5 ++++ test/fixtures/basic/nuxt.config.ts | 1 + 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/basic/modules/subpath/index.ts create mode 100644 test/fixtures/basic/modules/subpath/module.ts diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index 0f8ea05429..bc7ca1a95d 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -61,18 +61,28 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n let buildTimeModuleMeta: ModuleMeta = {} // Import if input is string if (typeof nuxtModule === 'string') { - const src = await resolvePath(nuxtModule) - try { + const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule] + let error: unknown + for (const path of paths) { + const src = await resolvePath(path) // Prefer ESM resolution if possible - nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir }) - } catch (error: unknown) { + try { + nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir }) + + // nuxt-module-builder generates a module.json with metadata including the version + if (existsSync(join(dirname(src), 'module.json'))) { + 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 } - // nuxt-module-builder generates a module.json with metadata including the version - if (existsSync(join(dirname(src), 'module.json'))) { - buildTimeModuleMeta = JSON.parse(await fsp.readFile(join(dirname(src), 'module.json'), 'utf-8')) - } } // Throw error if input is not a function diff --git a/test/fixtures/basic/modules/subpath/index.ts b/test/fixtures/basic/modules/subpath/index.ts new file mode 100644 index 0000000000..3c083d0b10 --- /dev/null +++ b/test/fixtures/basic/modules/subpath/index.ts @@ -0,0 +1 @@ +export const someUtil = () => {} diff --git a/test/fixtures/basic/modules/subpath/module.ts b/test/fixtures/basic/modules/subpath/module.ts new file mode 100644 index 0000000000..d1f975bc92 --- /dev/null +++ b/test/fixtures/basic/modules/subpath/module.ts @@ -0,0 +1,5 @@ +import { defineNuxtModule } from 'nuxt/kit' + +export default defineNuxtModule({ + meta: { name: 'subpath' }, +}) diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts index 7ee1bf3a53..fb727375cb 100644 --- a/test/fixtures/basic/nuxt.config.ts +++ b/test/fixtures/basic/nuxt.config.ts @@ -80,6 +80,7 @@ export default defineNuxtConfig({ } }, modules: [ + '~/modules/subpath', './modules/test', '~/modules/example', function (_, nuxt) {