Skip to content

Commit

Permalink
fix: stub in mock composables when module disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jul 13, 2024
1 parent b425fc5 commit 36e23da
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ export default defineNuxtModule<ModuleOptions>({
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
}

Expand Down
17 changes: 17 additions & 0 deletions src/runtime/nitro/composables/mock.ts
Original file line number Diff line number Diff line change
@@ -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: [],
}
}
10 changes: 10 additions & 0 deletions src/runtime/nuxt/composables/mock.ts
Original file line number Diff line number Diff line change
@@ -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<boolean | string>) {
return ref('')
}

0 comments on commit 36e23da

Please sign in to comment.