Skip to content

Commit

Permalink
Implement BuildConfig opt
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandernst committed Jun 7, 2022
1 parent 7869e74 commit 56937b7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
6 changes: 6 additions & 0 deletions examples/classic/docusaurus.config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions packages/docusaurus-types/src/index.d.ts
Expand Up @@ -17,6 +17,7 @@ import type {
DeepPartial,
} from 'utility-types';
import type {Location} from 'history';
import type {Options} from 'html-minifier-terser';

// === Configuration ===

Expand All @@ -38,6 +39,13 @@ export type PresetConfig =
| false
| null;

export type BuildConfig = {
/**
* Options for [html-minifier-terser](https://github.com/terser/html-minifier-terser)
*/
minify?: Options;
};

export type ThemeConfig = {
[key: string]: unknown;
};
Expand Down Expand Up @@ -210,6 +218,14 @@ export type DocusaurusConfig = {
* @see https://docusaurus.io/docs/api/docusaurus-config#githubPort
*/
githubPort?: string;
/**
* Customize the build options. Currently this object can accept the parameter
* "minify", which must be an object containing the options to be passed to
* the [minifier](https://github.com/DanielRuf/html-minifier-terser).
*
* @see https://docusaurus.io/docs/api/docusaurus-config#buildConfig
*/
buildConfig?: BuildConfig;
/**
* The [theme configuration](https://docusaurus.io/docs/api/themes/configuration)
* object to customize your site UI like navbar and footer.
Expand Down
26 changes: 17 additions & 9 deletions packages/docusaurus/src/client/serverEntry.tsx
Expand Up @@ -66,6 +66,7 @@ It might also require to wrap your client code in ${chalk.cyan('`useEffect`')} h
// Renderer for static-site-generator-webpack-plugin (async rendering).
async function doRender(locals: Locals & {path: string}) {
const {
buildConfig,
routesLocation,
headTags,
preBodyTags,
Expand Down Expand Up @@ -143,15 +144,22 @@ async function doRender(locals: Locals & {path: string}) {

try {
// Minify html with https://github.com/DanielRuf/html-minifier-terser
return await minify(renderedHtml, {
removeComments: false,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyJS: true,
});
return await minify(
renderedHtml,
_.merge(
{},
{
removeComments: false,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyJS: true,
},
buildConfig?.minify,
),
);
} catch (err) {
// prettier-ignore
console.error(chalk.red(`${chalk.bold('[ERROR]')} Minification of page ${chalk.cyan.underline(locals.path)} failed.`));
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus/src/deps.d.ts
Expand Up @@ -35,8 +35,10 @@ declare module 'react-loadable-ssr-addon-v5-slorber' {
declare module '@slorber/static-site-generator-webpack-plugin' {
import type {WebpackPluginInstance, Compiler} from 'webpack';
import type {HelmetServerState} from 'react-helmet-async';
import type {BuildConfig} from '@docusaurus/types';

export type Locals = {
buildConfig: BuildConfig;
routesLocation: {[filePath: string]: string};
generatedFilesDir: string;
headTags: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus/src/server/configValidation.ts
Expand Up @@ -193,6 +193,9 @@ export const ConfigSchema = Joi.object<DocusaurusConfig>({
plugins: Joi.array().items(PluginSchema).default(DEFAULT_CONFIG.plugins),
themes: Joi.array().items(ThemeSchema).default(DEFAULT_CONFIG.themes),
presets: Joi.array().items(PresetSchema).default(DEFAULT_CONFIG.presets),
buildConfig: Joi.object({
minify: Joi.object().optional(),
}).optional(),
themeConfig: Joi.object().unknown().default(DEFAULT_CONFIG.themeConfig),
scripts: Joi.array()
.items(
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus/src/webpack/server.ts
Expand Up @@ -37,7 +37,7 @@ export default async function createServerConfig({
headTags,
preBodyTags,
postBodyTags,
siteConfig: {noIndex, trailingSlash, ssrTemplate},
siteConfig: {buildConfig, noIndex, trailingSlash, ssrTemplate},
} = props;
const config = await createBaseConfig(props, true);

Expand Down Expand Up @@ -70,6 +70,7 @@ export default async function createServerConfig({
new StaticSiteGeneratorPlugin({
entry: 'main',
locals: {
buildConfig: buildConfig ?? {},
baseUrl,
generatedFilesDir,
routesLocation,
Expand Down
14 changes: 14 additions & 0 deletions website/docs/api/docusaurus.config.js.md
Expand Up @@ -274,6 +274,20 @@ module.exports = {
};
```

### `buildConfig` {#buildConfig}

- Type: `Object`

Customize the build options. Currently this object can accept the parameter "minify", which must be an object containing the options to be passed to the [minifier](https://github.com/DanielRuf/html-minifier-terser).

```js title="docusaurus.config.js"
module.exports = {
minify: {
removeComments: false,
},
};
```

### `themeConfig` {#themeConfig}

- Type: `Object`
Expand Down

0 comments on commit 56937b7

Please sign in to comment.