index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _utils = require("./utils");
  8. var _options = _interopRequireDefault(require("./options.json"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. // eslint-disable-next-line consistent-return
  11. const loader = function loader(content) {
  12. if (this._compiler && this._compiler.options && this._compiler.options.experiments && this._compiler.options.experiments.css && this._module && (this._module.type === "css" || this._module.type === "css/global" || this._module.type === "css/module" || this._module.type === "css/auto")) {
  13. return content;
  14. }
  15. };
  16. loader.pitch = function pitch(request) {
  17. if (this._compiler && this._compiler.options && this._compiler.options.experiments && this._compiler.options.experiments.css && this._module && (this._module.type === "css" || this._module.type === "css/global" || this._module.type === "css/module" || this._module.type === "css/auto")) {
  18. this.emitWarning(new Error('You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `style-loader` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `style-loader` in your webpack config (now `style-loader` does nothing).'));
  19. return;
  20. }
  21. const options = this.getOptions(_options.default);
  22. const injectType = options.injectType || "styleTag";
  23. const esModule = typeof options.esModule !== "undefined" ? options.esModule : true;
  24. const runtimeOptions = {};
  25. if (options.attributes) {
  26. runtimeOptions.attributes = options.attributes;
  27. }
  28. if (options.base) {
  29. runtimeOptions.base = options.base;
  30. }
  31. const insertType = typeof options.insert === "function" ? "function" : options.insert && _path.default.isAbsolute(options.insert) ? "module-path" : "selector";
  32. const styleTagTransformType = typeof options.styleTagTransform === "function" ? "function" : options.styleTagTransform && _path.default.isAbsolute(options.styleTagTransform) ? "module-path" : "default";
  33. switch (injectType) {
  34. case "linkTag":
  35. {
  36. const hmrCode = this.hot ? (0, _utils.getLinkHmrCode)(esModule, this, request) : "";
  37. // eslint-disable-next-line consistent-return
  38. return `
  39. ${(0, _utils.getImportLinkAPICode)(esModule, this)}
  40. ${(0, _utils.getImportInsertBySelectorCode)(esModule, this, insertType, options)}
  41. ${(0, _utils.getImportLinkContentCode)(esModule, this, request)}
  42. ${esModule ? "" : `content = content.__esModule ? content.default : content;`}
  43. var options = ${JSON.stringify(runtimeOptions)};
  44. ${(0, _utils.getInsertOptionCode)(insertType, options)}
  45. var update = API(content, options);
  46. ${hmrCode}
  47. ${esModule ? "export default {}" : ""}`;
  48. }
  49. case "lazyStyleTag":
  50. case "lazyAutoStyleTag":
  51. case "lazySingletonStyleTag":
  52. {
  53. const isSingleton = injectType === "lazySingletonStyleTag";
  54. const isAuto = injectType === "lazyAutoStyleTag";
  55. const hmrCode = this.hot ? (0, _utils.getStyleHmrCode)(esModule, this, request, true) : "";
  56. // eslint-disable-next-line consistent-return
  57. return `
  58. var exported = {};
  59. ${(0, _utils.getImportStyleAPICode)(esModule, this)}
  60. ${(0, _utils.getImportStyleDomAPICode)(esModule, this, isSingleton, isAuto)}
  61. ${(0, _utils.getImportInsertBySelectorCode)(esModule, this, insertType, options)}
  62. ${(0, _utils.getSetAttributesCode)(esModule, this, options)}
  63. ${(0, _utils.getImportInsertStyleElementCode)(esModule, this)}
  64. ${(0, _utils.getStyleTagTransformFnCode)(esModule, this, options, isSingleton, styleTagTransformType)}
  65. ${(0, _utils.getImportStyleContentCode)(esModule, this, request)}
  66. ${isAuto ? (0, _utils.getImportIsOldIECode)(esModule, this) : ""}
  67. ${esModule ? `if (content && content.locals) {
  68. exported.locals = content.locals;
  69. }
  70. ` : `content = content.__esModule ? content.default : content;
  71. exported.locals = content.locals || {};`}
  72. var refs = 0;
  73. var update;
  74. var options = ${JSON.stringify(runtimeOptions)};
  75. ${(0, _utils.getStyleTagTransformFn)(options, isSingleton)};
  76. options.setAttributes = setAttributes;
  77. ${(0, _utils.getInsertOptionCode)(insertType, options)}
  78. options.domAPI = ${(0, _utils.getdomAPI)(isAuto)};
  79. options.insertStyleElement = insertStyleElement;
  80. exported.use = function(insertOptions) {
  81. options.options = insertOptions || {};
  82. if (!(refs++)) {
  83. update = API(content, options);
  84. }
  85. return exported;
  86. };
  87. exported.unuse = function() {
  88. if (refs > 0 && !--refs) {
  89. update();
  90. update = null;
  91. }
  92. };
  93. ${hmrCode}
  94. ${(0, _utils.getExportLazyStyleCode)(esModule, this, request)}
  95. `;
  96. }
  97. case "styleTag":
  98. case "autoStyleTag":
  99. case "singletonStyleTag":
  100. default:
  101. {
  102. const isSingleton = injectType === "singletonStyleTag";
  103. const isAuto = injectType === "autoStyleTag";
  104. const hmrCode = this.hot ? (0, _utils.getStyleHmrCode)(esModule, this, request, false) : "";
  105. // eslint-disable-next-line consistent-return
  106. return `
  107. ${(0, _utils.getImportStyleAPICode)(esModule, this)}
  108. ${(0, _utils.getImportStyleDomAPICode)(esModule, this, isSingleton, isAuto)}
  109. ${(0, _utils.getImportInsertBySelectorCode)(esModule, this, insertType, options)}
  110. ${(0, _utils.getSetAttributesCode)(esModule, this, options)}
  111. ${(0, _utils.getImportInsertStyleElementCode)(esModule, this)}
  112. ${(0, _utils.getStyleTagTransformFnCode)(esModule, this, options, isSingleton, styleTagTransformType)}
  113. ${(0, _utils.getImportStyleContentCode)(esModule, this, request)}
  114. ${isAuto ? (0, _utils.getImportIsOldIECode)(esModule, this) : ""}
  115. ${esModule ? "" : `content = content.__esModule ? content.default : content;`}
  116. var options = ${JSON.stringify(runtimeOptions)};
  117. ${(0, _utils.getStyleTagTransformFn)(options, isSingleton)};
  118. options.setAttributes = setAttributes;
  119. ${(0, _utils.getInsertOptionCode)(insertType, options)}
  120. options.domAPI = ${(0, _utils.getdomAPI)(isAuto)};
  121. options.insertStyleElement = insertStyleElement;
  122. var update = API(content, options);
  123. ${hmrCode}
  124. ${(0, _utils.getExportStyleCode)(esModule, this, request)}
  125. `;
  126. }
  127. }
  128. };
  129. var _default = exports.default = loader;