LocalModuleDependency.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /** @import { ReplaceSource } from "webpack-sources" */
  9. /** @import Dependency from "../Dependency" */
  10. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  11. /** @import { Range } from "../javascript/JavascriptParser" */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[LocalModule, Range | undefined, boolean]>} ObjectDeserializerContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[LocalModule, Range | undefined, boolean]>} ObjectSerializerContext */
  14. /** @import LocalModule from "./LocalModule" */
  15. class LocalModuleDependency extends NullDependency {
  16. /**
  17. * Creates an instance of LocalModuleDependency.
  18. * @param {LocalModule} localModule local module
  19. * @param {Range | undefined} range range
  20. * @param {boolean} callNew true, when the local module should be called with new
  21. */
  22. constructor(localModule, range, callNew) {
  23. super();
  24. /** @type {LocalModule} */
  25. this.localModule = localModule;
  26. this.range = range;
  27. /** @type {boolean} */
  28. this.callNew = callNew;
  29. }
  30. /**
  31. * Serializes this instance into the provided serializer context.
  32. * @param {ObjectSerializerContext} context context
  33. */
  34. serialize(context) {
  35. context.write(this.localModule).write(this.range).write(this.callNew);
  36. super.serialize(context);
  37. }
  38. /**
  39. * Restores this instance from the provided deserializer context.
  40. * @param {ObjectDeserializerContext} context context
  41. */
  42. deserialize(context) {
  43. this.localModule = context.read();
  44. const c1 = context.rest;
  45. this.range = c1.read();
  46. const c2 = c1.rest;
  47. this.callNew = c2.read();
  48. super.deserialize(c2.rest);
  49. }
  50. }
  51. makeSerializable(
  52. LocalModuleDependency,
  53. "webpack/lib/dependencies/LocalModuleDependency"
  54. );
  55. LocalModuleDependency.Template = class LocalModuleDependencyTemplate extends (
  56. NullDependency.Template
  57. ) {
  58. /**
  59. * Applies the plugin by registering its hooks on the compiler.
  60. * @param {Dependency} dependency the dependency for which the template should be applied
  61. * @param {ReplaceSource} source the current replace source which can be modified
  62. * @param {DependencyTemplateContext} templateContext the context object
  63. * @returns {void}
  64. */
  65. apply(dependency, source, templateContext) {
  66. const dep = /** @type {LocalModuleDependency} */ (dependency);
  67. if (!dep.range) return;
  68. const moduleInstance = dep.callNew
  69. ? `new (function () { return ${dep.localModule.variableName()}; })()`
  70. : dep.localModule.variableName();
  71. source.replace(dep.range[0], dep.range[1] - 1, moduleInstance);
  72. }
  73. };
  74. module.exports = LocalModuleDependency;