4

The webpack documentation for the HMR API mentions the following method:

accept(dependencies: string[], callback: (updatedDependencies) => void) => void

I understand how I can accept a single dependency but am unsure how the callback for multiple dependencies should look like.

Here's my code:

 var $ = require('jquery')
 var page = require('page')
 var index = require('./index')
 var home = require('./home')

 $(function() {
   page('/', index)
   page('/home', home)
   page()

   if (module.hot) {
     module.hot.accept(['./index', './home'], function(updatedDependencies) {
       // what should I put in here?
     })
   }
 })
helpermethod
  • 59,493
  • 71
  • 188
  • 276

1 Answers1

4

Taking a shot at answering this, as no one else has tried.

From looking at your code it looks fine.

The callback method is run after the multiple dependencies have been accepted. You basically send a function as a parameter, then once all dependencies have been accepted, that function is executed.

So you could pretty much put anything you like in that function. For example:

if (module.hot) {
    module.hot.accept(['./index', './home'], function() {
        alert('all the dependencies have been accepted');
        console.log('all the dependencies have been accepted');
    });
};

In that example, once the accept method has run and has completed it will execute the callback function, which in this case sends an alert and logs a message to the console.

So in short, replace '// what should I put in here?' with the code you want to continue with, once the dependencies have been accepted.

I see in your callback you have a parameter 'updatedDependencies' you can debug and place a breakpoint on the first line of your callback method, and place your mouse over the 'updatedDependencies' parameter to see if it contains anything - if it does, you can obviously work with that data accordingly.

Hope this helps.

Sean Thorburn
  • 1,728
  • 17
  • 31