index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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')
  13. return [
  14. "// style-loader: Adds some css to the DOM by adding a <style> tag",
  15. "",
  16. "// load the styles",
  17. "var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
  18. "if(typeof content === 'string') content = [[module.id, content, '']];",
  19. "// Prepare cssTransformation",
  20. "var transform;",
  21. options.transform ? "transform = require(" + loaderUtils.stringifyRequest(this, "!" + path.resolve(options.transform)) + ");" : "",
  22. "var options = " + JSON.stringify(options),
  23. "options.transform = transform",
  24. "// add the styles to the DOM",
  25. "var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, options);",
  26. "if(content.locals) module.exports = content.locals;",
  27. "// Hot Module Replacement",
  28. "if(module.hot) {",
  29. " // When the styles change, update the <style> tags",
  30. " if(!content.locals) {",
  31. " module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
  32. " var newContent = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
  33. " if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];",
  34. " update(newContent);",
  35. " });",
  36. " }",
  37. " // When the module is disposed, remove the <style> tags",
  38. " module.hot.dispose(function() { update(); });",
  39. "}"
  40. ].join("\n");
  41. };