Skip to content

Commit

Permalink
emitSiteMessages infra for site errors/warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Nov 8, 2024
1 parent 91e41a3 commit d1d401c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/docusaurus/src/server/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import {generateSiteFiles} from './codegen/codegen';
import {getRoutesPaths, handleDuplicateRoutes} from './routes';
import {createSiteStorage} from './storage';
import {emitSiteMessages} from './siteMessages';
import type {LoadPluginsResult} from './plugins/plugins';
import type {
DocusaurusConfig,
Expand Down Expand Up @@ -54,7 +55,9 @@ export type LoadContextParams = {
localizePath?: boolean;
};

export type LoadSiteParams = LoadContextParams;
export type LoadSiteParams = LoadContextParams & {
isReload?: boolean;
};

export type Site = {
props: Props;
Expand Down Expand Up @@ -236,7 +239,7 @@ async function createSiteFiles({
* lifecycles to generate content and other data. It is side-effect-ful because
* it generates temp files in the `.docusaurus` folder for the bundler.
*/
export async function loadSite(params: LoadContextParams): Promise<Site> {
export async function loadSite(params: LoadSiteParams): Promise<Site> {
const context = await PerfLogger.async('Load context', () =>
loadContext(params),
);
Expand All @@ -252,14 +255,22 @@ export async function loadSite(params: LoadContextParams): Promise<Site> {
globalData,
});

// For now, we don't re-emit messages on site reloads, it's too verbose
if (!params.isReload) {
await emitSiteMessages({site});
}

return site;
}

export async function reloadSite(site: Site): Promise<Site> {
// TODO this can be optimized, for example:
// - plugins loading same data as before should not recreate routes/bundles
// - codegen does not need to re-run if nothing changed
return loadSite(site.params);
return loadSite({
...site.params,
isReload: true,
});
}

export async function reloadSitePlugin(
Expand Down
67 changes: 67 additions & 0 deletions packages/docusaurus/src/server/siteMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
import _ from 'lodash';
import {getCustomBabelConfigFilePath} from '@docusaurus/babel';
import logger from '@docusaurus/logger';
import type {Site} from './site';

type Params = {site: Site};

type SiteMessage = {type: 'warning' | 'error'; message: string};

type SiteMessageCreator = (params: Params) => Promise<SiteMessage[]>;

const uselessBabelConfigMessages: SiteMessageCreator = async ({site}) => {
const {
props: {siteDir, siteConfig},
} = site;
if (siteConfig.future.experimental_faster.swcJsLoader) {
const babelConfigFilePath = await getCustomBabelConfigFilePath(siteDir);
if (babelConfigFilePath) {
return [
{
type: 'warning',
message: `Your site is using the SWC js loader. You can safely remove the Babel config file at ${logger.code(
path.relative(process.cwd(), babelConfigFilePath),
)}.`,
},
];
}
}
return [];
};

async function collectAllSiteMessages(params: Params): Promise<SiteMessage[]> {
const messageCreators: SiteMessageCreator[] = [uselessBabelConfigMessages];
return (
await Promise.all(
messageCreators.map((createMessages) => createMessages(params)),
)
).flat();
}

function printSiteMessages(siteMessages: SiteMessage[]): void {
const [errors, warnings] = _.partition(
siteMessages,
(sm) => sm.type === 'error',
);
if (errors.length > 0) {
logger.error(`Docusaurus site errors:
- ${errors.map((sm) => sm.message).join('\n- ')}`);
}
if (warnings.length > 0) {
logger.warn(`Docusaurus site warnings:
- ${warnings.map((sm) => sm.message).join('\n- ')}`);
}
}

export async function emitSiteMessages(params: Params): Promise<void> {
const siteMessages = await collectAllSiteMessages(params);
printSiteMessages(siteMessages);
}

0 comments on commit d1d401c

Please sign in to comment.