gitbook-cli icon indicating copy to clipboard operation
gitbook-cli copied to clipboard

Hot Reload not working

Open dampion opened this issue 2 years ago • 3 comments

My environment information as following:

OS: Ventura 13.2.1 Gitbook version: 3.2.3 CLI version: 2.3.2 npm version: 6.14.12 node version: 10.24.1

I encounter the problems below when I run gitbook serve , which is running well but when I edit the files, which should show the following message

Restart after change in file xxx.md

but it didn't. Nothing happened. I need to ctrl+c to close the serve and restart the gitbook server manually.

But my colleague's (MBP BigSur 11.6.8 ) running well. Please don't tell the difference of the intel and mac chips make this reason........

dampion avatar Mar 16 '23 03:03 dampion

Have you solved it so far

ZouZhiLong avatar Mar 23 '23 03:03 ZouZhiLong

Have you solved it so far

Not yet, but I stop to use VuePress which is better.

dampion avatar Mar 23 '23 06:03 dampion

Came across this on my linux machine, and issue seems to be the glob patterns not matching when using the chokidar file watching library.

Reproducing

Wherever you have your global gitbook install, look for the watch.js file (for me it was ~/.gitbook/versions/3.2.3/lib/cli/watch.js) and try to hard code in the toWatch files:

var toWatch = [
        'book.json', 'book.js', '_layouts/**', 
        "content/README.md",
    ];

Although the '**/*.md' glob pattern is automatically generated, it seems that on different platforms it fails to match, and only hard wiring in "content/README.md" triggered the watcher.

Solution

One way of automating this is procedurally generating a full list of files to watch - in my case all the relevant files existed in the ./content directory, so injecting the following into the watch.js file

const fs = require('fs'); // ADDED

<-snip->

function watch(dir) {
    var d = Promise.defer();
    dir = path.resolve(dir);

    watchFolder = './content'; // ADDED
    var toWatch = [
        'book.json', 'book.js', '_layouts/**',
        ...fs.readdirSync(watchFolder).map(file => `${watchFolder}/${file}`) // ADDED
    ];

<-snip->

will have your releveant files being watched.

Of couse this is a very direct change and can probably be implemented more elegantly via plugins, but this would be the starting point.

creamy-seas avatar Jan 11 '24 15:01 creamy-seas