From 86bf3d5381cdca96fab2faaf94f6373201a5b8fa Mon Sep 17 00:00:00 2001 From: harlan Date: Thu, 31 Oct 2024 02:22:19 +1100 Subject: [PATCH] fix: rework alias paths --- src/module.ts | 11 +++++++---- src/prerender.ts | 6 ------ src/runtime/nitro/plugins/nuxt-content.ts | 2 +- src/runtime/nitro/routes/sitemap_index.xml.ts | 2 +- src/runtime/nitro/sitemap/nitro.ts | 3 +-- src/runtime/nitro/sitemap/urlset/sources.ts | 14 ++++++-------- virtual.d.ts | 11 +++++++++++ 7 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 virtual.d.ts diff --git a/src/module.ts b/src/module.ts index 2b8f2cb2..c0fee793 100644 --- a/src/module.ts +++ b/src/module.ts @@ -89,9 +89,9 @@ export default defineNuxtModule({ logger.debug('The module is disabled, skipping setup.') return } - nuxt.options.alias['#sitemap'] = resolve('./runtime/types') + nuxt.options.alias['#sitemap'] = resolve('./runtime') nuxt.options.nitro.alias = nuxt.options.nitro.alias || {} - nuxt.options.nitro.alias['#sitemap'] = resolve('./runtime/types') + nuxt.options.nitro.alias['#sitemap'] = resolve('./runtime') config.xslColumns = config.xslColumns || [ { label: 'URL', width: '50%' }, { label: 'Images', width: '25%', select: 'count(image:image)' }, @@ -275,6 +275,9 @@ export default defineNuxtModule({ extendTypes(name!, async ({ typesPath }) => { return ` declare module 'nitropack' { + interface PrerenderRoute { + _sitemap?: import('${typesPath}').SitemapUrl + } interface NitroRouteRules { index?: boolean sitemap?: import('${typesPath}').SitemapItemDefaults @@ -581,7 +584,7 @@ declare module 'vue-router' { const nitroPromise = createNitroPromise() let resolvedConfigUrls = false nuxt.hooks.hook('nitro:config', (nitroConfig) => { - nitroConfig.virtual!['#sitemap/global-sources.mjs'] = async () => { + nitroConfig.virtual!['#sitemap-virtual/global-sources.mjs'] = async () => { const { prerenderUrls, routeRules } = generateExtraRoutesFromNuxtConfig() const prerenderUrlsFinal = [ ...prerenderUrls, @@ -685,7 +688,7 @@ declare module 'vue-router' { const extraSitemapModules = typeof config.sitemaps == 'object' ? Object.keys(config.sitemaps).filter(n => n !== 'index') : [] const sitemapSources: Record = {} - nitroConfig.virtual![`#sitemap/child-sources.mjs`] = async () => { + nitroConfig.virtual![`#sitemap-virtual/child-sources.mjs`] = async () => { for (const sitemapName of extraSitemapModules) { sitemapSources[sitemapName] = sitemapSources[sitemapName] || [] const definition = (config.sitemaps as Record)[sitemapName] as SitemapDefinition diff --git a/src/prerender.ts b/src/prerender.ts index aeab6a2c..c3331f5f 100644 --- a/src/prerender.ts +++ b/src/prerender.ts @@ -26,12 +26,6 @@ function formatPrerenderRoute(route: PrerenderRoute) { return chalk.gray(str) } -declare module 'nitropack' { - interface PrerenderRoute { - _sitemap?: SitemapUrl - } -} - export function includesSitemapRoot(sitemapName: string, routes: string[]) { return routes.includes(`/__sitemap__/`) || routes.includes(`/sitemap.xml`) || routes.includes(`/${sitemapName}`) || routes.includes('/sitemap_index.xml') } diff --git a/src/runtime/nitro/plugins/nuxt-content.ts b/src/runtime/nitro/plugins/nuxt-content.ts index 78d72f0d..6619d4d2 100644 --- a/src/runtime/nitro/plugins/nuxt-content.ts +++ b/src/runtime/nitro/plugins/nuxt-content.ts @@ -1,5 +1,5 @@ import { defu } from 'defu' -import type { ParsedContent } from '@nuxt/content/dist/runtime/types' +import type { ParsedContent } from '@nuxt/content' import type { NitroApp } from 'nitropack' import type { SitemapUrl } from '../../types' import { useSimpleSitemapRuntimeConfig } from '../utils' diff --git a/src/runtime/nitro/routes/sitemap_index.xml.ts b/src/runtime/nitro/routes/sitemap_index.xml.ts index f3ce6627..a108d0e4 100644 --- a/src/runtime/nitro/routes/sitemap_index.xml.ts +++ b/src/runtime/nitro/routes/sitemap_index.xml.ts @@ -1,10 +1,10 @@ import { appendHeader, defineEventHandler, setHeader } from 'h3' import { joinURL } from 'ufo' -import { useNitroApp } from 'nitropack/runtime' import { useSimpleSitemapRuntimeConfig } from '../utils' import { buildSitemapIndex, urlsToIndexXml } from '../sitemap/builder/sitemap-index' import type { SitemapOutputHookCtx } from '../../types' import { useNitroUrlResolvers } from '../sitemap/nitro' +import { useNitroApp } from '#imports' export default defineEventHandler(async (e) => { const runtimeConfig = useSimpleSitemapRuntimeConfig() diff --git a/src/runtime/nitro/sitemap/nitro.ts b/src/runtime/nitro/sitemap/nitro.ts index b97b228e..c36c7f36 100644 --- a/src/runtime/nitro/sitemap/nitro.ts +++ b/src/runtime/nitro/sitemap/nitro.ts @@ -2,7 +2,6 @@ import { getQuery, setHeader, createError } from 'h3' import type { H3Event } from 'h3' import { fixSlashes } from 'site-config-stack/urls' import { defu } from 'defu' -import { useNitroApp } from 'nitropack/runtime' import type { ModuleRuntimeConfig, NitroUrlResolvers, @@ -15,7 +14,7 @@ import { createNitroRouteRuleMatcher } from '../kit' import { buildSitemapUrls, urlsToXml } from './builder/sitemap' import { normaliseEntry } from './urlset/normalise' import { sortSitemapUrls } from './urlset/sort' -import { createSitePathResolver, getPathRobotConfig, useSiteConfig } from '#imports' +import { useNitroApp, createSitePathResolver, getPathRobotConfig, useSiteConfig } from '#imports' export function useNitroUrlResolvers(e: H3Event): NitroUrlResolvers { const canonicalQuery = getQuery(e).canonical diff --git a/src/runtime/nitro/sitemap/urlset/sources.ts b/src/runtime/nitro/sitemap/urlset/sources.ts index c8bf9d16..cad2d6b4 100644 --- a/src/runtime/nitro/sitemap/urlset/sources.ts +++ b/src/runtime/nitro/sitemap/urlset/sources.ts @@ -77,20 +77,18 @@ export async function fetchDataSource(input: SitemapSourceBase | SitemapSourceRe } } -export function globalSitemapSources(): Promise<(SitemapSourceBase | SitemapSourceResolved)[]> { - // @ts-expect-error untyped - return import('#sitemap/global-sources.mjs') - .then(m => m.sources) as (SitemapSourceBase | SitemapSourceResolved)[] +export function globalSitemapSources() { + return import('#sitemap-virtual/global-sources.mjs') + .then(m => m.sources) } -export function childSitemapSources(definition: ModuleRuntimeConfig['sitemaps'][string]): Promise<(SitemapSourceBase | SitemapSourceResolved)[]> { +export function childSitemapSources(definition: ModuleRuntimeConfig['sitemaps'][string]) { return ( definition?._hasSourceChunk - // @ts-expect-error untyped - ? import(`#sitemap/child-sources.mjs`) + ? import(`#sitemap-virtual/child-sources.mjs`) .then(m => m.sources[definition.sitemapName] || []) : Promise.resolve([]) - ) as Promise<(SitemapSourceBase | SitemapSourceResolved)[]> + ) } export async function resolveSitemapSources(sources: (SitemapSourceBase | SitemapSourceResolved)[], event?: H3Event) { diff --git a/virtual.d.ts b/virtual.d.ts new file mode 100644 index 00000000..474bbff2 --- /dev/null +++ b/virtual.d.ts @@ -0,0 +1,11 @@ +declare module '#sitemap-virtual/global-sources.mjs' { + import type { SitemapSourceBase, SitemapSourceResolved } from '#sitemap/types' + + export const sources: (SitemapSourceBase | SitemapSourceResolved)[] +} + +declare module '#sitemap-virtual/child-sources.mjs' { + import type { SitemapSourceBase, SitemapSourceResolved } from '#sitemap/types' + + export const sources: Record +}