ContextDependencyHelpers.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { parseResource } = require("../util/identifier");
  7. /** @typedef {import("estree").Node} EsTreeNode */
  8. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  9. /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
  10. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  11. /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
  12. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  13. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  14. /** @typedef {import("./ContextDependency")} ContextDependency */
  15. /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
  16. /**
  17. * Escapes regular expression metacharacters
  18. * @param {string} str String to quote
  19. * @returns {string} Escaped string
  20. */
  21. const quoteMeta = str => {
  22. return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
  23. };
  24. /**
  25. * @param {string} prefix prefix
  26. * @returns {{prefix: string, context: string}} result
  27. */
  28. const splitContextFromPrefix = prefix => {
  29. const idx = prefix.lastIndexOf("/");
  30. let context = ".";
  31. if (idx >= 0) {
  32. context = prefix.slice(0, idx);
  33. prefix = `.${prefix.slice(idx)}`;
  34. }
  35. return {
  36. context,
  37. prefix
  38. };
  39. };
  40. /** @typedef {Partial<Omit<ContextDependencyOptions, "resource">>} PartialContextDependencyOptions */
  41. /** @typedef {{ new(options: ContextDependencyOptions, range: Range, valueRange: [number, number], ...args: any[]): ContextDependency }} ContextDependencyConstructor */
  42. /**
  43. * @param {ContextDependencyConstructor} Dep the Dependency class
  44. * @param {Range} range source range
  45. * @param {BasicEvaluatedExpression} param context param
  46. * @param {EsTreeNode} expr expr
  47. * @param {Pick<JavascriptParserOptions, `${"expr"|"wrapped"}Context${"Critical"|"Recursive"|"RegExp"}` | "exprContextRequest">} options options for context creation
  48. * @param {PartialContextDependencyOptions} contextOptions options for the ContextModule
  49. * @param {JavascriptParser} parser the parser
  50. * @param {...any} depArgs depArgs
  51. * @returns {ContextDependency} the created Dependency
  52. */
  53. exports.create = (
  54. Dep,
  55. range,
  56. param,
  57. expr,
  58. options,
  59. contextOptions,
  60. parser,
  61. ...depArgs
  62. ) => {
  63. if (param.isTemplateString()) {
  64. const quasis = /** @type {BasicEvaluatedExpression[]} */ (param.quasis);
  65. let prefixRaw = /** @type {string} */ (quasis[0].string);
  66. let postfixRaw =
  67. /** @type {string} */
  68. (quasis.length > 1 ? quasis[quasis.length - 1].string : "");
  69. const valueRange = /** @type {Range} */ (param.range);
  70. const { context, prefix } = splitContextFromPrefix(prefixRaw);
  71. const {
  72. path: postfix,
  73. query,
  74. fragment
  75. } = parseResource(postfixRaw, parser);
  76. // When there are more than two quasis, the generated RegExp can be more precise
  77. // We join the quasis with the expression regexp
  78. const innerQuasis = quasis.slice(1, quasis.length - 1);
  79. const innerRegExp =
  80. /** @type {RegExp} */ (options.wrappedContextRegExp).source +
  81. innerQuasis
  82. .map(
  83. q =>
  84. quoteMeta(/** @type {string} */ (q.string)) +
  85. /** @type {RegExp} */ (options.wrappedContextRegExp).source
  86. )
  87. .join("");
  88. // Example: `./context/pre${e}inner${e}inner2${e}post?query#frag`
  89. // context: "./context"
  90. // prefix: "./pre"
  91. // innerQuasis: [BEE("inner"), BEE("inner2")]
  92. // (BEE = BasicEvaluatedExpression)
  93. // postfix: "post"
  94. // query: "?query"
  95. // fragment: "#frag"
  96. // regExp: /^\.\/pre.*inner.*inner2.*post$/
  97. const regExp = new RegExp(
  98. `^${quoteMeta(prefix)}${innerRegExp}${quoteMeta(postfix)}$`
  99. );
  100. const dep = new Dep(
  101. {
  102. request: context + query + fragment,
  103. recursive: /** @type {boolean} */ (options.wrappedContextRecursive),
  104. regExp,
  105. mode: "sync",
  106. ...contextOptions
  107. },
  108. range,
  109. valueRange,
  110. ...depArgs
  111. );
  112. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  113. /** @type {{ value: string, range: Range }[]} */
  114. const replaces = [];
  115. const parts = /** @type {BasicEvaluatedExpression[]} */ (param.parts);
  116. parts.forEach((part, i) => {
  117. if (i % 2 === 0) {
  118. // Quasis or merged quasi
  119. let range = /** @type {Range} */ (part.range);
  120. let value = /** @type {string} */ (part.string);
  121. if (param.templateStringKind === "cooked") {
  122. value = JSON.stringify(value);
  123. value = value.slice(1, value.length - 1);
  124. }
  125. if (i === 0) {
  126. // prefix
  127. value = prefix;
  128. range = [
  129. /** @type {Range} */ (param.range)[0],
  130. /** @type {Range} */ (part.range)[1]
  131. ];
  132. value =
  133. (param.templateStringKind === "cooked" ? "`" : "String.raw`") +
  134. value;
  135. } else if (i === parts.length - 1) {
  136. // postfix
  137. value = postfix;
  138. range = [
  139. /** @type {Range} */ (part.range)[0],
  140. /** @type {Range} */ (param.range)[1]
  141. ];
  142. value = value + "`";
  143. } else if (
  144. part.expression &&
  145. part.expression.type === "TemplateElement" &&
  146. part.expression.value.raw === value
  147. ) {
  148. // Shortcut when it's a single quasi and doesn't need to be replaced
  149. return;
  150. }
  151. replaces.push({
  152. range,
  153. value
  154. });
  155. } else {
  156. // Expression
  157. parser.walkExpression(part.expression);
  158. }
  159. });
  160. dep.replaces = replaces;
  161. dep.critical =
  162. options.wrappedContextCritical &&
  163. "a part of the request of a dependency is an expression";
  164. return dep;
  165. } else if (
  166. param.isWrapped() &&
  167. ((param.prefix && param.prefix.isString()) ||
  168. (param.postfix && param.postfix.isString()))
  169. ) {
  170. let prefixRaw =
  171. /** @type {string} */
  172. (param.prefix && param.prefix.isString() ? param.prefix.string : "");
  173. let postfixRaw =
  174. /** @type {string} */
  175. (param.postfix && param.postfix.isString() ? param.postfix.string : "");
  176. const prefixRange =
  177. param.prefix && param.prefix.isString() ? param.prefix.range : null;
  178. const postfixRange =
  179. param.postfix && param.postfix.isString() ? param.postfix.range : null;
  180. const valueRange = /** @type {Range} */ (param.range);
  181. const { context, prefix } = splitContextFromPrefix(prefixRaw);
  182. const {
  183. path: postfix,
  184. query,
  185. fragment
  186. } = parseResource(postfixRaw, parser);
  187. const regExp = new RegExp(
  188. `^${quoteMeta(prefix)}${
  189. /** @type {RegExp} */ (options.wrappedContextRegExp).source
  190. }${quoteMeta(postfix)}$`
  191. );
  192. const dep = new Dep(
  193. {
  194. request: context + query + fragment,
  195. recursive: /** @type {boolean} */ (options.wrappedContextRecursive),
  196. regExp,
  197. mode: "sync",
  198. ...contextOptions
  199. },
  200. range,
  201. valueRange,
  202. ...depArgs
  203. );
  204. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  205. const replaces = [];
  206. if (prefixRange) {
  207. replaces.push({
  208. range: prefixRange,
  209. value: JSON.stringify(prefix)
  210. });
  211. }
  212. if (postfixRange) {
  213. replaces.push({
  214. range: postfixRange,
  215. value: JSON.stringify(postfix)
  216. });
  217. }
  218. dep.replaces = replaces;
  219. dep.critical =
  220. options.wrappedContextCritical &&
  221. "a part of the request of a dependency is an expression";
  222. if (parser && param.wrappedInnerExpressions) {
  223. for (const part of param.wrappedInnerExpressions) {
  224. if (part.expression) parser.walkExpression(part.expression);
  225. }
  226. }
  227. return dep;
  228. } else {
  229. const dep = new Dep(
  230. {
  231. request: /** @type {string} */ (options.exprContextRequest),
  232. recursive: /** @type {boolean} */ (options.exprContextRecursive),
  233. regExp: /** @type {RegExp} */ (options.exprContextRegExp),
  234. mode: "sync",
  235. ...contextOptions
  236. },
  237. range,
  238. /** @type {Range} */ (param.range),
  239. ...depArgs
  240. );
  241. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  242. dep.critical =
  243. options.exprContextCritical &&
  244. "the request of a dependency is an expression";
  245. parser.walkExpression(param.expression);
  246. return dep;
  247. }
  248. };