UI for Zipcoin Blue

CommonJsRequireDependencyParserPlugin.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const CommonJsRequireDependency = require("./CommonJsRequireDependency");
  7. const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
  8. const RequireHeaderDependency = require("./RequireHeaderDependency");
  9. const LocalModuleDependency = require("./LocalModuleDependency");
  10. const ContextDependencyHelpers = require("./ContextDependencyHelpers");
  11. const LocalModulesHelpers = require("./LocalModulesHelpers");
  12. const ParserHelpers = require("../ParserHelpers");
  13. class CommonJsRequireDependencyParserPlugin {
  14. constructor(options) {
  15. this.options = options;
  16. }
  17. apply(parser) {
  18. const options = this.options;
  19. parser.plugin("expression require.cache", ParserHelpers.toConstantDependency("__webpack_require__.c"));
  20. parser.plugin("expression require", (expr) => {
  21. const dep = new CommonJsRequireContextDependency(options.unknownContextRequest, options.unknownContextRecursive, options.unknownContextRegExp, expr.range);
  22. dep.critical = options.unknownContextCritical && "require function is used in a way in which dependencies cannot be statically extracted";
  23. dep.loc = expr.loc;
  24. dep.optional = !!parser.scope.inTry;
  25. parser.state.current.addDependency(dep);
  26. return true;
  27. });
  28. parser.plugin("call require", (expr) => {
  29. if(expr.arguments.length !== 1) return;
  30. let localModule;
  31. const param = parser.evaluateExpression(expr.arguments[0]);
  32. if(param.isConditional()) {
  33. let isExpression = false;
  34. const prevLength = parser.state.current.dependencies.length;
  35. const dep = new RequireHeaderDependency(expr.callee.range);
  36. dep.loc = expr.loc;
  37. parser.state.current.addDependency(dep);
  38. param.options.forEach(function(param) {
  39. const result = parser.applyPluginsBailResult("call require:commonjs:item", expr, param);
  40. if(result === undefined) {
  41. isExpression = true;
  42. }
  43. });
  44. if(isExpression) {
  45. parser.state.current.dependencies.length = prevLength;
  46. } else {
  47. return true;
  48. }
  49. }
  50. if(param.isString() && (localModule = LocalModulesHelpers.getLocalModule(parser.state, param.string))) {
  51. const dep = new LocalModuleDependency(localModule, expr.range);
  52. dep.loc = expr.loc;
  53. parser.state.current.addDependency(dep);
  54. return true;
  55. } else {
  56. const result = parser.applyPluginsBailResult("call require:commonjs:item", expr, param);
  57. if(result === undefined) {
  58. parser.applyPluginsBailResult("call require:commonjs:context", expr, param);
  59. } else {
  60. const dep = new RequireHeaderDependency(expr.callee.range);
  61. dep.loc = expr.loc;
  62. parser.state.current.addDependency(dep);
  63. }
  64. return true;
  65. }
  66. });
  67. parser.plugin("call require:commonjs:item", (expr, param) => {
  68. if(param.isString()) {
  69. const dep = new CommonJsRequireDependency(param.string, param.range);
  70. dep.loc = expr.loc;
  71. dep.optional = !!parser.scope.inTry;
  72. parser.state.current.addDependency(dep);
  73. return true;
  74. }
  75. });
  76. parser.plugin("call require:commonjs:context", (expr, param) => {
  77. const dep = ContextDependencyHelpers.create(CommonJsRequireContextDependency, expr.range, param, expr, options);
  78. if(!dep) return;
  79. dep.loc = expr.loc;
  80. dep.optional = !!parser.scope.inTry;
  81. parser.state.current.addDependency(dep);
  82. return true;
  83. });
  84. }
  85. }
  86. module.exports = CommonJsRequireDependencyParserPlugin;