LocalModuleDependency.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const NullDependency = require("./NullDependency");
  8. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../Dependency")} Dependency */
  10. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  11. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  14. /** @typedef {import("./LocalModule")} LocalModule */
  15. class LocalModuleDependency extends NullDependency {
  16. /**
  17. * @param {LocalModule} localModule local module
  18. * @param {Range | undefined} range range
  19. * @param {boolean} callNew true, when the local module should be called with new
  20. */
  21. constructor(localModule, range, callNew) {
  22. super();
  23. this.localModule = localModule;
  24. this.range = range;
  25. this.callNew = callNew;
  26. }
  27. /**
  28. * @param {ObjectSerializerContext} context context
  29. */
  30. serialize(context) {
  31. const { write } = context;
  32. write(this.localModule);
  33. write(this.range);
  34. write(this.callNew);
  35. super.serialize(context);
  36. }
  37. /**
  38. * @param {ObjectDeserializerContext} context context
  39. */
  40. deserialize(context) {
  41. const { read } = context;
  42. this.localModule = read();
  43. this.range = read();
  44. this.callNew = read();
  45. super.deserialize(context);
  46. }
  47. }
  48. makeSerializable(
  49. LocalModuleDependency,
  50. "webpack/lib/dependencies/LocalModuleDependency"
  51. );
  52. LocalModuleDependency.Template = class LocalModuleDependencyTemplate extends (
  53. NullDependency.Template
  54. ) {
  55. /**
  56. * @param {Dependency} dependency the dependency for which the template should be applied
  57. * @param {ReplaceSource} source the current replace source which can be modified
  58. * @param {DependencyTemplateContext} templateContext the context object
  59. * @returns {void}
  60. */
  61. apply(dependency, source, templateContext) {
  62. const dep = /** @type {LocalModuleDependency} */ (dependency);
  63. if (!dep.range) return;
  64. const moduleInstance = dep.callNew
  65. ? `new (function () { return ${dep.localModule.variableName()}; })()`
  66. : dep.localModule.variableName();
  67. source.replace(dep.range[0], dep.range[1] - 1, moduleInstance);
  68. }
  69. };
  70. module.exports = LocalModuleDependency;