Skip to content

Commit

Permalink
fix: read package.json version at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 6, 2024
1 parent 3409629 commit a63a0e8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"nuxt-site-config-kit": "^2.2.0",
"ofetch": "^1.3.3",
"pathe": "^1.1.1",
"pkg-types": "^1.0.3",
"radix3": "^1.1.0",
"semver": "^7.5.4",
"sirv": "^2.0.4",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { installNuxtSiteConfig } from 'nuxt-site-config-kit'
import type { NuxtI18nOptions } from '@nuxtjs/i18n/dist/module'
import { defu } from 'defu'
import type { NitroRouteConfig } from 'nitropack'
import { version } from '../package.json'
import { readPackageJSON } from 'pkg-types'
import type {
AppSourceContext,
AutoI18nConfig,
Expand Down Expand Up @@ -87,7 +87,9 @@ export default defineNuxtModule<ModuleOptions>({
inferStaticPagesAsRoutes: true,
},
async setup(config, nuxt) {
const logger = useLogger('@nuxtjs/sitemap')
const { resolve } = createResolver(import.meta.url)
const { name, version } = await readPackageJSON(resolve('../package.json'))
const logger = useLogger(name)
logger.level = (config.debug || nuxt.options.debug) ? 4 : 3
if (config.enabled === false) {
logger.debug('The module is disabled, skipping setup.')
Expand All @@ -108,7 +110,6 @@ export default defineNuxtModule<ModuleOptions>({
config.defaults.lastmod = normaliseDate(new Date())
}

const { resolve } = createResolver(import.meta.url)
// for trailing slashes / canonical absolute urls
await installNuxtSiteConfig()
const userGlobalSources: SitemapSourceInput[] = [
Expand Down Expand Up @@ -190,7 +191,7 @@ export default defineNuxtModule<ModuleOptions>({
}
else {
if (!normalisedLocales.length)
logger.warn('You are using @nuxtjs/i18n but have not configured any locales, this will cause issues with @nuxtjs/sitemap. Please configure `locales`.')
logger.warn(`You are using @nuxtjs/i18n but have not configured any locales, this will cause issues with ${name}. Please configure \`locales\`.`)
}
const hasSetAutoI18n = typeof config.autoI18n === 'object' && Object.keys(config.autoI18n).length
const hasI18nConfigForAlternatives = nuxtI18nConfig.differentDomains || usingI18nPages || (nuxtI18nConfig.strategy !== 'no_prefix' && nuxtI18nConfig.locales)
Expand Down Expand Up @@ -237,7 +238,7 @@ export default defineNuxtModule<ModuleOptions>({
}])
}

extendTypes('@nuxtjs/sitemap', async ({ typesPath }) => {
extendTypes(name!, async ({ typesPath }) => {
return `
declare module 'nitropack' {
interface NitroRouteRules {
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/nitro/routes/sitemap.xsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export default defineEventHandler(async (e) => {
setHeader(e, 'Cache-Control', `no-cache, no-store`)

const svgIcon = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="icon" style="margin-right: 4px; font-size: 25px;" width="1em" height="1em" viewBox="0 0 32 32"><path fill="#93c5fd" d="M4 26h4v4H4zm10 0h4v4h-4zm10 0h4v4h-4zm1-10h-8v-2h-2v2H7a2.002 2.002 0 0 0-2 2v6h2v-6h8v6h2v-6h8v6h2v-6a2.002 2.002 0 0 0-2-2zM9 2v10h14V2zm2 2h2v6h-2zm10 6h-6V4h6z"></path></svg>`
const creditName = `<a href="https://github.com/nuxt-modules/sitemap" style="color: black; display: flex; align-items: center; font-weight: 500;" target="_blank" rel="noopener">${svgIcon} Nuxt
Simple Sitemap v${version}</a>`
const creditName = `<a href="https://github.com/nuxt-modules/sitemap" style="color: black; display: flex; align-items: center; font-weight: 500;" target="_blank" rel="noopener">${svgIcon} Nuxt Sitemap v${version}</a>`

const { name: siteName, url: siteUrl } = useSiteConfig(e)

Expand Down

0 comments on commit a63a0e8

Please sign in to comment.