ContextDependencyHelpers.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ContextDependencyHelpers = exports;
  7. /**
  8. * Escapes regular expression metacharacters
  9. * @param {string} str String to quote
  10. * @return {string} Escaped string
  11. */
  12. function quotemeta(str) {
  13. return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
  14. }
  15. ContextDependencyHelpers.create = function(Dep, range, param, expr, options, chunkName) {
  16. let dep;
  17. let prefix;
  18. let postfix;
  19. let prefixRange;
  20. let valueRange;
  21. let idx;
  22. let context;
  23. let regExp;
  24. if(param.isTemplateString()) {
  25. prefix = param.quasis[0].string;
  26. postfix = param.quasis.length > 1 ? param.quasis[param.quasis.length - 1].string : "";
  27. prefixRange = [param.quasis[0].range[0], param.quasis[0].range[1]];
  28. valueRange = param.range;
  29. idx = prefix.lastIndexOf("/");
  30. context = ".";
  31. if(idx >= 0) {
  32. context = prefix.substr(0, idx);
  33. prefix = `.${prefix.substr(idx)}`;
  34. }
  35. // If there are more than two quasis, maybe the generated RegExp can be more precise?
  36. regExp = new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
  37. dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange, chunkName);
  38. dep.loc = expr.loc;
  39. dep.replaces = [{
  40. range: prefixRange,
  41. value: prefix
  42. }];
  43. dep.critical = options.wrappedContextCritical && "a part of the request of a dependency is an expression";
  44. return dep;
  45. } else if(param.isWrapped() && (param.prefix && param.prefix.isString() || param.postfix && param.postfix.isString())) {
  46. prefix = param.prefix && param.prefix.isString() ? param.prefix.string : "";
  47. postfix = param.postfix && param.postfix.isString() ? param.postfix.string : "";
  48. prefixRange = param.prefix && param.prefix.isString() ? param.prefix.range : null;
  49. valueRange = [prefixRange ? prefixRange[1] : param.range[0], param.range[1]];
  50. idx = prefix.lastIndexOf("/");
  51. context = ".";
  52. if(idx >= 0) {
  53. context = prefix.substr(0, idx);
  54. prefix = `.${prefix.substr(idx)}`;
  55. }
  56. regExp = new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
  57. dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange, chunkName);
  58. dep.loc = expr.loc;
  59. dep.prepend = param.prefix && param.prefix.isString() ? prefix : null;
  60. dep.critical = options.wrappedContextCritical && "a part of the request of a dependency is an expression";
  61. return dep;
  62. } else {
  63. dep = new Dep(options.exprContextRequest, options.exprContextRecursive, options.exprContextRegExp, range, param.range, chunkName);
  64. dep.loc = expr.loc;
  65. dep.critical = options.exprContextCritical && "the request of a dependency is an expression";
  66. return dep;
  67. }
  68. };