UI for Zipcoin Blue

RequireContextDependencyParserPlugin.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequireContextDependency = require("./RequireContextDependency");
  7. module.exports = class RequireContextDependencyParserPlugin {
  8. apply(parser) {
  9. parser.plugin("call require.context", expr => {
  10. let regExp = /^\.\/.*$/;
  11. let recursive = true;
  12. let asyncMode;
  13. switch(expr.arguments.length) {
  14. case 4:
  15. {
  16. const asyncModeExpr = parser.evaluateExpression(expr.arguments[3]);
  17. if(!asyncModeExpr.isString()) return;
  18. asyncMode = asyncModeExpr.string;
  19. }
  20. // falls through
  21. case 3:
  22. {
  23. const regExpExpr = parser.evaluateExpression(expr.arguments[2]);
  24. if(!regExpExpr.isRegExp()) return;
  25. regExp = regExpExpr.regExp;
  26. }
  27. // falls through
  28. case 2:
  29. {
  30. const recursiveExpr = parser.evaluateExpression(expr.arguments[1]);
  31. if(!recursiveExpr.isBoolean()) return;
  32. recursive = recursiveExpr.bool;
  33. }
  34. // falls through
  35. case 1:
  36. {
  37. const requestExpr = parser.evaluateExpression(expr.arguments[0]);
  38. if(!requestExpr.isString()) return;
  39. const dep = new RequireContextDependency(requestExpr.string, recursive, regExp, asyncMode, expr.range);
  40. dep.loc = expr.loc;
  41. dep.optional = parser.scope.inTry;
  42. parser.state.current.addDependency(dep);
  43. return true;
  44. }
  45. }
  46. });
  47. }
  48. };