mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-18 10:42:31 +02:00
feat(v2): allow plugins to specify paths to watch (#1272)
This commit is contained in:
parent
8629abf73a
commit
cefee2dec1
6 changed files with 27 additions and 14 deletions
|
@ -31,7 +31,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
{
|
{
|
||||||
name: 'docusaurus-content-blog',
|
name: 'docusaurus-plugin-content-blog',
|
||||||
options: {
|
options: {
|
||||||
include: ['*.md', '*.mdx'],
|
include: ['*.md', '*.mdx'],
|
||||||
path: '../v1/website/blog',
|
path: '../v1/website/blog',
|
||||||
|
|
|
@ -23,6 +23,7 @@ module.exports = {
|
||||||
extends: ['airbnb', 'prettier', 'prettier/react'],
|
extends: ['airbnb', 'prettier', 'prettier/react'],
|
||||||
plugins: ['react-hooks'],
|
plugins: ['react-hooks'],
|
||||||
rules: {
|
rules: {
|
||||||
|
'class-methods-use-this': OFF, // It's a way of allowing private variables.
|
||||||
'no-console': OFF,
|
'no-console': OFF,
|
||||||
'func-names': OFF,
|
'func-names': OFF,
|
||||||
'jsx-a11y/click-events-have-key-events': OFF, // Revisit in future™
|
'jsx-a11y/click-events-have-key-events': OFF, // Revisit in future™
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const _ = require('lodash');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
|
@ -46,10 +47,14 @@ module.exports = async function start(siteDir, cliOptions = {}) {
|
||||||
console.error(chalk.red(err.stack));
|
console.error(chalk.red(err.stack));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
const {plugins} = props;
|
||||||
const docsRelativeDir = props.siteConfig.customDocsPath;
|
const docsRelativeDir = props.siteConfig.customDocsPath;
|
||||||
|
const pluginPaths = _.flatten(
|
||||||
|
plugins.map(plugin => plugin.getPathsToWatch()),
|
||||||
|
);
|
||||||
const fsWatcher = chokidar.watch(
|
const fsWatcher = chokidar.watch(
|
||||||
[
|
[
|
||||||
// TODO: Watch plugin paths (e.g. blog)
|
...pluginPaths,
|
||||||
`../${docsRelativeDir}/**/*.md`,
|
`../${docsRelativeDir}/**/*.md`,
|
||||||
loadConfig.configFileName,
|
loadConfig.configFileName,
|
||||||
'sidebars.json',
|
'sidebars.json',
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {renderRoutes} from 'react-router-config';
|
||||||
|
|
||||||
import routes from '@generated/routes'; // eslint-disable-line
|
import routes from '@generated/routes'; // eslint-disable-line
|
||||||
// TODO: Generalize for blog plugin.
|
// 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 docsMetadatas from '@generated/docsMetadatas'; // eslint-disable-line
|
||||||
import env from '@generated/env'; // eslint-disable-line
|
import env from '@generated/env'; // eslint-disable-line
|
||||||
import docsSidebars from '@generated/docsSidebars'; // eslint-disable-line
|
import docsSidebars from '@generated/docsSidebars'; // eslint-disable-line
|
||||||
|
|
|
@ -88,11 +88,7 @@ module.exports = async function load(siteDir) {
|
||||||
// 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 plugin = new Plugin(opts, context);
|
return new Plugin(opts, context);
|
||||||
return {
|
|
||||||
name,
|
|
||||||
plugin,
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Plugin lifecycle - loadContents().
|
// 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
|
// this in future if there are plugins which need to run in certain order or depend on
|
||||||
// others for data.
|
// others for data.
|
||||||
const pluginsLoadedContents = await Promise.all(
|
const pluginsLoadedContents = await Promise.all(
|
||||||
plugins.map(async ({plugin, name}) => {
|
plugins.map(async plugin => {
|
||||||
if (!plugin.loadContents) {
|
if (!plugin.loadContents) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const name = plugin.getName();
|
||||||
const {options} = plugin;
|
const {options} = plugin;
|
||||||
const contents = await plugin.loadContents();
|
const contents = await plugin.loadContents();
|
||||||
const pluginContents = {
|
const pluginContents = {
|
||||||
|
@ -132,7 +129,7 @@ module.exports = async function load(siteDir) {
|
||||||
addRoute: config => pluginRouteConfigs.push(config),
|
addRoute: config => pluginRouteConfigs.push(config),
|
||||||
};
|
};
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
plugins.map(async ({plugin}, index) => {
|
plugins.map(async (plugin, index) => {
|
||||||
if (!plugin.generateRoutes) {
|
if (!plugin.generateRoutes) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -181,6 +178,7 @@ module.exports = async function load(siteDir) {
|
||||||
generatedFilesDir,
|
generatedFilesDir,
|
||||||
contentsStore,
|
contentsStore,
|
||||||
routesPaths,
|
routesPaths,
|
||||||
|
plugins,
|
||||||
};
|
};
|
||||||
|
|
||||||
return props;
|
return props;
|
||||||
|
|
|
@ -34,14 +34,19 @@ class DocusaurusContentBlogPlugin {
|
||||||
constructor(opts, context) {
|
constructor(opts, context) {
|
||||||
this.options = {...DEFAULT_OPTIONS, ...opts};
|
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
this.contentPath = path.resolve(this.context.siteDir, this.options.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
getName() {
|
||||||
|
return 'docusaurus-plugin-content-blog';
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadContents() {
|
async loadContents() {
|
||||||
const {pageCount, path: filePath, include, routeBasePath} = this.options;
|
const {pageCount, include, routeBasePath} = this.options;
|
||||||
const {env, siteConfig, siteDir} = this.context;
|
const {env, siteConfig} = this.context;
|
||||||
const blogDir = path.resolve(siteDir, filePath);
|
const blogDir = this.contentPath;
|
||||||
const {baseUrl} = siteConfig;
|
|
||||||
|
|
||||||
|
const {baseUrl} = siteConfig;
|
||||||
const blogFiles = await globby(include, {
|
const blogFiles = await globby(include, {
|
||||||
cwd: blogDir,
|
cwd: blogDir,
|
||||||
});
|
});
|
||||||
|
@ -127,6 +132,10 @@ class DocusaurusContentBlogPlugin {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPathsToWatch() {
|
||||||
|
return [this.contentPath];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = DocusaurusContentBlogPlugin;
|
module.exports = DocusaurusContentBlogPlugin;
|
Loading…
Add table
Add a link
Reference in a new issue