Is there a way in webpack watch mode to display on screen the timestamp when webpack last updated the build?
Asked Answered
P

6

13

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?

Praise answered 17/1, 2017 at 18:44 Comment(0)
P
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");
        });
    };
});
Perfervid answered 16/3, 2017 at 5:59 Comment(0)
E
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
        );
    });
});
Examination answered 26/10, 2018 at 15:20 Comment(0)
P
5

Install webpack-watch-time-plugin.

It displays the time when watcher rebuild happens.

Screenshot

Praise answered 19/1, 2017 at 11:29 Comment(1)
This doesn't seem to work with webpack 5.Homologue
S
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;
Spenserian answered 26/6, 2020 at 12:9 Comment(0)
H
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.

Hypesthesia answered 3/12, 2021 at 15:29 Comment(0)
B
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

Baton answered 25/2, 2023 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.