From 36e23da343f74a0bc0544d7977add0526085c29c Mon Sep 17 00:00:00 2001 From: harlan Date: Sat, 13 Jul 2024 17:41:42 +1000 Subject: [PATCH] fix: stub in mock composables when module disabled --- src/module.ts | 15 +++++++++++++++ src/runtime/nitro/composables/mock.ts | 17 +++++++++++++++++ src/runtime/nuxt/composables/mock.ts | 10 ++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/runtime/nitro/composables/mock.ts create mode 100644 src/runtime/nuxt/composables/mock.ts diff --git a/src/module.ts b/src/module.ts index bfaf02bb..ccc22cbb 100644 --- a/src/module.ts +++ b/src/module.ts @@ -173,6 +173,21 @@ export default defineNuxtModule({ logger.level = (config.debug || nuxt.options.debug) ? 4 : 3 if (config.enabled === false) { logger.debug('The module is disabled, skipping setup.') + // need to mock the composables to allow module still to work when disabled + ;['defineRobotMeta', 'useRobotsRule'] + .forEach((name) => { + addImports({ name, from: resolve(`./runtime/nuxt/composables/mock`) }) + }) + nuxt.options.nitro = nuxt.options.nitro || {} + nuxt.options.nitro.imports = nuxt.options.nitro.imports || {} + nuxt.options.nitro.imports.presets = nuxt.options.nitro.imports.presets || [] + nuxt.options.nitro.imports.presets.push({ + from: resolve('./runtime/nitro/composables/mock'), + imports: [ + 'getPathRobotConfig', + 'getSiteRobotConfig', + ], + }) return } diff --git a/src/runtime/nitro/composables/mock.ts b/src/runtime/nitro/composables/mock.ts new file mode 100644 index 00000000..9a422223 --- /dev/null +++ b/src/runtime/nitro/composables/mock.ts @@ -0,0 +1,17 @@ +import type { H3Event } from 'h3' + +// eslint-disable-next-line unused-imports/no-unused-vars +export function getPathRobotConfig(e: H3Event, options?: { skipSiteIndexable?: boolean, path?: string }) { + return { + indexable: true, + rule: '', + } +} + +// eslint-disable-next-line unused-imports/no-unused-vars +export function getSiteRobotConfig(e: H3Event): { indexable: boolean, hints: string[] } { + return { + indexable: true, + hints: [], + } +} diff --git a/src/runtime/nuxt/composables/mock.ts b/src/runtime/nuxt/composables/mock.ts new file mode 100644 index 00000000..f11e5eb7 --- /dev/null +++ b/src/runtime/nuxt/composables/mock.ts @@ -0,0 +1,10 @@ +import type { MaybeRef } from 'vue' +import { ref } from '#imports' + +// eslint-disable-next-line unused-imports/no-unused-vars +export function defineRobotMeta(component?: boolean) {} + +// eslint-disable-next-line unused-imports/no-unused-vars +export function useRobotsRule(rule?: MaybeRef) { + return ref('') +}