Skip to content

Commit

Permalink
feat(cli): debounce for watching
Browse files Browse the repository at this point in the history
  • Loading branch information
deepfunc committed Sep 29, 2019
1 parent 0ff8eaf commit 4663c43
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
24 changes: 21 additions & 3 deletions packages/cli/core/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const Hook = require('./hook');
const tag = require('./tag');
const walk = require('acorn/dist/walk');
const { isArr } = require('./util/tools');
const { debounce } = require('throttle-debounce');

const initCompiler = require('./init/compiler');
const initParser = require('./init/parser');
Expand Down Expand Up @@ -391,10 +392,17 @@ class Compile extends Hook {
watchOption.ignored = [this.options.target];
}

chokidar.watch([this.options.src], watchOption).on('all', (evt, filepath) => {
if (evt === 'change') {
const pendingFiles = [];

// debounce for watch files
const onFileChanged = debounce(300, () => {
const changedFiles = pendingFiles.splice(0, pendingFiles.length);
if (changedFiles.length > 1) {
// if more then one files changed, build the whole app.
this.start();
} else {
let buildTask = {
changed: path.resolve(filepath),
changed: changedFiles[0],
partial: true,
files: [],
outputAssets: false
Expand All @@ -412,6 +420,16 @@ class Compile extends Hook {
});
}
});

chokidar.watch([this.options.src], watchOption).on('all', (evt, filepath) => {
if (evt === 'change') {
const file = path.resolve(filepath);
if (!pendingFiles.includes(file)) {
pendingFiles.push(file);
}
onFileChanged();
}
});
}

applyCompiler (node, ctx) {
Expand Down
17 changes: 11 additions & 6 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"postcss-selector-parser": "^2.2.3",
"read-metadata": "^1.0.0",
"request": "^2.67.0",
"throttle-debounce": "^2.1.0",
"tildify": "^1.2.0",
"time-ago": "^0.2.1",
"tty-table": "^2.6.15",
Expand Down

0 comments on commit 4663c43

Please sign in to comment.