1

I have a Webpack project where I am building a couple of libraries. Both of them are meant to be imported with <script> tags. My config looks like the following:

entry: {
  bundle: ['./src/index', 'webpack-hot-middleware/client?reload=true'],
  mylib: ['./src/loader', 'webpack-hot-middleware/client?reload=true']
},
target: web,
output: {
  path: `${__dirname}/dist`, 
  publicPath: 'http://localhost:3000/',
  filename: '[name].js',
  library: ['mylib', '[name]'],
  libraryTarget: 'umd'
},

For my tests, my entry point is very simple:

module.exports = 'abracadabra';

According to the guide I follow how to build a complex library with webpack, and if I understood right, this should be sufficient to get this behavior in the browser (after opening the index html file with the script tag):

console.log(mylib) -> 'abracadabra'

However, this doesn't work, and in the browser's console I can see mylibis defined but it has a strange shape:

Object {mylib: Object}
   mylib: Object
     subscribe: function subscribe(handler)
     useCustomOverlay: function useCustomOverlay(customOverlay)
     __proto__: Object__proto__:

Some of that code has to do with the HMR (Hot Module Replacement), but still I don't understand how I am supposed to use my module.

Any ideas?

1 Answer 1

0

Do you have this in your entrypoint?

if (module.hot) {
    module.hot.accept();
}
3
  • I read andrewhfarmer.com/webpack-hmr-tutorial and now I understand I need to include those lines to enable HMR. However, they don't solve the problem. Right now I am attaching my exports to the window in order to continue with development: window.mylib = module.exports; I will upload a sample project to Github to explore this issue and ask for advise as soon as I can… Jul 8, 2016 at 17:59
  • I made some quick tests and, in my setup, Webpack seems to reload my modules even if I don't add the above lines… strange? Jul 8, 2016 at 18:12
  • Given module.hot is true, then module.exports will contain the exported things in the entry point. Not sure where to go from here. Jun 15, 2017 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.