UI for Zipcoin Blue

CommonJsPlugin.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ConstDependency = require("./ConstDependency");
  7. const CommonJsRequireDependency = require("./CommonJsRequireDependency");
  8. const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
  9. const RequireResolveDependency = require("./RequireResolveDependency");
  10. const RequireResolveContextDependency = require("./RequireResolveContextDependency");
  11. const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency");
  12. const RequireHeaderDependency = require("./RequireHeaderDependency");
  13. const NullFactory = require("../NullFactory");
  14. const RequireResolveDependencyParserPlugin = require("./RequireResolveDependencyParserPlugin");
  15. const CommonJsRequireDependencyParserPlugin = require("./CommonJsRequireDependencyParserPlugin");
  16. const ParserHelpers = require("../ParserHelpers");
  17. class CommonJsPlugin {
  18. constructor(options) {
  19. this.options = options;
  20. }
  21. apply(compiler) {
  22. const options = this.options;
  23. compiler.plugin("compilation", (compilation, params) => {
  24. const normalModuleFactory = params.normalModuleFactory;
  25. const contextModuleFactory = params.contextModuleFactory;
  26. compilation.dependencyFactories.set(CommonJsRequireDependency, normalModuleFactory);
  27. compilation.dependencyTemplates.set(CommonJsRequireDependency, new CommonJsRequireDependency.Template());
  28. compilation.dependencyFactories.set(CommonJsRequireContextDependency, contextModuleFactory);
  29. compilation.dependencyTemplates.set(CommonJsRequireContextDependency, new CommonJsRequireContextDependency.Template());
  30. compilation.dependencyFactories.set(RequireResolveDependency, normalModuleFactory);
  31. compilation.dependencyTemplates.set(RequireResolveDependency, new RequireResolveDependency.Template());
  32. compilation.dependencyFactories.set(RequireResolveContextDependency, contextModuleFactory);
  33. compilation.dependencyTemplates.set(RequireResolveContextDependency, new RequireResolveContextDependency.Template());
  34. compilation.dependencyFactories.set(RequireResolveHeaderDependency, new NullFactory());
  35. compilation.dependencyTemplates.set(RequireResolveHeaderDependency, new RequireResolveHeaderDependency.Template());
  36. compilation.dependencyFactories.set(RequireHeaderDependency, new NullFactory());
  37. compilation.dependencyTemplates.set(RequireHeaderDependency, new RequireHeaderDependency.Template());
  38. params.normalModuleFactory.plugin("parser", (parser, parserOptions) => {
  39. if(typeof parserOptions.commonjs !== "undefined" && !parserOptions.commonjs)
  40. return;
  41. const requireExpressions = ["require", "require.resolve", "require.resolveWeak"];
  42. for(let expression of requireExpressions) {
  43. parser.plugin(`typeof ${expression}`, ParserHelpers.toConstantDependency(JSON.stringify("function")));
  44. parser.plugin(`evaluate typeof ${expression}`, ParserHelpers.evaluateToString("function"));
  45. parser.plugin(`evaluate Identifier ${expression}`, ParserHelpers.evaluateToIdentifier(expression, true));
  46. }
  47. parser.plugin("evaluate typeof module", ParserHelpers.evaluateToString("object"));
  48. parser.plugin("assign require", (expr) => {
  49. // to not leak to global "require", we need to define a local require here.
  50. const dep = new ConstDependency("var require;", 0);
  51. dep.loc = expr.loc;
  52. parser.state.current.addDependency(dep);
  53. parser.scope.definitions.push("require");
  54. return true;
  55. });
  56. parser.plugin("can-rename require", () => true);
  57. parser.plugin("rename require", (expr) => {
  58. // define the require variable. It's still undefined, but not "not defined".
  59. const dep = new ConstDependency("var require;", 0);
  60. dep.loc = expr.loc;
  61. parser.state.current.addDependency(dep);
  62. return false;
  63. });
  64. parser.plugin("typeof module", () => true);
  65. parser.plugin("evaluate typeof exports", ParserHelpers.evaluateToString("object"));
  66. parser.apply(
  67. new CommonJsRequireDependencyParserPlugin(options),
  68. new RequireResolveDependencyParserPlugin(options)
  69. );
  70. });
  71. });
  72. }
  73. }
  74. module.exports = CommonJsPlugin;