Skip to content

Commit

Permalink
feat: 更换watch到watchpack
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Mar 19, 2019
1 parent aa3396f commit 8c6229a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 122 deletions.
3 changes: 2 additions & 1 deletion src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = class Asset {
if (this.__mtime) {
return this.__mtime;
}
return this.stats.mtimeMs;
return this.stats.mtime.getTime();
} catch (err) {
return 0;
}
Expand All @@ -58,6 +58,7 @@ module.exports = class Asset {
} else {
this.__content = fs.readFileSync(this.filePath);
}
this.__stats = fs.statSync(this.filePath);
} catch (e) {
this.__content = null;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Mpbuilder {

async watch() {
await this.run();
log.info('开启watching');
this.watching.watch();
}

Expand Down
1 change: 0 additions & 1 deletion src/loader/ts-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ module.exports = function typescript(asset, options = {}) {
const formattedDiagnostics = Object.keys(groupedByFile)
.map((file) => groupedByFile[file])
.map((diagnostics) => codeframe(diagnostics, asset.dir));
console.log('formattedDiagnostics', formattedDiagnostics);
formattedDiagnostics.forEach((diagnostic) => {
if (diagnostic.category === ts.DiagnosticCategory.Error) {
console.log(chalk.red('[ts-loader-error]'), asset.path);
Expand Down
155 changes: 35 additions & 120 deletions src/watching.js
Original file line number Diff line number Diff line change
@@ -1,124 +1,7 @@
// class Watching {
// constructor(mpb, watchOptions, handler) {
// this.mpb = mpb;
// this.startTime = null;
// this.invalid = false;
// this.handler = handler;
// this.callbacks = [];
// this.closed = false;
// if (typeof watchOptions === 'number') {
// this.watchOptions = {
// aggregateTimeout: watchOptions
// };
// } else if (watchOptions && typeof watchOptions === 'object') {
// this.watchOptions = Object.assign({}, watchOptions);
// } else {
// this.watchOptions = {};
// }
// this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200;
// this.running = true;
// }

// _done(err, compilation) {
// this.running = false;
// if (this.invalid) return this._go();

// const stats = compilation ? this._getStats(compilation) : null;
// if (err) {
// this.compiler.applyPlugins('failed', err);
// this.handler(err, stats);
// return;
// }

// this.compiler.applyPlugins('done', stats);
// this.handler(null, stats);
// if (!this.closed) {
// this.watch(
// compilation.fileDependencies,
// compilation.contextDependencies,
// compilation.missingDependencies
// );
// }
// this.callbacks.forEach((cb) => cb());
// this.callbacks.length = 0;
// }

// watch(files, dirs, missing) {
// this.pausedWatcher = null;
// this.watcher = this.compiler.watchFileSystem.watch(
// files,
// dirs,
// missing,
// this.startTime,
// this.watchOptions,
// (
// err,
// filesModified,
// contextModified,
// missingModified,
// fileTimestamps,
// contextTimestamps
// ) => {
// this.pausedWatcher = this.watcher;
// this.watcher = null;
// if (err) return this.handler(err);

// this.mpb.fileTimestamps = fileTimestamps;
// this.mpb.contextTimestamps = contextTimestamps;
// this.invalidate();
// },
// (fileName, changeTime) => {
// this.mpb.applyPlugins('invalid', fileName, changeTime);
// }
// );
// }

// invalidate(callback) {
// if (callback) {
// this.callbacks.push(callback);
// }
// if (this.watcher) {
// this.pausedWatcher = this.watcher;
// this.watcher.pause();
// this.watcher = null;
// }
// if (this.running) {
// this.invalid = true;
// return false;
// }
// }

// close(callback) {
// if (callback === undefined) callback = function() {};

// this.closed = true;
// if (this.watcher) {
// this.watcher.close();
// this.watcher = null;
// }
// if (this.pausedWatcher) {
// this.pausedWatcher.close();
// this.pausedWatcher = null;
// }
// if (this.running) {
// this.invalid = true;
// this._done = () => {
// this.compiler.applyPlugins('watch-close');
// callback();
// };
// } else {
// this.compiler.applyPlugins('watch-close');
// callback();
// }
// }
// }

// module.exports = Watching;

const chokidar = require('chokidar');
const _ = require('lodash');
const chalk = require('chalk');
const log = require('./log');
const Watchpack = require('watchpack');

module.exports = class Watching {
constructor(mpb, handle = () => {}) {
Expand All @@ -127,9 +10,41 @@ module.exports = class Watching {
this.pending = false;
this.pendingPaths = [];
this.handle = handle;
this.watchTimer = Date.now();
this.watcher = null;
}

watch() {
const files = this.mpb.assetManager.getWatchFiles();
if (this.watcher) {
this.listenWatcherEvent();
this.watcher.watch(files, [], this.watchTimer);
} else {
this.watcher = new Watchpack({
aggregateTimeout: 1000,
poll: true
});
this.listenWatcherEvent();
this.watcher.watch(files, [], this.watchTimer);
}
this.files = files;
}

listenWatcherEvent() {
this.watcher.on('aggregated', (changes, removals) => {
this.watchTimer = Date.now();
this.watcher.close();
this.watcher = null;
changes.forEach((filePath) => {
this.handleWatch(filePath, 'change');
});
removals.forEach((filePath) => {
this.handleWatch(filePath, 'unlink');
});
});
}

watch(missing) {
watch1(missing) {
const files = this.mpb.assetManager.getWatchFiles();
if (this.watcher) {
const addPaths = _.difference(files, this.files);
Expand All @@ -144,13 +59,13 @@ module.exports = class Watching {
this.watcher = chokidar.watch(files, {
ignored: missing,
persistent: true,
usePolling: true,
ignoreInitial: true
// awaitWriteFinish: {
// stabilityThreshold: 2000,
// pollInterval: 100
// }
});
log.info('开启watching');
this.watcher.on('add', (/* path */) => {
// 不监听文件夹, 所有没有add事件
// this.handleWatch(path, 'add');
Expand Down

0 comments on commit 8c6229a

Please sign in to comment.