mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-17 11:07:07 +02:00
refactor(v2): convert plugins into a class
This commit is contained in:
parent
006f7050cd
commit
92c7e1f44b
2 changed files with 83 additions and 76 deletions
|
@ -84,14 +84,21 @@ module.exports = async function load(siteDir) {
|
|||
// Process plugins.
|
||||
if (siteConfig.plugins) {
|
||||
const context = {env, siteDir, siteConfig};
|
||||
// Currently runs all plugins in parallel and not order-dependent. We could change
|
||||
// this in future if there's a need.
|
||||
await Promise.all(
|
||||
siteConfig.plugins.map(async ({name, options: opts}) => {
|
||||
// TODO: Resolve using node_modules as well.
|
||||
// eslint-disable-next-line
|
||||
const plugin = require(path.resolve(__dirname, '../../plugins', name));
|
||||
const pluginContent = await plugin.onLoadContent(opts, context);
|
||||
const {options, contents} = pluginContent;
|
||||
contentsStore[options.contentKey] = pluginContent;
|
||||
const Plugin = require(path.resolve(__dirname, '../../plugins', name));
|
||||
const plugin = new Plugin(opts, context);
|
||||
const {options} = plugin;
|
||||
const contents = await plugin.load();
|
||||
const pluginContents = {
|
||||
options,
|
||||
contents,
|
||||
};
|
||||
contentsStore[options.contentKey] = pluginContents;
|
||||
await generate(
|
||||
generatedFilesDir,
|
||||
options.cachePath,
|
||||
|
|
|
@ -28,15 +28,19 @@ const DEFAULT_OPTIONS = {
|
|||
cachePath: 'blogMetadata.js',
|
||||
};
|
||||
|
||||
async function onLoadContent(opts, context) {
|
||||
const options = {...DEFAULT_OPTIONS, ...opts};
|
||||
class DocusaurusContentBlogPlugin {
|
||||
constructor(opts, context) {
|
||||
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
const {env, siteConfig, siteDir} = context;
|
||||
const {pageCount, path: filePath, routeBasePath} = options;
|
||||
async load() {
|
||||
const {pageCount, path: filePath, include, routeBasePath} = this.options;
|
||||
const {env, siteConfig, siteDir} = this.context;
|
||||
const blogDir = path.resolve(siteDir, filePath);
|
||||
const {baseUrl} = siteConfig;
|
||||
|
||||
const blogFiles = await globby(options.include, {
|
||||
const blogFiles = await globby(include, {
|
||||
cwd: blogDir,
|
||||
});
|
||||
|
||||
|
@ -95,12 +99,8 @@ async function onLoadContent(opts, context) {
|
|||
});
|
||||
}
|
||||
|
||||
return {
|
||||
contents: blogMetadata,
|
||||
options,
|
||||
};
|
||||
return blogMetadata;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
onLoadContent,
|
||||
};
|
||||
module.exports = DocusaurusContentBlogPlugin;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue