如果用户需要根据某些条件配置可选的 docusaurus 配置密钥,那么在 docusaurus.config.js 文件中解决该问题的最佳方法是什么?例如:
module.exports = {
/* If condition is true then */
showLastUpdateAuthor: true,
/* otherwise set it to false */
//Other config key value...
}
这是我尝试过的并且有效。有更好的方法来处理这个问题吗? 此处讨论了通过扩展语法获得的见解。
const branch = require('child_process')
.execSync('git branch --show-current')
.toString().trim();
module.exports = {
...(branch != 'main') ? {showLastUpdateAuthor: true,}:{showLastUpdateAuthor: false,},
//Other config key value...
}
它起作用了。还有其他更好的办法吗?
const branch = require('child_process')
.execSync('git branch --show-current')
.toString().trim();
module.exports = {
...(branch != 'main') ? {showLastUpdateAuthor: true,}:{showLastUpdateAuthor: false,},
//Other config key value...
}
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });
module.exports = function (context, options) {
return {
name: 'docusaurus-plugin-define-env',
configureWebpack(config, isServer, utils) {
// Convert all process.env variables into a format suitable for DefinePlugin
const envVariables = Object.keys(process.env).reduce((env, key) => {
env[`process.env.${key}`] = JSON.stringify(process.env[key]);
return env;
}, {});
return {
plugins: [
new webpack.DefinePlugin(envVariables),
],
};
},
};
};