useable.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var loaderUtils = require("loader-utils"),
  6. path = require("path");
  7. module.exports = function() {};
  8. module.exports.pitch = function(remainingRequest) {
  9. if(this.cacheable) this.cacheable();
  10. var query = loaderUtils.parseQuery(this.query);
  11. return [
  12. "var refs = 0;",
  13. "var dispose;",
  14. "var content = require(" + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ");",
  15. "if(typeof content === 'string') content = [[module.id, content, '']];",
  16. "exports.use = exports.ref = function() {",
  17. " if(!(refs++)) {",
  18. " exports.locals = content.locals;",
  19. " dispose = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "addStyles.js")) + ")(content, " + JSON.stringify(query) + ");",
  20. " }",
  21. " return exports;",
  22. "};",
  23. "exports.unuse = exports.unref = function() {",
  24. " if(!(--refs)) {",
  25. " dispose();",
  26. " dispose = null;",
  27. " }",
  28. "};",
  29. "if(module.hot) {",
  30. " var lastRefs = module.hot.data && module.hot.data.refs || 0;",
  31. " if(lastRefs) {",
  32. " exports.ref();",
  33. " if(!content.locals) {",
  34. " refs = lastRefs;",
  35. " }",
  36. " }",
  37. " if(!content.locals) {",
  38. " module.hot.accept();",
  39. " }",
  40. " module.hot.dispose(function(data) {",
  41. " data.refs = content.locals ? 0 : refs;",
  42. " if(dispose) {",
  43. " dispose();",
  44. " }",
  45. " });",
  46. "}"
  47. ].join("\n");
  48. };