useable.js 1.6 KB

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