12

Sometimes while running webpack in watch mode and editing source files I am not sure whether webpack has packed my changes or not.

Is there a way to print a timestamp to the console each time webpack updates the bundle?

Bjorn Reppen
  • 22,007
  • 9
  • 64
  • 88

6 Answers6

19

You can add a custom plugin like:

config.plugins.push(new function() {
    this.apply = (compiler) => {
        compiler.hooks.done.tap("Log On Done Plugin", () => {
            console.log(("\n[" + new Date().toLocaleString() + "]") + " Begin a new compilation.\n");
        });
    };
});
jeanfrg
  • 2,366
  • 2
  • 29
  • 40
datou3600
  • 206
  • 1
  • 4
9

The answer of datou3600 is awesome, but why not to be better?

Adding a little delay:

  1. The text is placed at the end
  2. A screen blinks noticeable to an eye

Here is the code:

config.plugins.push(function(){
    this.plugin('done', function(stats) {
        setTimeout(
            () => {
                console.log(('\n[' + new Date().toLocaleString() + ']') + ' --- DONE.\n');
            },
            100
        );
    });
});
Alexey Ruzin
  • 967
  • 8
  • 17
5

Install webpack-watch-time-plugin.

It displays the time when watcher rebuild happens.

Screenshot

Bjorn Reppen
  • 22,007
  • 9
  • 64
  • 88
4

Just an update as

DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead

you can do it like this:

class WatchTimerPlugin {
  apply(compiler) {
    compiler.hooks.done.tap('Watch Timer Plugin', (stats) => {
      console.log(('\n[' + new Date().toLocaleString() + ']') + ' --- DONE.\n');
    });
  }
}

module.exports = WatchTimerPlugin;
SetiZ
  • 51
  • 1
  • 5
3

In case you wanted to use the timestamp programmatically - e.g. for debugging, or to ensure that remote synchronization of your latest build worked properly - you could also use webpack.DefinePlugin.runtimeValue:

new webpack.DefinePlugin({
    BUILDTIME: webpack.DefinePlugin.runtimeValue(Date.now, true)
})

This would always give you the latest build time through the constant BUILDTIME.

StrikeAgainst
  • 556
  • 6
  • 23
0

The problem also may be solved without any WebPack modification by the terminal app. For instance, in the iTerm there is a setting "Show Timestamps".

enter image description here The result looks like that: enter image description here

Dmitry Davydov
  • 987
  • 13
  • 21