From b3c41114fd29a68788578c314789a8b6fc2ebc12 Mon Sep 17 00:00:00 2001 From: Reece Dunham Date: Fri, 8 Jan 2021 19:01:48 +0000 Subject: [PATCH 1/4] refactor(v2): Allow plugins to consume webpack stats Signed-off-by: Reece Dunham --- packages/docusaurus-types/src/index.d.ts | 12 ++++++++++-- packages/docusaurus/src/commands/build.ts | 6 +++--- packages/docusaurus/src/commands/start.ts | 2 +- packages/docusaurus/src/webpack/utils.ts | 10 ++++++---- website/docs/lifecycle-apis.md | 7 +++++-- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index 5612d4522548..28e0906a127e 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -7,7 +7,7 @@ // ESLint doesn't understand types dependencies in d.ts // eslint-disable-next-line import/no-extraneous-dependencies -import {Loader, Configuration} from 'webpack'; +import {Loader, Configuration, Stats} from 'webpack'; import {Command} from 'commander'; import {ParsedUrlQueryInput} from 'querystring'; import {MergeStrategy} from 'webpack-merge'; @@ -178,6 +178,13 @@ export interface Props extends LoadContext, InjectedHtmlTags { plugins: Plugin[]; } +/** + * Same as `Props` but also has webpack stats appended. + */ +export interface PropsPostBuild extends Props { + stats: Stats.ToJsonOutput; +} + export interface PluginContentLoadedActions { addRoute(config: RouteConfig): void; createData(name: string, data: any): Promise; @@ -206,7 +213,7 @@ export interface Plugin { actions: PluginContentLoadedActions; }): void; routesLoaded?(routes: RouteConfig[]): void; // TODO remove soon, deprecated (alpha-60) - postBuild?(props: Props): void; + postBuild?(props: PropsPostBuild): void; postStart?(props: Props): void; configureWebpack?( config: Configuration, @@ -224,6 +231,7 @@ export interface Plugin { postBodyTags?: HtmlTags; }; getSwizzleComponentList?(): string[]; + // TODO before/afterDevServer implementation // translations getTranslationFiles?(): Promise; diff --git a/packages/docusaurus/src/commands/build.ts b/packages/docusaurus/src/commands/build.ts index d4edea5f8b8a..34b1557535c6 100644 --- a/packages/docusaurus/src/commands/build.ts +++ b/packages/docusaurus/src/commands/build.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import chalk = require('chalk'); +import chalk from 'chalk'; import CopyWebpackPlugin from 'copy-webpack-plugin'; import fs from 'fs-extra'; import path from 'path'; @@ -193,7 +193,7 @@ async function buildLocale({ } // Run webpack to build JS bundle (client) and static html files (server). - await compile([clientConfig, serverConfig]); + const finalCompileResult = await compile([clientConfig, serverConfig]); // Remove server.bundle.js because it is not needed. if ( @@ -215,7 +215,7 @@ async function buildLocale({ if (!plugin.postBuild) { return; } - await plugin.postBuild(props); + await plugin.postBuild({...props, stats: finalCompileResult}); }), ); diff --git a/packages/docusaurus/src/commands/start.ts b/packages/docusaurus/src/commands/start.ts index 01482808e099..406bc3f91705 100644 --- a/packages/docusaurus/src/commands/start.ts +++ b/packages/docusaurus/src/commands/start.ts @@ -103,7 +103,7 @@ export default async function start( cwd: siteDir, ignoreInitial: true, usePolling: !!cliOptions.poll, - interval: Number.isInteger(cliOptions.poll) + interval: Number.isInteger((cliOptions.poll as unknown) as never) ? (cliOptions.poll as number) : undefined, }); diff --git a/packages/docusaurus/src/webpack/utils.ts b/packages/docusaurus/src/webpack/utils.ts index d55a3084be48..22ad871bce95 100644 --- a/packages/docusaurus/src/webpack/utils.ts +++ b/packages/docusaurus/src/webpack/utils.ts @@ -198,22 +198,24 @@ function filterWarnings( return warnings.filter((warning) => !isWarningFiltered(warning)); } -export function compile(config: Configuration[]): Promise { +export function compile(config: Configuration[]): Promise { return new Promise((resolve, reject) => { const compiler = webpack(config); compiler.run((err, stats) => { if (err) { reject(new Error(err.toString())); } + // let plugins consume all the stats + const allStats = stats?.toJson('verbose'); if (stats?.hasErrors()) { - stats.toJson('errors-only').errors.forEach((e) => { + allStats.errors.forEach((e) => { console.error(e); }); reject(new Error('Failed to compile with errors.')); } if (stats?.hasWarnings()) { // Custom filtering warnings (see https://github.com/webpack/webpack/issues/7841). - let {warnings} = stats.toJson('errors-warnings'); + let warnings = [...allStats.warnings]; const warningsFilter = ((config[0].stats as Stats.ToJsonOptionsObject) ?.warningsFilter || []) as WarningFilter[]; @@ -226,7 +228,7 @@ export function compile(config: Configuration[]): Promise { console.warn(warning); }); } - resolve(); + resolve(allStats); }); }); } diff --git a/website/docs/lifecycle-apis.md b/website/docs/lifecycle-apis.md index a97d1cc09359..9b6caec277f9 100644 --- a/website/docs/lifecycle-apis.md +++ b/website/docs/lifecycle-apis.md @@ -362,20 +362,23 @@ type Props = { postBodyTags: string; routesPaths: string[]; plugins: Plugin[]; + stats: Stats.ToJsonOutput; }; ``` Example: -```js {4-9} title="docusaurus-plugin/src/index.js" +```js {4-11} title="docusaurus-plugin/src/index.js" module.exports = function (context, options) { return { name: 'docusaurus-plugin', - async postBuild({siteConfig = {}, routesPaths = [], outDir}) { + async postBuild({siteConfig = {}, routesPaths = [], outDir, stats}) { // Print out to console all the rendered routes. routesPaths.map((route) => { console.log(route); }); + // Print out to console all the webpack stats. + console.log(stats); }, }; }; From c3b50cc2164c9a57d4357c02bb13a48f5f977acb Mon Sep 17 00:00:00 2001 From: Reece Dunham Date: Fri, 8 Jan 2021 14:47:45 -0500 Subject: [PATCH 2/4] Warn level only --- packages/docusaurus/src/webpack/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/webpack/utils.ts b/packages/docusaurus/src/webpack/utils.ts index 22ad871bce95..39d37ded07fe 100644 --- a/packages/docusaurus/src/webpack/utils.ts +++ b/packages/docusaurus/src/webpack/utils.ts @@ -206,7 +206,7 @@ export function compile(config: Configuration[]): Promise { reject(new Error(err.toString())); } // let plugins consume all the stats - const allStats = stats?.toJson('verbose'); + const allStats = stats?.toJson('warn'); if (stats?.hasErrors()) { allStats.errors.forEach((e) => { console.error(e); From 3b77348cbf8d43cda7cd66a3fd071c3c6d09a8f9 Mon Sep 17 00:00:00 2001 From: Reece Dunham Date: Fri, 8 Jan 2021 14:51:41 -0500 Subject: [PATCH 3/4] Fix issue --- packages/docusaurus/src/webpack/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/webpack/utils.ts b/packages/docusaurus/src/webpack/utils.ts index 39d37ded07fe..0cf2730a4a73 100644 --- a/packages/docusaurus/src/webpack/utils.ts +++ b/packages/docusaurus/src/webpack/utils.ts @@ -206,7 +206,7 @@ export function compile(config: Configuration[]): Promise { reject(new Error(err.toString())); } // let plugins consume all the stats - const allStats = stats?.toJson('warn'); + const allStats = stats?.toJson('errors-warnings'); if (stats?.hasErrors()) { allStats.errors.forEach((e) => { console.error(e); From 96a42325775a284a4482512846fe7922494771cc Mon Sep 17 00:00:00 2001 From: Reece Dunham Date: Mon, 11 Jan 2021 09:12:43 -0500 Subject: [PATCH 4/4] Revert start.ts change --- packages/docusaurus/src/commands/start.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/commands/start.ts b/packages/docusaurus/src/commands/start.ts index 406bc3f91705..01482808e099 100644 --- a/packages/docusaurus/src/commands/start.ts +++ b/packages/docusaurus/src/commands/start.ts @@ -103,7 +103,7 @@ export default async function start( cwd: siteDir, ignoreInitial: true, usePolling: !!cliOptions.poll, - interval: Number.isInteger((cliOptions.poll as unknown) as never) + interval: Number.isInteger(cliOptions.poll) ? (cliOptions.poll as number) : undefined, });