LocalModuleDependency.js 768 B

12345678910111213141516171819202122232425262728
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NullDependency = require("./NullDependency");
  7. class LocalModuleDependency extends NullDependency {
  8. constructor(localModule, range, callNew) {
  9. super();
  10. localModule.flagUsed();
  11. this.localModule = localModule;
  12. this.range = range;
  13. this.callNew = callNew;
  14. }
  15. }
  16. LocalModuleDependency.Template = class LocalModuleDependencyTemplate {
  17. apply(dep, source) {
  18. if (!dep.range) return;
  19. const moduleInstance = dep.callNew
  20. ? `new (function () { return ${dep.localModule.variableName()}; })()`
  21. : dep.localModule.variableName();
  22. source.replace(dep.range[0], dep.range[1] - 1, moduleInstance);
  23. }
  24. };
  25. module.exports = LocalModuleDependency;