Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(faster,bundler,core): improve js loader DX #10655

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
emitSiteMessages infra for site errors/warnings
  • Loading branch information
slorber committed Nov 8, 2024
commit d1d401c851afe7f4af1ffba83778e9bcc3c154c3
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);
}