feat(v2): allow plugins to specify paths to watch (#1272)

This commit is contained in:
Yangshun Tay 2019-03-09 13:00:44 -08:00 committed by GitHub
parent 8629abf73a
commit cefee2dec1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 14 deletions

View file

@ -31,7 +31,7 @@ module.exports = {
},
plugins: [
{
name: 'docusaurus-content-blog',
name: 'docusaurus-plugin-content-blog',
options: {
include: ['*.md', '*.mdx'],
path: '../v1/website/blog',

View file

@ -23,6 +23,7 @@ module.exports = {
extends: ['airbnb', 'prettier', 'prettier/react'],
plugins: ['react-hooks'],
rules: {
'class-methods-use-this': OFF, // It's a way of allowing private variables.
'no-console': OFF,
'func-names': OFF,
'jsx-a11y/click-events-have-key-events': OFF, // Revisit in future™

View file

@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
const _ = require('lodash');
const path = require('path');
const fs = require('fs-extra');
const chalk = require('chalk');
@ -46,10 +47,14 @@ module.exports = async function start(siteDir, cliOptions = {}) {
console.error(chalk.red(err.stack));
});
};
const {plugins} = props;
const docsRelativeDir = props.siteConfig.customDocsPath;
const pluginPaths = _.flatten(
plugins.map(plugin => plugin.getPathsToWatch()),
);
const fsWatcher = chokidar.watch(
[
// TODO: Watch plugin paths (e.g. blog)
...pluginPaths,
`../${docsRelativeDir}/**/*.md`,
loadConfig.configFileName,
'sidebars.json',

View file

@ -10,7 +10,7 @@ import {renderRoutes} from 'react-router-config';
import routes from '@generated/routes'; // eslint-disable-line
// TODO: Generalize for blog plugin.
import blogMetadata from '@generated/docusaurus-content-blog/blogMetadata.json'; // eslint-disable-line
import blogMetadata from '@generated/docusaurus-plugin-content-blog/blogMetadata.json'; // eslint-disable-line
import docsMetadatas from '@generated/docsMetadatas'; // eslint-disable-line
import env from '@generated/env'; // eslint-disable-line
import docsSidebars from '@generated/docsSidebars'; // eslint-disable-line

View file

@ -88,11 +88,7 @@ module.exports = async function load(siteDir) {
// TODO: Resolve using node_modules as well.
// eslint-disable-next-line
const Plugin = require(path.resolve(__dirname, '../../plugins', name));
const plugin = new Plugin(opts, context);
return {
name,
plugin,
};
return new Plugin(opts, context);
});
// Plugin lifecycle - loadContents().
@ -102,11 +98,12 @@ module.exports = async function load(siteDir) {
// this in future if there are plugins which need to run in certain order or depend on
// others for data.
const pluginsLoadedContents = await Promise.all(
plugins.map(async ({plugin, name}) => {
plugins.map(async plugin => {
if (!plugin.loadContents) {
return null;
}
const name = plugin.getName();
const {options} = plugin;
const contents = await plugin.loadContents();
const pluginContents = {
@ -132,7 +129,7 @@ module.exports = async function load(siteDir) {
addRoute: config => pluginRouteConfigs.push(config),
};
await Promise.all(
plugins.map(async ({plugin}, index) => {
plugins.map(async (plugin, index) => {
if (!plugin.generateRoutes) {
return;
}
@ -181,6 +178,7 @@ module.exports = async function load(siteDir) {
generatedFilesDir,
contentsStore,
routesPaths,
plugins,
};
return props;

View file

@ -34,14 +34,19 @@ class DocusaurusContentBlogPlugin {
constructor(opts, context) {
this.options = {...DEFAULT_OPTIONS, ...opts};
this.context = context;
this.contentPath = path.resolve(this.context.siteDir, this.options.path);
}
getName() {
return 'docusaurus-plugin-content-blog';
}
async loadContents() {
const {pageCount, path: filePath, include, routeBasePath} = this.options;
const {env, siteConfig, siteDir} = this.context;
const blogDir = path.resolve(siteDir, filePath);
const {baseUrl} = siteConfig;
const {pageCount, include, routeBasePath} = this.options;
const {env, siteConfig} = this.context;
const blogDir = this.contentPath;
const {baseUrl} = siteConfig;
const blogFiles = await globby(include, {
cwd: blogDir,
});
@ -127,6 +132,10 @@ class DocusaurusContentBlogPlugin {
});
});
}
getPathsToWatch() {
return [this.contentPath];
}
}
module.exports = DocusaurusContentBlogPlugin;