mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-08 22:03:01 +02:00
chore(v2): improve typing
This commit is contained in:
parent
cbf80bef5a
commit
38af934464
8 changed files with 52 additions and 39 deletions
|
@ -58,7 +58,7 @@ export async function start(
|
||||||
return filepath;
|
return filepath;
|
||||||
};
|
};
|
||||||
|
|
||||||
const pluginPaths = _.compact(
|
const pluginPaths: string[] = _.compact(
|
||||||
_.flatten<string | undefined>(
|
_.flatten<string | undefined>(
|
||||||
plugins.map(plugin => plugin.getPathsToWatch && plugin.getPathsToWatch()),
|
plugins.map(plugin => plugin.getPathsToWatch && plugin.getPathsToWatch()),
|
||||||
),
|
),
|
||||||
|
|
|
@ -12,8 +12,9 @@ module.exports = {
|
||||||
projectName: 'hello',
|
projectName: 'hello',
|
||||||
baseUrl: '/',
|
baseUrl: '/',
|
||||||
useLessField: 'what',
|
useLessField: 'what',
|
||||||
superman: 'lol',
|
customFields: {
|
||||||
admin: 'endi',
|
admin: 'endi',
|
||||||
customFields: ['admin', 'superman'],
|
superman: 'lol',
|
||||||
|
},
|
||||||
url: 'https://docusaurus.io',
|
url: 'https://docusaurus.io',
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,11 +20,14 @@ describe('loadConfig', () => {
|
||||||
`
|
`
|
||||||
Object {
|
Object {
|
||||||
"baseUrl": "/",
|
"baseUrl": "/",
|
||||||
|
"customFields": Object {},
|
||||||
"favicon": "img/docusaurus.ico",
|
"favicon": "img/docusaurus.ico",
|
||||||
"organizationName": "endiliey",
|
"organizationName": "endiliey",
|
||||||
"plugins": Any<Array>,
|
"plugins": Any<Array>,
|
||||||
"projectName": "hello",
|
"projectName": "hello",
|
||||||
"tagline": "Hello World",
|
"tagline": "Hello World",
|
||||||
|
"themeConfig": Object {},
|
||||||
|
"themes": Array [],
|
||||||
"title": "Hello",
|
"title": "Hello",
|
||||||
"url": "https://docusaurus.io",
|
"url": "https://docusaurus.io",
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import _ from 'lodash';
|
||||||
import importFresh from 'import-fresh';
|
import importFresh from 'import-fresh';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {CONFIG_FILE_NAME} from '../constants';
|
import {CONFIG_FILE_NAME} from '../constants';
|
||||||
|
import {PresetConfig} from './presets';
|
||||||
|
|
||||||
export interface DocusaurusConfig {
|
export interface DocusaurusConfig {
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
|
@ -21,14 +22,16 @@ export interface DocusaurusConfig {
|
||||||
url: string;
|
url: string;
|
||||||
organizationName?: string;
|
organizationName?: string;
|
||||||
projectName?: string;
|
projectName?: string;
|
||||||
customFields?: string[];
|
|
||||||
githubHost?: string;
|
githubHost?: string;
|
||||||
plugins?: PluginConfig[];
|
plugins?: PluginConfig[];
|
||||||
presets?: any[];
|
themes?: PluginConfig[];
|
||||||
|
presets?: PresetConfig[];
|
||||||
themeConfig?: {
|
themeConfig?: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
};
|
};
|
||||||
|
customFields?: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const REQUIRED_FIELDS = ['baseUrl', 'favicon', 'tagline', 'title', 'url'];
|
const REQUIRED_FIELDS = ['baseUrl', 'favicon', 'tagline', 'title', 'url'];
|
||||||
|
@ -39,14 +42,25 @@ const OPTIONAL_FIELDS = [
|
||||||
'customFields',
|
'customFields',
|
||||||
'githubHost',
|
'githubHost',
|
||||||
'plugins',
|
'plugins',
|
||||||
|
'themes',
|
||||||
'presets',
|
'presets',
|
||||||
'themeConfig',
|
'themeConfig',
|
||||||
];
|
];
|
||||||
|
|
||||||
const DEFAULT_CONFIG: {
|
const DEFAULT_CONFIG: {
|
||||||
|
plugins: PluginConfig[];
|
||||||
|
themes: PluginConfig[];
|
||||||
|
customFields: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
themeConfig: {
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
} = {
|
} = {
|
||||||
plugins: [],
|
plugins: [],
|
||||||
|
themes: [],
|
||||||
|
customFields: {},
|
||||||
|
themeConfig: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatFields(fields: string[]): string {
|
function formatFields(fields: string[]): string {
|
||||||
|
@ -74,16 +88,8 @@ export function loadConfig(siteDir: string): DocusaurusConfig {
|
||||||
// Merge default config with loaded config.
|
// Merge default config with loaded config.
|
||||||
const config: DocusaurusConfig = {...DEFAULT_CONFIG, ...loadedConfig};
|
const config: DocusaurusConfig = {...DEFAULT_CONFIG, ...loadedConfig};
|
||||||
|
|
||||||
// User's own array of custom fields/
|
|
||||||
// e.g: if they want to include some.field so they can access it later from `props.siteConfig`.
|
|
||||||
const {customFields = []} = config;
|
|
||||||
|
|
||||||
// Don't allow unrecognized fields.
|
// Don't allow unrecognized fields.
|
||||||
const allowedFields = [
|
const allowedFields = [...REQUIRED_FIELDS, ...OPTIONAL_FIELDS];
|
||||||
...REQUIRED_FIELDS,
|
|
||||||
...OPTIONAL_FIELDS,
|
|
||||||
...customFields,
|
|
||||||
];
|
|
||||||
const unrecognizedFields = Object.keys(config).filter(
|
const unrecognizedFields = Object.keys(config).filter(
|
||||||
field => !allowedFields.includes(field),
|
field => !allowedFields.includes(field),
|
||||||
);
|
);
|
||||||
|
|
|
@ -134,7 +134,7 @@ ${Object.keys(registry)
|
||||||
genRoutes,
|
genRoutes,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const props = {
|
const props: Props = {
|
||||||
siteConfig,
|
siteConfig,
|
||||||
siteDir,
|
siteDir,
|
||||||
outDir,
|
outDir,
|
||||||
|
|
|
@ -16,15 +16,17 @@ export interface Preset {
|
||||||
themes?: PluginConfig[];
|
themes?: PluginConfig[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type PresetConfig = [string, Object] | string;
|
||||||
|
|
||||||
export function loadPresets(
|
export function loadPresets(
|
||||||
context: LoadContext,
|
context: LoadContext,
|
||||||
): {
|
): {
|
||||||
plugins: PluginConfig[];
|
plugins: PluginConfig[];
|
||||||
themes: PluginConfig[];
|
themes: PluginConfig[];
|
||||||
} {
|
} {
|
||||||
const presets: any[] = context.siteConfig.presets || [];
|
const presets: PresetConfig[] = context.siteConfig.presets || [];
|
||||||
const plugins: (PluginConfig[] | undefined)[] = [];
|
const unflatPlugins: (PluginConfig[])[] = [];
|
||||||
const themes: (PluginConfig[] | undefined)[] = [];
|
const unflatThemes: (PluginConfig[])[] = [];
|
||||||
|
|
||||||
presets.forEach(presetItem => {
|
presets.forEach(presetItem => {
|
||||||
let presetModuleImport;
|
let presetModuleImport;
|
||||||
|
@ -38,12 +40,12 @@ export function loadPresets(
|
||||||
const presetModule = importFresh(presetModuleImport);
|
const presetModule = importFresh(presetModuleImport);
|
||||||
const preset: Preset = presetModule(context, presetOptions);
|
const preset: Preset = presetModule(context, presetOptions);
|
||||||
|
|
||||||
plugins.push(preset.plugins);
|
preset.plugins && unflatPlugins.push(preset.plugins);
|
||||||
themes.push(preset.themes);
|
preset.themes && unflatThemes.push(preset.themes);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
plugins: _.compact(_.flatten<PluginConfig | undefined>(plugins)),
|
plugins: _.compact(_.flatten<PluginConfig>(unflatPlugins)),
|
||||||
themes: _.compact(_.flatten<PluginConfig | undefined>(themes)),
|
themes: _.compact(_.flatten<PluginConfig>(unflatThemes)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,20 +59,19 @@ You may also check the doc for [Deployment](deployment.md) for more information
|
||||||
|
|
||||||
### Custom configurations
|
### Custom configurations
|
||||||
|
|
||||||
You may have your own custom fields. And `docusaurus.config.js` will be aware of the fields and guard your configuration from unknown fields.
|
Docusaurus guards `docusaurus.config.js` from unknown fields. To add a custom field, define it on `customFields`
|
||||||
|
|
||||||
- [customFields](docusaurus.config.js.md#customFields)
|
- [customFields](docusaurus.config.js.md#customFields)
|
||||||
|
|
||||||
To add a custom field, add the field name to `customFields`. Then, you may use the field for your customization data:
|
Example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// docusaurus.config.js
|
// docusaurus.config.js
|
||||||
module.exports = {
|
module.exports = {
|
||||||
customFields: ['seo'],
|
customFields: {
|
||||||
seo: {
|
'image': '',
|
||||||
image: '',
|
'keywords': []
|
||||||
keywords: [],
|
}
|
||||||
},
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -203,21 +203,23 @@ module.exports = {
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### `customFields` and other custom fields
|
### `customFields`
|
||||||
|
|
||||||
Docusaurus guards `docusaurus.config.js` from unknown fields. To add a custom field, add the field name to `customFields`, then add the field to the module.
|
Docusaurus guards `docusaurus.config.js` from unknown fields. To add a custom field, define it on `customFields`
|
||||||
|
|
||||||
- Type: `string[]`
|
- Type: `Object`
|
||||||
|
|
||||||
```js
|
```jsx
|
||||||
// docusaurus.config.js
|
// docusaurus.config.js
|
||||||
module.exports = {
|
module.exports = {
|
||||||
customFields: ['seo'],
|
customFields: {
|
||||||
seo: // ... the actual custom field
|
admin: 'endi',
|
||||||
|
superman: 'lol'
|
||||||
|
},
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Attempting to add custom fields without indicating in `customFields` will lead to error in build time:
|
Attempting to add unknown field in the config will lead to error in build time:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
Error: The field(s) 'foo', 'bar' are not recognized in docusaurus.config.js
|
Error: The field(s) 'foo', 'bar' are not recognized in docusaurus.config.js
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue