Skip to content

Commit

Permalink
fix: Implement TimeFixPlugin by @egoist to avoid webpack rebuilds
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Nov 7, 2017
1 parent e05da61 commit 6dfe660
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 1 addition & 4 deletions 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'
Expand Down Expand Up @@ -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)
}))
}

Expand Down
4 changes: 4 additions & 0 deletions lib/builder/webpack/base.config.js
Expand Up @@ -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'

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -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({
Expand Down
18 changes: 18 additions & 0 deletions 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
})
}
}

0 comments on commit 6dfe660

Please sign in to comment.