From 453df9775745af160652a5299a30f33531fd6cd6 Mon Sep 17 00:00:00 2001 From: wackbyte Date: Fri, 14 Jun 2024 17:35:40 -0400 Subject: [PATCH] chore: logging cleanup Use the logger provided by Astro more consistently in integrations and remove an old duplicate check in the sitemap integration. --- .changeset/thin-icons-yell.md | 7 +++++++ packages/integrations/mdx/src/index.ts | 22 ++++++++++++++++------ packages/integrations/mdx/src/utils.ts | 12 ++++++------ packages/integrations/node/src/index.ts | 6 +++--- packages/integrations/sitemap/src/index.ts | 10 +--------- 5 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 .changeset/thin-icons-yell.md diff --git a/.changeset/thin-icons-yell.md b/.changeset/thin-icons-yell.md new file mode 100644 index 000000000000..af1042907333 --- /dev/null +++ b/.changeset/thin-icons-yell.md @@ -0,0 +1,7 @@ +--- +'@astrojs/sitemap': patch +'@astrojs/node': patch +'@astrojs/mdx': patch +--- + +Refactor to use Astro's integration logger for logging diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index bd0278933aea..30f827dc8200 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -1,7 +1,13 @@ import fs from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; import { markdownConfigDefaults } from '@astrojs/markdown-remark'; -import type { AstroIntegration, ContainerRenderer, ContentEntryType, HookParameters } from 'astro'; +import type { + AstroIntegration, + AstroIntegrationLogger, + ContainerRenderer, + ContentEntryType, + HookParameters, +} from 'astro'; import astroJSXRenderer from 'astro/jsx/renderer.js'; import type { Options as RemarkRehypeOptions } from 'remark-rehype'; import type { PluggableList } from 'unified'; @@ -75,7 +81,7 @@ export default function mdx(partialMdxOptions: Partial = {}): AstroI }, }); }, - 'astro:config:done': ({ config }) => { + 'astro:config:done': ({ config, logger }) => { // We resolve the final MDX options here so that other integrations have a chance to modify // `config.markdown` before we access it const extendMarkdownConfig = @@ -84,7 +90,8 @@ export default function mdx(partialMdxOptions: Partial = {}): AstroI const resolvedMdxOptions = applyDefaultOptions({ options: partialMdxOptions, defaults: markdownConfigToMdxOptions( - extendMarkdownConfig ? config.markdown : markdownConfigDefaults + extendMarkdownConfig ? config.markdown : markdownConfigDefaults, + logger ), }); @@ -104,12 +111,15 @@ const defaultMdxOptions = { optimize: false, } satisfies Partial; -function markdownConfigToMdxOptions(markdownConfig: typeof markdownConfigDefaults): MdxOptions { +function markdownConfigToMdxOptions( + markdownConfig: typeof markdownConfigDefaults, + logger: AstroIntegrationLogger +): MdxOptions { return { ...defaultMdxOptions, ...markdownConfig, - remarkPlugins: ignoreStringPlugins(markdownConfig.remarkPlugins), - rehypePlugins: ignoreStringPlugins(markdownConfig.rehypePlugins), + remarkPlugins: ignoreStringPlugins(markdownConfig.remarkPlugins, logger), + rehypePlugins: ignoreStringPlugins(markdownConfig.rehypePlugins, logger), remarkRehype: (markdownConfig.remarkRehype as any) ?? {}, }; } diff --git a/packages/integrations/mdx/src/utils.ts b/packages/integrations/mdx/src/utils.ts index 9adf3586f417..4705316ccdb6 100644 --- a/packages/integrations/mdx/src/utils.ts +++ b/packages/integrations/mdx/src/utils.ts @@ -1,8 +1,8 @@ import type { Options as AcornOpts } from 'acorn'; import { parse } from 'acorn'; -import type { AstroConfig, SSRError } from 'astro'; +import type { AstroConfig, AstroIntegrationLogger, SSRError } from 'astro'; import matter from 'gray-matter'; -import { bold, yellow } from 'kleur/colors'; +import { bold } from 'kleur/colors'; import type { MdxjsEsm } from 'mdast-util-mdx'; import type { PluggableList } from 'unified'; @@ -85,22 +85,22 @@ export function jsToTreeNode( }; } -export function ignoreStringPlugins(plugins: any[]): PluggableList { +export function ignoreStringPlugins(plugins: any[], logger: AstroIntegrationLogger): PluggableList { let validPlugins: PluggableList = []; let hasInvalidPlugin = false; for (const plugin of plugins) { if (typeof plugin === 'string') { - console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`)); + logger.warn(`${bold(plugin)} not applied.`); hasInvalidPlugin = true; } else if (Array.isArray(plugin) && typeof plugin[0] === 'string') { - console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`)); + logger.warn(`${bold(plugin[0])} not applied.`); hasInvalidPlugin = true; } else { validPlugins.push(plugin); } } if (hasInvalidPlugin) { - console.warn( + logger.warn( `To inherit Markdown plugins in MDX, please use explicit imports in your config instead of "strings." See Markdown docs: https://docs.astro.build/en/guides/markdown-content/#markdown-plugins` ); } diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index 5fec3850c79f..e11576990e98 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -45,7 +45,7 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr }, }); }, - 'astro:config:done': ({ setAdapter, config }) => { + 'astro:config:done': ({ setAdapter, config, logger }) => { _options = { ...userOptions, client: config.build.client?.toString(), @@ -57,8 +57,8 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr setAdapter(getAdapter(_options)); if (config.output === 'static') { - console.warn( - `[@astrojs/node] \`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.` + logger.warn( + `\`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.` ); } }, diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index 622ce958ff10..98aa4f31cc74 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -88,15 +88,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { const { filter, customPages, serialize, entryLimit } = opts; - let finalSiteUrl: URL; - if (config.site) { - finalSiteUrl = new URL(config.base, config.site); - } else { - console.warn( - 'The Sitemap integration requires the `site` astro.config option. Skipping.' - ); - return; - } + let finalSiteUrl = new URL(config.base, config.site); const shouldIgnoreStatus = isStatusCodePage(Object.keys(opts.i18n?.locales ?? {})); let pageUrls = pages .filter((p) => !shouldIgnoreStatus(p.pathname))