utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getExportLazyStyleCode = getExportLazyStyleCode;
  6. exports.getExportStyleCode = getExportStyleCode;
  7. exports.getImportInsertBySelectorCode = getImportInsertBySelectorCode;
  8. exports.getImportInsertStyleElementCode = getImportInsertStyleElementCode;
  9. exports.getImportIsOldIECode = getImportIsOldIECode;
  10. exports.getImportLinkAPICode = getImportLinkAPICode;
  11. exports.getImportLinkContentCode = getImportLinkContentCode;
  12. exports.getImportStyleAPICode = getImportStyleAPICode;
  13. exports.getImportStyleContentCode = getImportStyleContentCode;
  14. exports.getImportStyleDomAPICode = getImportStyleDomAPICode;
  15. exports.getInsertOptionCode = getInsertOptionCode;
  16. exports.getLinkHmrCode = getLinkHmrCode;
  17. exports.getSetAttributesCode = getSetAttributesCode;
  18. exports.getStyleHmrCode = getStyleHmrCode;
  19. exports.getStyleTagTransformFn = getStyleTagTransformFn;
  20. exports.getStyleTagTransformFnCode = getStyleTagTransformFnCode;
  21. exports.getdomAPI = getdomAPI;
  22. exports.stringifyRequest = stringifyRequest;
  23. var _path = _interopRequireDefault(require("path"));
  24. var _isEqualLocals = _interopRequireDefault(require("./runtime/isEqualLocals"));
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. const matchRelativePath = /^\.\.?[/\\]/;
  27. function isAbsolutePath(str) {
  28. return _path.default.posix.isAbsolute(str) || _path.default.win32.isAbsolute(str);
  29. }
  30. function isRelativePath(str) {
  31. return matchRelativePath.test(str);
  32. }
  33. // TODO simplify for the next major release
  34. function stringifyRequest(loaderContext, request) {
  35. if (typeof loaderContext.utils !== "undefined" && typeof loaderContext.utils.contextify === "function") {
  36. return JSON.stringify(loaderContext.utils.contextify(loaderContext.context, request));
  37. }
  38. const splitted = request.split("!");
  39. const {
  40. context
  41. } = loaderContext;
  42. return JSON.stringify(splitted.map(part => {
  43. // First, separate singlePath from query, because the query might contain paths again
  44. const splittedPart = part.match(/^(.*?)(\?.*)/);
  45. const query = splittedPart ? splittedPart[2] : "";
  46. let singlePath = splittedPart ? splittedPart[1] : part;
  47. if (isAbsolutePath(singlePath) && context) {
  48. singlePath = _path.default.relative(context, singlePath);
  49. if (isAbsolutePath(singlePath)) {
  50. // If singlePath still matches an absolute path, singlePath was on a different drive than context.
  51. // In this case, we leave the path platform-specific without replacing any separators.
  52. // @see https://github.com/webpack/loader-utils/pull/14
  53. return singlePath + query;
  54. }
  55. if (isRelativePath(singlePath) === false) {
  56. // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules).
  57. singlePath = `./${singlePath}`;
  58. }
  59. }
  60. return singlePath.replace(/\\/g, "/") + query;
  61. }).join("!"));
  62. }
  63. function getImportLinkAPICode(esModule, loaderContext) {
  64. const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/injectStylesIntoLinkTag.js")}`);
  65. return esModule ? `import API from ${modulePath};` : `var API = require(${modulePath});`;
  66. }
  67. function getImportLinkContentCode(esModule, loaderContext, request) {
  68. const modulePath = stringifyRequest(loaderContext, `!!${request}`);
  69. return esModule ? `import content from ${modulePath};` : `var content = require(${modulePath});`;
  70. }
  71. function getImportStyleAPICode(esModule, loaderContext) {
  72. const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/injectStylesIntoStyleTag.js")}`);
  73. return esModule ? `import API from ${modulePath};` : `var API = require(${modulePath});`;
  74. }
  75. function getImportStyleDomAPICode(esModule, loaderContext, isSingleton, isAuto) {
  76. const styleAPI = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/styleDomAPI.js")}`);
  77. const singletonAPI = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/singletonStyleDomAPI.js")}`);
  78. if (isAuto) {
  79. return esModule ? `import domAPI from ${styleAPI};
  80. import domAPISingleton from ${singletonAPI};` : `var domAPI = require(${styleAPI});
  81. var domAPISingleton = require(${singletonAPI});`;
  82. }
  83. return esModule ? `import domAPI from ${isSingleton ? singletonAPI : styleAPI};` : `var domAPI = require(${isSingleton ? singletonAPI : styleAPI});`;
  84. }
  85. function getImportStyleContentCode(esModule, loaderContext, request) {
  86. const modulePath = stringifyRequest(loaderContext, `!!${request}`);
  87. return esModule ? `import content, * as namedExport from ${modulePath};` : `var content = require(${modulePath});`;
  88. }
  89. function getImportInsertBySelectorCode(esModule, loaderContext, insertType, options) {
  90. if (insertType === "selector") {
  91. const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/insertBySelector.js")}`);
  92. return esModule ? `import insertFn from ${modulePath};` : `var insertFn = require(${modulePath});`;
  93. }
  94. if (insertType === "module-path") {
  95. const modulePath = stringifyRequest(loaderContext, `${options.insert}`);
  96. loaderContext.addBuildDependency(options.insert);
  97. return esModule ? `import insertFn from ${modulePath};` : `var insertFn = require(${modulePath});`;
  98. }
  99. return "";
  100. }
  101. function getInsertOptionCode(insertType, options) {
  102. if (insertType === "selector") {
  103. const insert = options.insert ? JSON.stringify(options.insert) : '"head"';
  104. return `
  105. options.insert = insertFn.bind(null, ${insert});
  106. `;
  107. }
  108. if (insertType === "module-path") {
  109. return `options.insert = insertFn;`;
  110. }
  111. // Todo remove "function" type for insert option in next major release, because code duplication occurs. Leave require.resolve()
  112. return `options.insert = ${options.insert.toString()};`;
  113. }
  114. function getImportInsertStyleElementCode(esModule, loaderContext) {
  115. const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/insertStyleElement.js")}`);
  116. return esModule ? `import insertStyleElement from ${modulePath};` : `var insertStyleElement = require(${modulePath});`;
  117. }
  118. function getStyleHmrCode(esModule, loaderContext, request, lazy) {
  119. const modulePath = stringifyRequest(loaderContext, `!!${request}`);
  120. return `
  121. if (module.hot) {
  122. if (!content.locals || module.hot.invalidate) {
  123. var isEqualLocals = ${_isEqualLocals.default.toString()};
  124. var isNamedExport = ${esModule ? "!content.locals" : false};
  125. var oldLocals = isNamedExport ? namedExport : content.locals;
  126. module.hot.accept(
  127. ${modulePath},
  128. function () {
  129. ${esModule ? `if (!isEqualLocals(oldLocals, isNamedExport ? namedExport : content.locals, isNamedExport)) {
  130. module.hot.invalidate();
  131. return;
  132. }
  133. oldLocals = isNamedExport ? namedExport : content.locals;
  134. ${lazy ? `if (update && refs > 0) {
  135. update(content);
  136. }` : `update(content);`}` : `content = require(${modulePath});
  137. content = content.__esModule ? content.default : content;
  138. ${lazy ? "" : `if (typeof content === 'string') {
  139. content = [[module.id, content, '']];
  140. }`}
  141. if (!isEqualLocals(oldLocals, content.locals)) {
  142. module.hot.invalidate();
  143. return;
  144. }
  145. oldLocals = content.locals;
  146. ${lazy ? `if (update && refs > 0) {
  147. update(content);
  148. }` : `update(content);`}`}
  149. }
  150. )
  151. }
  152. module.hot.dispose(function() {
  153. ${lazy ? `if (update) {
  154. update();
  155. }` : `update();`}
  156. });
  157. }
  158. `;
  159. }
  160. function getLinkHmrCode(esModule, loaderContext, request) {
  161. const modulePath = stringifyRequest(loaderContext, `!!${request}`);
  162. return `
  163. if (module.hot) {
  164. module.hot.accept(
  165. ${modulePath},
  166. function() {
  167. ${esModule ? "update(content);" : `content = require(${modulePath});
  168. content = content.__esModule ? content.default : content;
  169. update(content);`}
  170. }
  171. );
  172. module.hot.dispose(function() {
  173. update();
  174. });
  175. }`;
  176. }
  177. function getdomAPI(isAuto) {
  178. return isAuto ? "isOldIE() ? domAPISingleton : domAPI" : "domAPI";
  179. }
  180. function getImportIsOldIECode(esModule, loaderContext) {
  181. const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/isOldIE.js")}`);
  182. return esModule ? `import isOldIE from ${modulePath};` : `var isOldIE = require(${modulePath});`;
  183. }
  184. function getStyleTagTransformFnCode(esModule, loaderContext, options, isSingleton, styleTagTransformType) {
  185. if (isSingleton) {
  186. return "";
  187. }
  188. if (styleTagTransformType === "default") {
  189. const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/styleTagTransform.js")}`);
  190. return esModule ? `import styleTagTransformFn from ${modulePath};` : `var styleTagTransformFn = require(${modulePath});`;
  191. }
  192. if (styleTagTransformType === "module-path") {
  193. const modulePath = stringifyRequest(loaderContext, `${options.styleTagTransform}`);
  194. loaderContext.addBuildDependency(options.styleTagTransform);
  195. return esModule ? `import styleTagTransformFn from ${modulePath};` : `var styleTagTransformFn = require(${modulePath});`;
  196. }
  197. return "";
  198. }
  199. function getStyleTagTransformFn(options, isSingleton) {
  200. // Todo remove "function" type for styleTagTransform option in next major release, because code duplication occurs. Leave require.resolve()
  201. return isSingleton ? "" : typeof options.styleTagTransform === "function" ? `options.styleTagTransform = ${options.styleTagTransform.toString()}` : `options.styleTagTransform = styleTagTransformFn`;
  202. }
  203. function getExportStyleCode(esModule, loaderContext, request) {
  204. const modulePath = stringifyRequest(loaderContext, `!!${request}`);
  205. return esModule ? `export * from ${modulePath};
  206. export default content && content.locals ? content.locals : undefined;` : "module.exports = content && content.locals || {};";
  207. }
  208. function getExportLazyStyleCode(esModule, loaderContext, request) {
  209. const modulePath = stringifyRequest(loaderContext, `!!${request}`);
  210. return esModule ? `export * from ${modulePath};
  211. export default exported;` : "module.exports = exported;";
  212. }
  213. function getSetAttributesCode(esModule, loaderContext, options) {
  214. let modulePath;
  215. if (typeof options.attributes !== "undefined") {
  216. modulePath = options.attributes.nonce !== "undefined" ? stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/setAttributesWithAttributesAndNonce.js")}`) : stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/setAttributesWithAttributes.js")}`);
  217. } else {
  218. modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/setAttributesWithoutAttributes.js")}`);
  219. }
  220. return esModule ? `import setAttributes from ${modulePath};` : `var setAttributes = require(${modulePath});`;
  221. }
  222. // eslint-disable-next-line import/prefer-default-export