Skip to content

Commit

Permalink
fix: rework alias paths
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 30, 2024
1 parent a66e1d6 commit 86bf3d5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 22 deletions.
11 changes: 7 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export default defineNuxtModule<ModuleOptions>({
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)' },
Expand Down Expand Up @@ -275,6 +275,9 @@ export default defineNuxtModule<ModuleOptions>({
extendTypes(name!, async ({ typesPath }) => {
return `
declare module 'nitropack' {
interface PrerenderRoute {
_sitemap?: import('${typesPath}').SitemapUrl
}
interface NitroRouteRules {
index?: boolean
sitemap?: import('${typesPath}').SitemapItemDefaults
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, SitemapSourceInput[]> = {}
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<string, SitemapDefinition>)[sitemapName] as SitemapDefinition
Expand Down
6 changes: 0 additions & 6 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/plugins/nuxt-content.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/routes/sitemap_index.xml.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/nitro/sitemap/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
14 changes: 6 additions & 8 deletions src/runtime/nitro/sitemap/urlset/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions virtual.d.ts
Original file line number Diff line number Diff line change
@@ -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<string, (SitemapSourceBase | SitemapSourceResolved)[]>
}

0 comments on commit 86bf3d5

Please sign in to comment.