Skip to content

Commit fb4a5fb

Browse files
thadguidryslorber
andauthored
docs: mention equivalent config syntaxes (facebook#8951)
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
1 parent 8beeb81 commit fb4a5fb

File tree

2 files changed

+90
-20
lines changed

2 files changed

+90
-20
lines changed

website/docs/api/docusaurus.config.js.mdx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,33 @@ Refer to the Getting Started [**Configuration**](docs/configuration.mdx) for exa
1616

1717
`docusaurus.config.js` contains configurations for your site and is placed in the root directory of your site.
1818

19-
It usually exports a site configuration object:
19+
This file is run in Node.js using the [**CommonJS**](https://flaviocopes.com/commonjs/) module system, and should export a site configuration object, or a function that creates it.
20+
21+
Examples:
2022

2123
```js title="docusaurus.config.js"
2224
module.exports = {
23-
// site config...
25+
title: 'Docusaurus',
26+
url: 'https://docusaurus.io',
27+
// your site config ...
2428
};
2529
```
2630

27-
<details>
28-
<summary>Config files also support config creator functions and async code.</summary>
29-
3031
```js title="docusaurus.config.js"
31-
module.exports = function configCreator() {
32+
module.exports = async function createConfigAsync() {
3233
return {
33-
// site config...
34+
title: 'Docusaurus',
35+
url: 'https://docusaurus.io',
36+
// your site config ...
3437
};
3538
};
3639
```
3740

38-
```js title="docusaurus.config.js"
39-
module.exports = async function configCreatorAsync() {
40-
return {
41-
// site config...
42-
};
43-
};
44-
```
41+
:::tip
4542

46-
```js title="docusaurus.config.js"
47-
module.exports = Promise.resolve({
48-
// site config...
49-
});
50-
```
43+
Refer to [**Syntax to declare `docusaurus.config.js`**](docs/configuration.mdx#syntax-to-declare-docusaurus-config) for a more exhaustive list of examples and explanations.
5144

52-
</details>
45+
:::
5346

5447
## Required fields {#required-fields}
5548

website/docs/configuration.mdx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,83 @@ Docusaurus has a unique take on configurations. We encourage you to congregate i
1616

1717
Keeping a well-maintained `docusaurus.config.js` helps you, your collaborators, and your open source contributors to be able to focus on documentation while still being able to customize the site.
1818

19+
## Syntax to declare `docusaurus.config.js` {#syntax-to-declare-docusaurus-config}
20+
21+
The `docusaurus.config.js` file is run in Node.js and should export either:
22+
23+
- a **config object**
24+
- a **function** that creates the config object
25+
26+
:::info
27+
28+
The `docusaurus.config.js` file only supports the [**CommonJS**](https://flaviocopes.com/commonjs/) module system:
29+
30+
- **Required:** use `module.exports = /* your config*/` to export your Docusaurus config
31+
- **Optional:** use `require("lib")` to import Node.js packages
32+
- **Optional:** use `await import("lib")` (dynamic import) in an async function to import ESM-Only Node.js packages
33+
34+
:::
35+
36+
Node.js gives us the ability to declare our Docusaurus configuration in various **equivalent ways**, and all the following config examples lead to the exact same result:
37+
38+
```js title="docusaurus.config.js"
39+
module.exports = {
40+
title: 'Docusaurus',
41+
url: 'https://docusaurus.io',
42+
// your site config ...
43+
};
44+
```
45+
46+
```js title="docusaurus.config.js"
47+
const config = {
48+
title: 'Docusaurus',
49+
url: 'https://docusaurus.io',
50+
// your site config ...
51+
};
52+
53+
module.exports = config;
54+
```
55+
56+
```js title="docusaurus.config.js"
57+
module.exports = function configCreator() {
58+
return {
59+
title: 'Docusaurus',
60+
url: 'https://docusaurus.io',
61+
// your site config ...
62+
};
63+
};
64+
```
65+
66+
```js title="docusaurus.config.js"
67+
module.exports = async function createConfigAsync() {
68+
return {
69+
title: 'Docusaurus',
70+
url: 'https://docusaurus.io',
71+
// your site config ...
72+
};
73+
};
74+
```
75+
76+
:::tip Using ESM-only packages
77+
78+
Using an async config creator can be useful to import ESM-only modules (notably most Remark plugins). It is possible to import such modules thanks to dynamic imports:
79+
80+
```js title="docusaurus.config.js"
81+
module.exports = async function createConfigAsync() {
82+
// Use a dynamic import instead of require('esm-lib')
83+
// highlight-next-line
84+
const lib = await import('lib');
85+
86+
return {
87+
title: 'Docusaurus',
88+
url: 'https://docusaurus.io',
89+
// rest of your site config...
90+
};
91+
};
92+
```
93+
94+
:::
95+
1996
## What goes into a `docusaurus.config.js`? {#what-goes-into-a-docusaurusconfigjs}
2097

2198
You should not have to write your `docusaurus.config.js` from scratch even if you are developing your site. All templates come with a `docusaurus.config.js` that includes defaults for the common options.

0 commit comments

Comments
 (0)