mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-01 07:49:43 +02:00
feat(v2): local plugin path resolve (#1294)
* feat(v2): plugin resolve * nits * Update README.md
This commit is contained in:
parent
14a9c5461f
commit
6b1d2e8c9c
3 changed files with 98 additions and 8 deletions
|
@ -25,8 +25,6 @@ const REQUIRED_FIELDS = [
|
|||
|
||||
const OPTIONAL_FIELDS = [
|
||||
'algolia',
|
||||
'chainWebpack',
|
||||
'configureWebpack',
|
||||
'customDocsPath',
|
||||
'customFields',
|
||||
'defaultLanguage',
|
||||
|
|
|
@ -75,12 +75,26 @@ module.exports = async function load(siteDir) {
|
|||
const context = {env, siteDir, siteConfig};
|
||||
|
||||
// Initialize plugins.
|
||||
const plugins = pluginConfigs.map(({name, options}) => {
|
||||
// TODO: Resolve using node_modules as well.
|
||||
// eslint-disable-next-line
|
||||
const Plugin = require(path.resolve(__dirname, '../../plugins', name));
|
||||
return new Plugin(options, context);
|
||||
});
|
||||
const pluginDir = path.resolve(__dirname, '../../plugins');
|
||||
const plugins = pluginConfigs.map(
|
||||
({name, path: pluginPath = path.join(pluginDir, name), options}) => {
|
||||
let Plugin;
|
||||
// If it exist in provided path or official plugin directory
|
||||
if (pluginPath && fs.existsSync(pluginPath)) {
|
||||
// eslint-disable-next-line
|
||||
Plugin = require(pluginPath);
|
||||
} else {
|
||||
// Resolve using node_modules as well.
|
||||
try {
|
||||
// eslint-disable-next-line
|
||||
Plugin = require(name);
|
||||
} catch (e) {
|
||||
throw new Error(`'${name}' plugin cannot be found.`);
|
||||
}
|
||||
}
|
||||
return new Plugin(options, context);
|
||||
},
|
||||
);
|
||||
|
||||
// Plugin lifecycle - loadContents().
|
||||
// Currently plugins run lifecycle in parallel and are not order-dependent. We could change
|
||||
|
|
78
v2/plugins/README.md
Normal file
78
v2/plugins/README.md
Normal file
|
@ -0,0 +1,78 @@
|
|||
# Docusaurus Plugins
|
||||
|
||||
Plugins are one of the best ways to add functionality to our Docusaurus. Plugins allow third-party developers to extend or modify the default functionality that Docusaurus provides.
|
||||
|
||||
## Installing a Plugin
|
||||
|
||||
A plugin is usually a dependency, so you install them like other packages in node using NPM. However, you don't need to install official plugin provided by Docusaurus team because it comes by default.
|
||||
|
||||
```bash
|
||||
yarn add docusaurus-plugin-name
|
||||
```
|
||||
|
||||
Then you add it in your site's `docusaurus.config.js` plugin arrays:
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
plugins: [
|
||||
{
|
||||
name: 'docusaurus-plugin-content-pages',
|
||||
},
|
||||
{
|
||||
// Plugin with options
|
||||
name: 'docusaurus-plugin-content-blog',
|
||||
options: {
|
||||
include: ['*.md', '*.mdx'],
|
||||
path: '../v1/website/blog',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
Docusaurus can also load plugins from your local folder, you can do something like below:
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
plugins: [
|
||||
{
|
||||
path: '/path/to/docusaurus-local-plugin',
|
||||
},
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
## Basic Plugin Architecture
|
||||
|
||||
For examples, please refer to several official plugins created.
|
||||
|
||||
```js
|
||||
// A JavaScript class
|
||||
class DocusaurusPlugin {
|
||||
constructor(options, context) {
|
||||
|
||||
// options are the plugin options set on config file
|
||||
this.options = {...options};
|
||||
|
||||
// context are provided from docusaurus. Example: siteConfig can be accessed from context
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
getName() {
|
||||
// plugin name identifier
|
||||
}
|
||||
|
||||
async loadContents() {
|
||||
// Content loading hook that runs the first time plugin is loaded
|
||||
// expect a content data structure to be returned
|
||||
}
|
||||
|
||||
async generateRoutes({metadata, actions}) {
|
||||
// This is routes generation hook
|
||||
}
|
||||
|
||||
getPathsToWatch() {
|
||||
// path to watch
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue