mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-17 19:16:58 +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.
|
// Process plugins.
|
||||||
if (siteConfig.plugins) {
|
if (siteConfig.plugins) {
|
||||||
const context = {env, siteDir, siteConfig};
|
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(
|
await Promise.all(
|
||||||
siteConfig.plugins.map(async ({name, options: opts}) => {
|
siteConfig.plugins.map(async ({name, options: opts}) => {
|
||||||
// TODO: Resolve using node_modules as well.
|
// TODO: Resolve using node_modules as well.
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
const plugin = require(path.resolve(__dirname, '../../plugins', name));
|
const Plugin = require(path.resolve(__dirname, '../../plugins', name));
|
||||||
const pluginContent = await plugin.onLoadContent(opts, context);
|
const plugin = new Plugin(opts, context);
|
||||||
const {options, contents} = pluginContent;
|
const {options} = plugin;
|
||||||
contentsStore[options.contentKey] = pluginContent;
|
const contents = await plugin.load();
|
||||||
|
const pluginContents = {
|
||||||
|
options,
|
||||||
|
contents,
|
||||||
|
};
|
||||||
|
contentsStore[options.contentKey] = pluginContents;
|
||||||
await generate(
|
await generate(
|
||||||
generatedFilesDir,
|
generatedFilesDir,
|
||||||
options.cachePath,
|
options.cachePath,
|
||||||
|
|
|
@ -28,15 +28,19 @@ const DEFAULT_OPTIONS = {
|
||||||
cachePath: 'blogMetadata.js',
|
cachePath: 'blogMetadata.js',
|
||||||
};
|
};
|
||||||
|
|
||||||
async function onLoadContent(opts, context) {
|
class DocusaurusContentBlogPlugin {
|
||||||
const options = {...DEFAULT_OPTIONS, ...opts};
|
constructor(opts, context) {
|
||||||
|
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
const {env, siteConfig, siteDir} = context;
|
async load() {
|
||||||
const {pageCount, path: filePath, routeBasePath} = options;
|
const {pageCount, path: filePath, include, routeBasePath} = this.options;
|
||||||
|
const {env, siteConfig, siteDir} = this.context;
|
||||||
const blogDir = path.resolve(siteDir, filePath);
|
const blogDir = path.resolve(siteDir, filePath);
|
||||||
const {baseUrl} = siteConfig;
|
const {baseUrl} = siteConfig;
|
||||||
|
|
||||||
const blogFiles = await globby(options.include, {
|
const blogFiles = await globby(include, {
|
||||||
cwd: blogDir,
|
cwd: blogDir,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -95,12 +99,8 @@ async function onLoadContent(opts, context) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return blogMetadata;
|
||||||
contents: blogMetadata,
|
}
|
||||||
options,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = DocusaurusContentBlogPlugin;
|
||||||
onLoadContent,
|
|
||||||
};
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue