|
| 1 | + |
| 2 | +import { useNuxt, loadNuxtModuleInstance } from '@nuxt/kit' |
| 3 | +import { resolve } from 'pathe' |
| 4 | +import type { NuxtI18nOptions } from '@nuxtjs/i18n' |
| 5 | +import type { NuxtModule } from 'nuxt/schema' |
| 6 | + |
| 7 | +export async function webManifests(manifestDir) { |
| 8 | + |
| 9 | + const i18nOptions = await getNuxtModuleOptions('@nuxtjs/i18n') as NuxtI18nOptions |
| 10 | + |
| 11 | + return i18nOptions.locales.map(({code})=>{ |
| 12 | + const localePath = getLocalePath(i18nOptions, code) |
| 13 | + return { |
| 14 | + localDir: resolve(manifestDir, localePath), |
| 15 | + optionsI18n: { manifest: { |
| 16 | + start_url: `/${localePath}`, |
| 17 | + scope: `/${localePath}`, |
| 18 | + lang: code, |
| 19 | + }} |
| 20 | + } |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +//from https://github.com/nuxt-modules/sitemap/blob/main/src/util/kit.ts |
| 25 | +async function getNuxtModuleOptions(module: string | NuxtModule, nuxt: Nuxt = useNuxt()) { |
| 26 | + const moduleMeta = (typeof module === 'string' ? { name: module } : await module.getMeta?.()) || {} |
| 27 | + const { nuxtModule } = (await loadNuxtModuleInstance(module, nuxt)) |
| 28 | + |
| 29 | + let moduleEntry: [string | NuxtModule, Record<string, any>] | undefined |
| 30 | + for (const m of nuxt.options.modules) { |
| 31 | + if (Array.isArray(m) && m.length >= 2) { |
| 32 | + const _module = m[0] |
| 33 | + const _moduleEntryName = typeof _module === 'string' |
| 34 | + ? _module |
| 35 | + : (await (_module as any as NuxtModule).getMeta?.())?.name || '' |
| 36 | + if (_moduleEntryName === moduleMeta.name) |
| 37 | + moduleEntry = m as [string | NuxtModule, Record<string, any>] |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + let inlineOptions = {} |
| 42 | + if (moduleEntry) |
| 43 | + inlineOptions = moduleEntry[1] |
| 44 | + if (nuxtModule.getOptions) |
| 45 | + return nuxtModule.getOptions(inlineOptions, nuxt) |
| 46 | + return inlineOptions |
| 47 | +} |
| 48 | + |
| 49 | +function getLocalePath(options, localeCode) { |
| 50 | + switch (options.strategy) { |
| 51 | + case 'prefix_except_default': |
| 52 | + case 'prefix_and_default': |
| 53 | + return localeCode === options.defaultLocale ? "" : localeCode |
| 54 | + case 'prefix': |
| 55 | + return localeCode |
| 56 | + default: |
| 57 | + throw "Strategy not implemented" |
| 58 | + } |
| 59 | +} |
0 commit comments