index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "use strict";
  2. var _path = _interopRequireDefault(require("path"));
  3. var _loaderUtils = _interopRequireDefault(require("loader-utils"));
  4. var _schemaUtils = _interopRequireDefault(require("schema-utils"));
  5. var _options = _interopRequireDefault(require("./options.json"));
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7. module.exports = () => {};
  8. module.exports.pitch = function loader(request) {
  9. const options = _loaderUtils.default.getOptions(this) || {};
  10. (0, _schemaUtils.default)(_options.default, options, {
  11. name: 'Style Loader',
  12. baseDataPath: 'options'
  13. });
  14. const insert = typeof options.insert === 'undefined' ? '"head"' : typeof options.insert === 'string' ? JSON.stringify(options.insert) : options.insert.toString();
  15. const injectType = options.injectType || 'styleTag';
  16. switch (injectType) {
  17. case 'linkTag':
  18. {
  19. const hmrCode = this.hot ? `
  20. if (module.hot) {
  21. module.hot.accept(
  22. ${_loaderUtils.default.stringifyRequest(this, `!!${request}`)},
  23. function() {
  24. update(require(${_loaderUtils.default.stringifyRequest(this, `!!${request}`)}));
  25. }
  26. );
  27. module.hot.dispose(function() {
  28. update();
  29. });
  30. }` : '';
  31. return `var options = ${JSON.stringify(options)};
  32. options.insert = ${insert};
  33. var update = require(${_loaderUtils.default.stringifyRequest(this, `!${_path.default.join(__dirname, 'runtime/injectStylesIntoLinkTag.js')}`)})(require(${_loaderUtils.default.stringifyRequest(this, `!!${request}`)}), options);
  34. ${hmrCode}`;
  35. }
  36. case 'lazyStyleTag':
  37. case 'lazySingletonStyleTag':
  38. {
  39. const isSingleton = injectType === 'lazySingletonStyleTag';
  40. const hmrCode = this.hot ? `
  41. if (module.hot) {
  42. var lastRefs = module.hot.data && module.hot.data.refs || 0;
  43. if (lastRefs) {
  44. exports.use();
  45. if (!content.locals) {
  46. refs = lastRefs;
  47. }
  48. }
  49. if (!content.locals) {
  50. module.hot.accept();
  51. }
  52. module.hot.dispose(function(data) {
  53. data.refs = content.locals ? 0 : refs;
  54. if (dispose) {
  55. dispose();
  56. }
  57. });
  58. }` : '';
  59. return `var refs = 0;
  60. var dispose;
  61. var content = require(${_loaderUtils.default.stringifyRequest(this, `!!${request}`)});
  62. var options = ${JSON.stringify(options)};
  63. options.insert = ${insert};
  64. options.singleton = ${isSingleton};
  65. if (typeof content === 'string') {
  66. content = [[module.id, content, '']];
  67. }
  68. if (content.locals) {
  69. exports.locals = content.locals;
  70. }
  71. exports.use = function() {
  72. if (!(refs++)) {
  73. dispose = require(${_loaderUtils.default.stringifyRequest(this, `!${_path.default.join(__dirname, 'runtime/injectStylesIntoStyleTag.js')}`)})(content, options);
  74. }
  75. return exports;
  76. };
  77. exports.unuse = function() {
  78. if (refs > 0 && !--refs) {
  79. dispose();
  80. dispose = null;
  81. }
  82. };
  83. ${hmrCode}
  84. `;
  85. }
  86. case 'styleTag':
  87. case 'singletonStyleTag':
  88. default:
  89. {
  90. const isSingleton = injectType === 'singletonStyleTag';
  91. const hmrCode = this.hot ? `
  92. if (module.hot) {
  93. if (!content.locals) {
  94. module.hot.accept(
  95. ${_loaderUtils.default.stringifyRequest(this, `!!${request}`)},
  96. function () {
  97. var newContent = require(${_loaderUtils.default.stringifyRequest(this, `!!${request}`)});
  98. if (typeof newContent === 'string') {
  99. newContent = [[module.id, newContent, '']];
  100. }
  101. update(newContent);
  102. }
  103. )
  104. }
  105. module.hot.dispose(function() {
  106. update();
  107. });
  108. }` : '';
  109. return `var content = require(${_loaderUtils.default.stringifyRequest(this, `!!${request}`)});
  110. if (typeof content === 'string') {
  111. content = [[module.id, content, '']];
  112. }
  113. var options = ${JSON.stringify(options)}
  114. options.insert = ${insert};
  115. options.singleton = ${isSingleton};
  116. var update = require(${_loaderUtils.default.stringifyRequest(this, `!${_path.default.join(__dirname, 'runtime/injectStylesIntoStyleTag.js')}`)})(content, options);
  117. if (content.locals) {
  118. module.exports = content.locals;
  119. }
  120. ${hmrCode}`;
  121. }
  122. }
  123. };