Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to hot reloading clear console ? #565

Closed
albttx opened this issue Aug 24, 2016 · 9 comments
Closed

Is there a way to hot reloading clear console ? #565

albttx opened this issue Aug 24, 2016 · 9 comments
Labels

Comments

@albttx
Copy link

albttx commented Aug 24, 2016

When the code is hot reload, is there a chrome/firefox plugin or anything to show the seperation from the reload in the console ?

Or add something like console.log("--------------------------------------"); to separate them ?

@SpaceK33z
Copy link
Member

You could hack into hot reload messages like this:

window.addEventListener('message', function onWebpackMessage (e) {
  // do something
});

@albttx
Copy link
Author

albttx commented Sep 5, 2016

Awesome ! It's perfectly work !

@albttx albttx closed this as completed Sep 5, 2016
@tavurth
Copy link

tavurth commented Apr 22, 2017

Add this to your apps index.js:

// Clear after module reload
window.addEventListener('message', e => {
    if ('production' !== process.env.NODE_ENV) {
        console.clear();
    }
});

For example, this is part of my index.jsx entry point:

...

const render = Component => {
    ReactDOM.render(
        <Provider store={store}>
            <MuiThemeProvider className='full-size'>
                <Component />
            </MuiThemeProvider>
        </Provider>,
        document.getElementById('root')
    );
};

render(App);

if (module.hot) {
    module.hot.accept();

    window.addEventListener('message', e => {
        if ('production' !== process.env.NODE_ENV) {
            console.clear();
        }
    });
}

if (module.hot) will get culled out when we run production minification, and every time HMR reloads, we'll clear the console.

@szalapski
Copy link

szalapski commented Aug 21, 2018

This doesn't work right for me--it clears the console on any message. How can I get it to clear only on an HMR reload?

@alex3683
Copy link

@szalapski I'm using vue-cli and don't know if it changes something on the messages sent by the webpack-dev-server, but in my case this works:

window.addEventListener('message', e => {
  if (process.env.NODE_ENV !== 'production' && e.data && e.data.type === 'webpackInvalid') {
    console.clear();
  }
});

So the console will be cleared as soon as webpack detects the change (at least that's what I think webpackInvalid is used for ...).

@szalapski
Copy link

I don't appear to ever have "e.data". I have e.payload and e.source, and nothing in them seems to distinguish a HMR-reload event from any other.

@alex3683
Copy link

@szalapski Which version of the dev server are you running? Mine is the latest, 3.1.8.

@szalapski
Copy link

szalapski commented Oct 29, 2018

How do I find that? I don't see a "devServer" anywhere in my .js files. It's a basic web server, right? It appears I am not using it. Instead, I use HTTP.sys (since this is an ASP.NET Core site) with Webpack dev middleware. But, I'm not sure why the web server is related to this--maybe it is in the way the server communicates HMR events?

Maybe this is the wrong place to ask, but it is the only place on the web that seems to address it at all. I'll go ask on stackoverflow.

@szalapski
Copy link

szalapski commented Dec 26, 2018

This seemed to work better for me.

if (module.hot) {
    module.hot.accept() // already had this init code 

    module.hot.addStatusHandler(status => {
        if (status === 'prepare') console.clear()
    })
}

See also https://stackoverflow.com/a/53933757/7453 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants