HarmonyExportHeaderDependency.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. class HarmonyExportHeaderDependency extends NullDependency {
  15. /**
  16. * @param {Range | false} range range
  17. * @param {Range} rangeStatement range statement
  18. */
  19. constructor(range, rangeStatement) {
  20. super();
  21. this.range = range;
  22. this.rangeStatement = rangeStatement;
  23. }
  24. get type() {
  25. return "harmony export header";
  26. }
  27. /**
  28. * @param {ObjectSerializerContext} context context
  29. */
  30. serialize(context) {
  31. const { write } = context;
  32. write(this.range);
  33. write(this.rangeStatement);
  34. super.serialize(context);
  35. }
  36. /**
  37. * @param {ObjectDeserializerContext} context context
  38. */
  39. deserialize(context) {
  40. const { read } = context;
  41. this.range = read();
  42. this.rangeStatement = read();
  43. super.deserialize(context);
  44. }
  45. }
  46. makeSerializable(
  47. HarmonyExportHeaderDependency,
  48. "webpack/lib/dependencies/HarmonyExportHeaderDependency"
  49. );
  50. HarmonyExportHeaderDependency.Template = class HarmonyExportDependencyTemplate extends (
  51. NullDependency.Template
  52. ) {
  53. /**
  54. * @param {Dependency} dependency the dependency for which the template should be applied
  55. * @param {ReplaceSource} source the current replace source which can be modified
  56. * @param {DependencyTemplateContext} templateContext the context object
  57. * @returns {void}
  58. */
  59. apply(dependency, source, templateContext) {
  60. const dep = /** @type {HarmonyExportHeaderDependency} */ (dependency);
  61. const content = "";
  62. const replaceUntil = dep.range
  63. ? dep.range[0] - 1
  64. : dep.rangeStatement[1] - 1;
  65. source.replace(dep.rangeStatement[0], replaceUntil, content);
  66. }
  67. };
  68. module.exports = HarmonyExportHeaderDependency;