Skip to content

Commit 7d59750

Browse files
authoredJun 17, 2024··
chore: logging cleanup (#11263)
1 parent 6fcc246 commit 7d59750

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed
 

‎.changeset/thin-icons-yell.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astrojs/sitemap': patch
3+
'@astrojs/node': patch
4+
'@astrojs/mdx': patch
5+
---
6+
7+
Refactor to use Astro's integration logger for logging

‎packages/integrations/mdx/src/index.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import fs from 'node:fs/promises';
22
import { fileURLToPath } from 'node:url';
33
import { markdownConfigDefaults } from '@astrojs/markdown-remark';
4-
import type { AstroIntegration, ContainerRenderer, ContentEntryType, HookParameters } from 'astro';
4+
import type {
5+
AstroIntegration,
6+
AstroIntegrationLogger,
7+
ContainerRenderer,
8+
ContentEntryType,
9+
HookParameters,
10+
} from 'astro';
511
import astroJSXRenderer from 'astro/jsx/renderer.js';
612
import type { Options as RemarkRehypeOptions } from 'remark-rehype';
713
import type { PluggableList } from 'unified';
@@ -75,7 +81,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
7581
},
7682
});
7783
},
78-
'astro:config:done': ({ config }) => {
84+
'astro:config:done': ({ config, logger }) => {
7985
// We resolve the final MDX options here so that other integrations have a chance to modify
8086
// `config.markdown` before we access it
8187
const extendMarkdownConfig =
@@ -84,7 +90,8 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
8490
const resolvedMdxOptions = applyDefaultOptions({
8591
options: partialMdxOptions,
8692
defaults: markdownConfigToMdxOptions(
87-
extendMarkdownConfig ? config.markdown : markdownConfigDefaults
93+
extendMarkdownConfig ? config.markdown : markdownConfigDefaults,
94+
logger
8895
),
8996
});
9097

@@ -104,12 +111,15 @@ const defaultMdxOptions = {
104111
optimize: false,
105112
} satisfies Partial<MdxOptions>;
106113

107-
function markdownConfigToMdxOptions(markdownConfig: typeof markdownConfigDefaults): MdxOptions {
114+
function markdownConfigToMdxOptions(
115+
markdownConfig: typeof markdownConfigDefaults,
116+
logger: AstroIntegrationLogger
117+
): MdxOptions {
108118
return {
109119
...defaultMdxOptions,
110120
...markdownConfig,
111-
remarkPlugins: ignoreStringPlugins(markdownConfig.remarkPlugins),
112-
rehypePlugins: ignoreStringPlugins(markdownConfig.rehypePlugins),
121+
remarkPlugins: ignoreStringPlugins(markdownConfig.remarkPlugins, logger),
122+
rehypePlugins: ignoreStringPlugins(markdownConfig.rehypePlugins, logger),
113123
remarkRehype: (markdownConfig.remarkRehype as any) ?? {},
114124
};
115125
}

‎packages/integrations/mdx/src/utils.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Options as AcornOpts } from 'acorn';
22
import { parse } from 'acorn';
3-
import type { AstroConfig, SSRError } from 'astro';
3+
import type { AstroConfig, AstroIntegrationLogger, SSRError } from 'astro';
44
import matter from 'gray-matter';
5-
import { bold, yellow } from 'kleur/colors';
5+
import { bold } from 'kleur/colors';
66
import type { MdxjsEsm } from 'mdast-util-mdx';
77
import type { PluggableList } from 'unified';
88

@@ -85,22 +85,22 @@ export function jsToTreeNode(
8585
};
8686
}
8787

88-
export function ignoreStringPlugins(plugins: any[]): PluggableList {
88+
export function ignoreStringPlugins(plugins: any[], logger: AstroIntegrationLogger): PluggableList {
8989
let validPlugins: PluggableList = [];
9090
let hasInvalidPlugin = false;
9191
for (const plugin of plugins) {
9292
if (typeof plugin === 'string') {
93-
console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`));
93+
logger.warn(`${bold(plugin)} not applied.`);
9494
hasInvalidPlugin = true;
9595
} else if (Array.isArray(plugin) && typeof plugin[0] === 'string') {
96-
console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`));
96+
logger.warn(`${bold(plugin[0])} not applied.`);
9797
hasInvalidPlugin = true;
9898
} else {
9999
validPlugins.push(plugin);
100100
}
101101
}
102102
if (hasInvalidPlugin) {
103-
console.warn(
103+
logger.warn(
104104
`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`
105105
);
106106
}

‎packages/integrations/node/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
4545
},
4646
});
4747
},
48-
'astro:config:done': ({ setAdapter, config }) => {
48+
'astro:config:done': ({ setAdapter, config, logger }) => {
4949
_options = {
5050
...userOptions,
5151
client: config.build.client?.toString(),
@@ -57,8 +57,8 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
5757
setAdapter(getAdapter(_options));
5858

5959
if (config.output === 'static') {
60-
console.warn(
61-
`[@astrojs/node] \`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.`
60+
logger.warn(
61+
`\`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.`
6262
);
6363
}
6464
},

‎packages/integrations/sitemap/src/index.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
8888

8989
const { filter, customPages, serialize, entryLimit } = opts;
9090

91-
let finalSiteUrl: URL;
92-
if (config.site) {
93-
finalSiteUrl = new URL(config.base, config.site);
94-
} else {
95-
console.warn(
96-
'The Sitemap integration requires the `site` astro.config option. Skipping.'
97-
);
98-
return;
99-
}
91+
let finalSiteUrl = new URL(config.base, config.site);
10092
const shouldIgnoreStatus = isStatusCodePage(Object.keys(opts.i18n?.locales ?? {}));
10193
let pageUrls = pages
10294
.filter((p) => !shouldIgnoreStatus(p.pathname))

0 commit comments

Comments
 (0)
Please sign in to comment.