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

Hot module reloading #7

Open
guncha opened this issue Oct 27, 2016 · 0 comments
Open

Hot module reloading #7

guncha opened this issue Oct 27, 2016 · 0 comments

Comments

@guncha
Copy link

guncha commented Oct 27, 2016

I forked the loader to customize it for our codebase and to add HMR and thought you might find it useful:

module.exports.pitch = function(remainingRequest) {
    this.cacheable()

    var cnames = loaderUtils.stringifyRequest(this, "!" + require.resolve("classnames/bind"))
    var styles = loaderUtils.stringifyRequest(this, "!!" + remainingRequest)

    return `
      if (module.hot) {
        var styles = require(${ styles })
        var classnames = require(${ cnames }).bind(styles)

        var fn = function() {
          return classnames.apply(null, arguments)
        }

        // Allow using this as an ES6 module
        fn.default = fn

        // Allow access to the raw style map
        fn.styles = styles

        module.exports = fn

        module.hot.accept(${ styles }, function() {
          fn.styles = styles = require(${ styles })
          classnames = require(${ cnames }).bind(styles)
        })
      } else {
        module.exports = require(${ cnames }).bind(require(${ styles }))
      }
    `;
}
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

1 participant