shama / gaze

:crystal_ball: A globbing fs.watch wrapper built from the best parts of other fine watch libs.
MIT License
1.16k stars 167 forks source link

how to use gaze in a "chained" npm command? #259

Closed DongShi closed 5 years ago

DongShi commented 5 years ago

Hi there. I have the following package.json scripts:

    "scripts": {
        "clean": "rimraf dist/*",
        "copy": "node ./build/copy.js",
        "dev": "npm run clean && npm run copy && webpack-dev-server --config ./build/webpack.config.dev.js",
        "start": "NODE_ENV=development npm run dev",
    },

where in copy.js I setup some gaze watcher to monitor file changes, and if it happens, the new files would be copied to some folder. And ideally, webpack-dev-server would reflect the new changes.

Something like this:

   gaze(`${fromPath}/**/*.*`, (err, watcher) => {
                console.log('Creating file watcher for', fromPath, err || '')
                if (err) return
                const reCopy = filePath => {
                    const relPath = filePath.substr(path.join(__dirname, `../${fromPath}/`).length)
                    // Re copy files
                    console.log(`${fromPath}/${relPath}`, 'changes')
                    shell.cp(`${fromPath}/${relPath}`, `${toPath}/${relPath}`)
                }
                watcher.on('changed', reCopy)
            })

However after I run npm start, it seems gaze would block the change at copy.js so my webpack-dev-server never gets started.

So gaze is a synchronized and blocking process? Is there any suggestion on how to handle this case? Put gaze in a child process?

Thanks

DongShi commented 5 years ago

actually find an answer myself https://stackoverflow.com/questions/52322913/webpack-4-devserver-hmr-plus-full-reload-on-other-file-changes-like-views/52476173

the solution mentioned in the SO post also applies to gaze.