Skip to content

Commit

Permalink
cleanup, create new webpack ForceTerminatePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Feb 8, 2024
1 parent 7953746 commit 0d2f756
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions packages/docusaurus/src/webpack/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ import CleanWebpackPlugin from './plugins/CleanWebpackPlugin';
import type {Props} from '@docusaurus/types';
import type {Configuration} from 'webpack';

// When building, include the plugin to force terminate building if errors
// happened in the client bundle.
class ForceTerminatePlugin implements webpack.WebpackPluginInstance {
apply(compiler: webpack.Compiler) {
compiler.hooks.done.tap('client:done', (stats) => {
if (stats.hasErrors()) {
const errorsWarnings = stats.toJson('errors-warnings');
logger.error(
`Client bundle compiled with errors therefore further build is impossible.\n${formatStatsErrorMessage(
errorsWarnings,
)}`,
);
process.exit(1);
}
});
}
}

async function createBaseClientConfig({
props,
hydrate,
Expand All @@ -29,10 +47,9 @@ async function createBaseClientConfig({
hydrate: boolean;
minify: boolean;
}): Promise<Configuration> {
const isBuilding = process.argv[2] === 'build';
const baseConfig = await createBaseConfig({props, isServer: false, minify});

const config = merge(baseConfig, {
return merge(baseConfig, {
// Useless, disabled on purpose (errors on existing sites with no
// browserslist config)
// target: 'browserslist',
Expand All @@ -53,28 +70,6 @@ async function createBaseClientConfig({
}),
],
});

// When building, include the plugin to force terminate building if errors
// happened in the client bundle.
if (isBuilding) {
config.plugins?.push({
apply: (compiler) => {
compiler.hooks.done.tap('client:done', (stats) => {
if (stats.hasErrors()) {
const errorsWarnings = stats.toJson('errors-warnings');
logger.error(
`Client bundle compiled with errors therefore further build is impossible.\n${formatStatsErrorMessage(
errorsWarnings,
)}`,
);
process.exit(1);
}
});
},
});
}

return config;
}

// client config when running "docusaurus start"
Expand Down Expand Up @@ -145,6 +140,7 @@ export async function createBuildClientConfig({
await createBaseClientConfig({props, minify, hydrate: true}),
{
plugins: [
new ForceTerminatePlugin(),
// Remove/clean build folders before building bundles.
new CleanWebpackPlugin({verbose: false}),
// Visualize size of webpack output files with an interactive zoomable
Expand Down

0 comments on commit 0d2f756

Please sign in to comment.