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

What are the equivalent of webpack-serve's "on" hooks? #1509

Closed
marcofugaro opened this issue Sep 23, 2018 · 10 comments
Closed

What are the equivalent of webpack-serve's "on" hooks? #1509

marcofugaro opened this issue Sep 23, 2018 · 10 comments

Comments

@marcofugaro
Copy link
Contributor

marcofugaro commented Sep 23, 2018

Sorry if this is a question, but I can't find a solution.

I found myself migrating back to webpack-dev-server after webpack-serve was deprecated. I found really useful to attach hooks in the webpack.config.js without having to touch node at all.

The hooks I used were on.listening, on.build-finished, on.compiler-error to show custom stats to the console. What are the equilvalent hooks in webpack-dev-server? I only found after that could substitute the on.listening, how do I attach hooks to the remaining two events, without having to run webpack from node?

Another thing I was doing:

on: {
  listening: ({ server, options }) => {
    // try to open into the already existing tab
    openBrowser(`http://localhost:${options.port}`)
  },
},

How do I do this in webpack-dev-server? How do I get the port from the arguments in after?

@alexander-akait
Copy link
Member

@marcofugaro it is feature, can you describe use case for this?

@marcofugaro
Copy link
Contributor Author

marcofugaro commented Sep 24, 2018

Sure thing! Basically I was using these hooks to show only the information I need to the console (same thing I was using webpack-command for).

Here is config I was using, take a look at the comments:

const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages')
const openBrowser = require('react-dev-utils/openBrowser')
const prettyMs = require('pretty-ms')

module.exports = {
  serve: {
    port: 8080,
    // don't show all the default webpack stuff
    logLevel: 'silent',
    hotClient: {
      logLevel: 'silent',
    },
    devMiddleware: {
      logLevel: 'silent',
    },
    on: {
      listening: ({ options }) => {
        // try to open into the already existing tab
        openBrowser(`http://localhost:${options.port}`)
      },
      'build-finished': ({ stats }) => {
        if (stats.hasErrors()) {
          return
        }
        
        const time = prettyMs(stats.endTime - stats.startTime)
        console.log(`Compiled successfully in ${time}`)
      },
      'compiler-error': stats => {
        // format the errors in a nicer way
        const messages = formatWebpackMessages(stats.json)
        console.log(messages.errors[0])
      },
    },
  },
}

@aszmyd

This comment has been minimized.

@polco
Copy link

polco commented Jan 15, 2019

what about the solution given in this issue: #132 ?
using the hook API instead of the deprecated plugin one:

...
const compiler = webpack(webpackConfig);
compiler.hooks.done.tap('done', (stats) => {})
const server = new WebpackDevServer(compiler, options);
...

@marcofugaro
Copy link
Contributor Author

@polco as I already specified, I don't want to call webpack from node, I have a simple webpack.config.js and want to keep it that way.

@polco
Copy link

polco commented Jan 15, 2019

My bad

@alexander-akait
Copy link
Member

PR welcome, it is should be not diffucult

@ghost
Copy link

ghost commented Jun 18, 2020

@evilebottnawi Could you stop writing PR welcome on every issue.

@alexander-akait
Copy link
Member

@atilkan What is the problem?

@alexander-akait
Copy link
Member

I want to close this issue in favor of webpack's built-in hooks, also we have some callbacks, like before/after, so I think it should be enough to implement the same behaviour, anyway if you really need something new feel free to open an issue with example what is missing

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

No branches or pull requests

4 participants