fix(v2): fix chokidar not watching files correctly (#1340)

This commit is contained in:
Endilie Yacop Sucipto 2019-04-07 01:52:49 +07:00 committed by Yangshun Tay
parent aa27f82acc
commit 2248b4b927
4 changed files with 26 additions and 5 deletions

View file

@ -44,7 +44,11 @@ class DocusaurusPluginContentBlog {
} }
getPathsToWatch() { getPathsToWatch() {
return [this.contentPath]; const {include = []} = this.options;
const globPattern = include.map(
pattern => `${this.contentPath}/${pattern}`,
);
return [...globPattern];
} }
// Fetches blog contents and returns metadata for the contents. // Fetches blog contents and returns metadata for the contents.

View file

@ -41,7 +41,11 @@ class DocusaurusPluginContentDocs {
} }
getPathsToWatch() { getPathsToWatch() {
return [this.contentPath, this.options.sidebarPath]; const {include = []} = this.options;
const globPattern = include.map(
pattern => `${this.contentPath}/${pattern}`,
);
return [...globPattern, this.options.sidebarPath];
} }
// Fetches blog contents and returns metadata for the contents. // Fetches blog contents and returns metadata for the contents.

View file

@ -30,7 +30,11 @@ class DocusaurusPluginContentPages {
} }
getPathsToWatch() { getPathsToWatch() {
return [this.contentPath]; const {include = []} = this.options;
const globPattern = include.map(
pattern => `${this.contentPath}/${pattern}`,
);
return [...globPattern];
} }
async loadContent() { async loadContent() {

View file

@ -41,19 +41,28 @@ module.exports = async function start(siteDir, cliOptions = {}) {
// Reload files processing. // Reload files processing.
if (!cliOptions.noWatch) { if (!cliOptions.noWatch) {
const reload = () => { const reload = filepath => {
console.log(`${filepath} has changed`);
load(siteDir).catch(err => { load(siteDir).catch(err => {
console.error(chalk.red(err.stack)); console.error(chalk.red(err.stack));
}); });
}; };
const {plugins} = props; const {plugins} = props;
const normalizeToSiteDir = filepath => {
if (filepath && path.isAbsolute(filepath)) {
return path.relative(siteDir, filepath);
}
return filepath;
};
const pluginPaths = _.compact( const pluginPaths = _.compact(
_.flatten( _.flatten(
plugins.map( plugins.map(
plugin => plugin.getPathsToWatch && plugin.getPathsToWatch(), plugin => plugin.getPathsToWatch && plugin.getPathsToWatch(),
), ),
), ),
); ).map(normalizeToSiteDir);
const fsWatcher = chokidar.watch( const fsWatcher = chokidar.watch(
[...pluginPaths, loadConfig.configFileName], [...pluginPaths, loadConfig.configFileName],
{ {