From 6dfe6604129b1eff7a6991d6b4259a723181de19 Mon Sep 17 00:00:00 2001 From: Atinux Date: Tue, 7 Nov 2017 11:47:55 +0100 Subject: [PATCH] fix: Implement TimeFixPlugin by @egoist to avoid webpack rebuilds --- lib/builder/builder.js | 5 +---- lib/builder/webpack/base.config.js | 4 ++++ lib/builder/webpack/timefix-plugin.js | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 lib/builder/webpack/timefix-plugin.js diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 38265c5ed1..f2edc82f94 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -1,6 +1,6 @@ import _ from 'lodash' import chokidar from 'chokidar' -import fs, { remove, readFile, writeFile, mkdirp, utimes, existsSync } from 'fs-extra' +import fs, { remove, readFile, writeFile, mkdirp, existsSync } from 'fs-extra' import hash from 'hash-sum' import pify from 'pify' import webpack from 'webpack' @@ -366,9 +366,6 @@ export default class Builder { await mkdirp(dirname(path)) // Write file await writeFile(path, content, 'utf8') - // Fix webpack loop (https://github.com/webpack/watchpack/issues/25#issuecomment-287789288) - const dateFS = Date.now() / 1000 - 1000 - return utimes(path, dateFS, dateFS) })) } diff --git a/lib/builder/webpack/base.config.js b/lib/builder/webpack/base.config.js index e9546072e3..8072e0ff09 100644 --- a/lib/builder/webpack/base.config.js +++ b/lib/builder/webpack/base.config.js @@ -3,6 +3,7 @@ import { cloneDeep } from 'lodash' import { join, resolve } from 'path' import webpack from 'webpack' import { isUrl, urlJoin } from 'utils' +import TimeFixPlugin from './timefix-plugin' /* |-------------------------------------------------------------------------- @@ -104,6 +105,9 @@ export default function webpackBaseConfig(name) { plugins: this.options.build.plugins } + // Add timefix-plugin before others plugins + config.plugins.unshift(new TimeFixPlugin()) + // CSS extraction if (this.options.build.extractCSS) { config.plugins.push(new ExtractTextPlugin({ diff --git a/lib/builder/webpack/timefix-plugin.js b/lib/builder/webpack/timefix-plugin.js new file mode 100644 index 0000000000..9c16918735 --- /dev/null +++ b/lib/builder/webpack/timefix-plugin.js @@ -0,0 +1,18 @@ +// Taken from https://github.com/egoist/poi/blob/3e93c88c520db2d20c25647415e6ae0d3de61145/packages/poi/lib/webpack/timefix-plugin.js#L1-L16 +// Thanks to @egoist +export default class TimeFixPlugin { + constructor(timefix = 11000) { + this.timefix = timefix + } + + apply(compiler) { + compiler.plugin('watch-run', (watching, callback) => { + watching.startTime += this.timefix + callback() + }) + + compiler.plugin('done', stats => { + stats.startTime -= this.timefix + }) + } +}