Skip to content

Commit

Permalink
move minimizers to bundler package
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Sep 19, 2024
1 parent bb48dd8 commit 8dac80f
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 97 deletions.
3 changes: 3 additions & 0 deletions packages/docusaurus-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
},
"license": "MIT",
"dependencies": {
"@docusaurus/faster": "3.5.2",
"@docusaurus/logger": "3.5.2",
"@docusaurus/types": "3.5.2",
"copy-webpack-plugin": "^11.0.0",
"css-minimizer-webpack-plugin": "^5.0.1",
"mini-css-extract-plugin": "^2.9.1",
"terser-webpack-plugin": "^5.3.9",
"tslib": "^2.6.0",
"webpack": "^5.88.1",
"webpackbar": "^6.0.1"
Expand Down
89 changes: 89 additions & 0 deletions packages/docusaurus-bundler/src/currentBundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* 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 webpack from 'webpack';
import WebpackBar from 'webpackbar';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import logger from '@docusaurus/logger';
import type {CurrentBundler, DocusaurusConfig} from '@docusaurus/types';

// We inject a site config slice because the Rspack flag might change place
type SiteConfigSlice = {
future: {
experimental_faster: Pick<
DocusaurusConfig['future']['experimental_faster'],
'rspackBundler'
>;
};
};

function isRspack(siteConfig: SiteConfigSlice): boolean {
return siteConfig.future.experimental_faster.rspackBundler;
}

export async function getCurrentBundler({
siteConfig,
}: {
siteConfig: SiteConfigSlice;
}): Promise<CurrentBundler> {
if (isRspack(siteConfig)) {
// TODO add support for Rspack
logger.error(
'Rspack bundler is not supported yet, will use Webpack instead',
);
}
return {
name: 'webpack',
instance: webpack,
};
}

export async function getCSSExtractPlugin({
currentBundler,
}: {
currentBundler: CurrentBundler;
}): Promise<typeof MiniCssExtractPlugin> {
if (currentBundler.name === 'rspack') {
throw new Error('Rspack bundler is not supported yet');
}
return MiniCssExtractPlugin;
}

export async function getCopyPlugin({
currentBundler,
}: {
currentBundler: CurrentBundler;
}): Promise<typeof CopyWebpackPlugin> {
if (currentBundler.name === 'rspack') {
throw new Error('Rspack bundler is not supported yet');
}
// https://github.com/webpack-contrib/copy-webpack-plugin
return CopyWebpackPlugin;
}

export async function getProgressBarPlugin({
currentBundler,
}: {
currentBundler: CurrentBundler;
}): Promise<typeof WebpackBar> {
if (currentBundler.name === 'rspack') {
class CustomRspackProgressPlugin extends currentBundler.instance
.ProgressPlugin {
constructor({name}: {name: string}) {
// TODO add support for color
// Unfortunately the rspack.ProgressPlugin does not have a name option
// See https://rspack.dev/plugins/webpack/progress-plugin
// @ts-expect-error: adapt Rspack ProgressPlugin constructor
super({prefix: name});
}
}
return CustomRspackProgressPlugin as typeof WebpackBar;
}

return WebpackBar;
}
File renamed without changes.
88 changes: 8 additions & 80 deletions packages/docusaurus-bundler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,85 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import webpack from 'webpack';
import WebpackBar from 'webpackbar';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import logger from '@docusaurus/logger';
import type {CurrentBundler, DocusaurusConfig} from '@docusaurus/types';
export {importSwcJsMinifierOptions, importSwcJsLoaderFactory} from './faster';

// We inject a site config slice because the Rspack flag might change place
type SiteConfigSlice = {
future: {
experimental_faster: Pick<
DocusaurusConfig['future']['experimental_faster'],
'rspackBundler'
>;
};
};
export {
getCurrentBundler,
getCSSExtractPlugin,
getCopyPlugin,
getProgressBarPlugin,
} from './currentBundler';

function isRspack(siteConfig: SiteConfigSlice): boolean {
return siteConfig.future.experimental_faster.rspackBundler;
}

export async function getCurrentBundler({
siteConfig,
}: {
siteConfig: SiteConfigSlice;
}): Promise<CurrentBundler> {
if (isRspack(siteConfig)) {
// TODO add support for Rspack
logger.error(
'Rspack bundler is not supported yet, will use Webpack instead',
);
}
return {
name: 'webpack',
instance: webpack,
};
}

export async function getCSSExtractPlugin({
currentBundler,
}: {
currentBundler: CurrentBundler;
}): Promise<typeof MiniCssExtractPlugin> {
if (currentBundler.name === 'rspack') {
throw new Error('Rspack bundler is not supported yet');
}
return MiniCssExtractPlugin;
}

export async function getCopyPlugin({
currentBundler,
}: {
currentBundler: CurrentBundler;
}): Promise<typeof CopyWebpackPlugin> {
if (currentBundler.name === 'rspack') {
throw new Error('Rspack bundler is not supported yet');
}
// https://github.com/webpack-contrib/copy-webpack-plugin
return CopyWebpackPlugin;
}

export async function getProgressBarPlugin({
currentBundler,
}: {
currentBundler: CurrentBundler;
}): Promise<typeof WebpackBar> {
if (currentBundler.name === 'rspack') {
class CustomRspackProgressPlugin extends currentBundler.instance
.ProgressPlugin {
constructor({name}: {name: string}) {
// TODO add support for color
// Unfortunately the rspack.ProgressPlugin does not have a name option
// See https://rspack.dev/plugins/webpack/progress-plugin
// @ts-expect-error: adapt Rspack ProgressPlugin constructor
super({prefix: name});
}
}
return CustomRspackProgressPlugin as typeof WebpackBar;
}

return WebpackBar;
}
export {getMinimizers} from './minification';
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

import TerserPlugin from 'terser-webpack-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import {importSwcJsMinifierOptions} from '../faster';
import {importSwcJsMinifierOptions} from './faster';
import type {CustomOptions, CssNanoOptions} from 'css-minimizer-webpack-plugin';
import type {WebpackPluginInstance} from 'webpack';
import type {FasterConfig} from '@docusaurus/types';
import type {CurrentBundler, FasterConfig} from '@docusaurus/types';

export type MinimizersConfig = {
faster: Pick<FasterConfig, 'swcJsMinimizer'>;
currentBundler: CurrentBundler;
};

// See https://github.com/webpack-contrib/terser-webpack-plugin#parallel
Expand Down Expand Up @@ -111,8 +112,23 @@ function getCssMinimizer(): WebpackPluginInstance {
}
}

export async function getMinimizers(
async function getWebpackMinimizers(
params: MinimizersConfig,
): Promise<WebpackPluginInstance[]> {
return Promise.all([getJsMinimizer(params), getCssMinimizer()]);
}

async function getRspackMinimizers({
currentBundler,
}: MinimizersConfig): Promise<WebpackPluginInstance[]> {
console.log('currentBundler', currentBundler.name);
throw new Error('TODO Rspack minimizers not implemented yet');
}

export async function getMinimizers(
params: MinimizersConfig,
): Promise<WebpackPluginInstance[]> {
return params.currentBundler.name === 'rspack'
? getRspackMinimizers(params)
: getWebpackMinimizers(params);
}
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"babel-loader": "^9.1.3",
"clsx": "^2.0.0",
"core-js": "^3.31.1",
"terser-webpack-plugin": "^5.3.9",
"tslib": "^2.6.0",
"webpack": "^5.88.1",
"webpack-merge": "^5.9.0",
Expand Down
12 changes: 5 additions & 7 deletions packages/docusaurus-plugin-pwa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import path from 'path';
import {type Configuration} from 'webpack';
import {getProgressBarPlugin} from '@docusaurus/bundler';
import Terser from 'terser-webpack-plugin';
import {getProgressBarPlugin, getMinimizers} from '@docusaurus/bundler';
import {injectManifest} from 'workbox-build';
import {normalizeUrl} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
Expand Down Expand Up @@ -159,11 +158,10 @@ export default function pluginPWA(
// See https://developers.google.com/web/tools/workbox/guides/using-bundlers#webpack
minimizer: debug
? []
: [
new Terser({
test: swSourceFileTest,
}),
],
: await getMinimizers({
faster: props.siteConfig.future.experimental_faster,
currentBundler: props.currentBundler,
}),
},
plugins: [
new props.currentBundler.instance.EnvironmentPlugin({
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
"semver": "^7.5.4",
"serve-handler": "npm:@docusaurus/[email protected]",
"shelljs": "^0.8.5",
"terser-webpack-plugin": "^5.3.10",
"tslib": "^2.6.0",
"update-notifier": "^6.0.2",
"url-loader": "^4.1.1",
Expand Down
7 changes: 4 additions & 3 deletions packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import fs from 'fs-extra';
import path from 'path';
import {getCSSExtractPlugin} from '@docusaurus/bundler';
import {getCSSExtractPlugin, getMinimizers} from '@docusaurus/bundler';
import {md5Hash, getFileLoaderUtils} from '@docusaurus/utils';
import {createJsLoaderFactory, getCustomBabelConfigFilePath} from './utils';
import {getMinimizers} from './minification';
import {loadThemeAliases, loadDocusaurusAliases} from './aliases';
import type {Configuration} from 'webpack';
import type {
Expand Down Expand Up @@ -180,7 +179,9 @@ export async function createBaseConfig({
// Only minimize client bundle in production because server bundle is only
// used for static site generation
minimize: minimizeEnabled,
minimizer: minimizeEnabled ? await getMinimizers({faster}) : undefined,
minimizer: minimizeEnabled
? await getMinimizers({faster, currentBundler: props.currentBundler})
: undefined,
splitChunks: isServer
? false
: {
Expand Down
6 changes: 4 additions & 2 deletions packages/docusaurus/src/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import fs from 'fs-extra';
import path from 'path';
import crypto from 'crypto';
import {getCSSExtractPlugin} from '@docusaurus/bundler';
import {
getCSSExtractPlugin,
importSwcJsLoaderFactory,
} from '@docusaurus/bundler';
import logger from '@docusaurus/logger';
import {BABEL_CONFIG_FILE_NAME} from '@docusaurus/utils';
import {type Configuration} from 'webpack';
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages';
import {importSwcJsLoaderFactory} from '../faster';
import type webpack from 'webpack';
import type {
ConfigureWebpackUtils,
Expand Down

0 comments on commit 8dac80f

Please sign in to comment.