RequireContextDependencyParserPlugin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. switch(expr.arguments.length) {
  13. case 3:
  14. {
  15. const regExpExpr = parser.evaluateExpression(expr.arguments[2]);
  16. if(!regExpExpr.isRegExp()) return;
  17. regExp = regExpExpr.regExp;
  18. }
  19. // falls through
  20. case 2:
  21. {
  22. const recursiveExpr = parser.evaluateExpression(expr.arguments[1]);
  23. if(!recursiveExpr.isBoolean()) return;
  24. recursive = recursiveExpr.bool;
  25. }
  26. // falls through
  27. case 1:
  28. {
  29. const requestExpr = parser.evaluateExpression(expr.arguments[0]);
  30. if(!requestExpr.isString()) return;
  31. const dep = new RequireContextDependency(requestExpr.string, recursive, regExp, expr.range);
  32. dep.loc = expr.loc;
  33. dep.optional = parser.scope.inTry;
  34. parser.state.current.addDependency(dep);
  35. return true;
  36. }
  37. }
  38. });
  39. }
  40. };