mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 08:07:26 +02:00
v2: prepare to move
This commit is contained in:
parent
dc7ef96849
commit
45736200b0
172 changed files with 0 additions and 0 deletions
67
v2/lib/load/config.js
Normal file
67
v2/lib/load/config.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function loadConfig(siteDir, deleteCache = true) {
|
||||
const configPath = path.resolve(siteDir, 'siteConfig.js');
|
||||
if (deleteCache) {
|
||||
delete require.cache[configPath];
|
||||
}
|
||||
let config = {};
|
||||
if (fs.existsSync(configPath)) {
|
||||
config = require(configPath); // eslint-disable-line
|
||||
}
|
||||
|
||||
const requiredFields = [
|
||||
'title',
|
||||
'tagline',
|
||||
'organizationName',
|
||||
'projectName',
|
||||
'baseUrl'
|
||||
];
|
||||
const optionalFields = [
|
||||
'customDocsPath',
|
||||
'defaultLanguage',
|
||||
'highlight',
|
||||
'markdownPlugins',
|
||||
'configureWebpack',
|
||||
'chainWebpack',
|
||||
'docsUrl',
|
||||
'customFields'
|
||||
];
|
||||
const missingFields = requiredFields.filter(field => !config[field]);
|
||||
if (missingFields && missingFields.length > 0) {
|
||||
throw new Error(
|
||||
`${missingFields.join(', ')} fields are missing in siteConfig.js`
|
||||
);
|
||||
}
|
||||
|
||||
/* Fill default value */
|
||||
const defaultConfig = {
|
||||
customDocsPath: 'docs',
|
||||
docsUrl: 'docs'
|
||||
};
|
||||
Object.keys(defaultConfig).forEach(field => {
|
||||
if (!config[field]) {
|
||||
config[field] = defaultConfig[field];
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
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;
|
||||
|
||||
/* We don't allow useless/ not meaningful field */
|
||||
const allowedFields = [...requiredFields, ...optionalFields, ...customFields];
|
||||
const uselessFields = Object.keys(config).filter(
|
||||
field => !allowedFields.includes(field)
|
||||
);
|
||||
if (uselessFields && uselessFields.length > 0) {
|
||||
throw new Error(
|
||||
`${uselessFields.join(', ')} fields are useless in siteConfig.js`
|
||||
);
|
||||
}
|
||||
|
||||
return config;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue