You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.9 KiB

4 years ago
  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. options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
  14. // The variable is needed, because the function should be inlined.
  15. // If is just stored it in options, JSON.stringify will quote
  16. // the function and it would be just a string at runtime
  17. var insertInto;
  18. if (typeof options.insertInto === "function") {
  19. insertInto = options.insertInto.toString();
  20. }
  21. // We need to check if it a string, or variable will be "undefined"
  22. // and the loader crashes
  23. if (typeof options.insertInto === "string") {
  24. insertInto = '"' + options.insertInto + '"';
  25. }
  26. var hmr = [
  27. // Hot Module Replacement,
  28. "if(module.hot) {",
  29. // When the styles change, update the <style> tags
  30. " module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
  31. " var newContent = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
  32. "",
  33. " if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];",
  34. "",
  35. " var locals = (function(a, b) {",
  36. " var key, idx = 0;",
  37. "",
  38. " for(key in a) {",
  39. " if(!b || a[key] !== b[key]) return false;",
  40. " idx++;",
  41. " }",
  42. "",
  43. " for(key in b) idx--;",
  44. "",
  45. " return idx === 0;",
  46. " }(content.locals, newContent.locals));",
  47. "",
  48. // This error is caught and not shown and causes a full reload
  49. " if(!locals) throw new Error('Aborting CSS HMR due to changed css-modules locals.');",
  50. "",
  51. " update(newContent);",
  52. " });",
  53. "",
  54. // When the module is disposed, remove the <style> tags
  55. " module.hot.dispose(function() { update(); });",
  56. "}"
  57. ].join("\n");
  58. return [
  59. // Style Loader
  60. // Adds CSS to the DOM by adding a <style> tag
  61. "",
  62. // Load styles
  63. "var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
  64. "",
  65. "if(typeof content === 'string') content = [[module.id, content, '']];",
  66. "",
  67. // Transform styles",
  68. "var transform;",
  69. "var insertInto;",
  70. "",
  71. options.transform ? "transform = require(" + loaderUtils.stringifyRequest(this, "!" + path.resolve(options.transform)) + ");" : "",
  72. "",
  73. "var options = " + JSON.stringify(options),
  74. "",
  75. "options.transform = transform",
  76. "options.insertInto = " + insertInto + ";",
  77. "",
  78. // Add styles to the DOM
  79. "var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, options);",
  80. "",
  81. "if(content.locals) module.exports = content.locals;",
  82. "",
  83. options.hmr ? hmr : ""
  84. ].join("\n");
  85. };