UnsupportedDependency.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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<[string, Range]>} ObjectDeserializerContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, Range]>} ObjectSerializerContext */
  14. class UnsupportedDependency extends NullDependency {
  15. /**
  16. * Creates an instance of UnsupportedDependency.
  17. * @param {string} request the request string
  18. * @param {Range} range location in source code
  19. */
  20. constructor(request, range) {
  21. super();
  22. /** @type {string} */
  23. this.request = request;
  24. this.range = range;
  25. }
  26. /**
  27. * Serializes this instance into the provided serializer context.
  28. * @param {ObjectSerializerContext} context context
  29. */
  30. serialize(context) {
  31. context.write(this.request).write(this.range);
  32. super.serialize(context);
  33. }
  34. /**
  35. * Restores this instance from the provided deserializer context.
  36. * @param {ObjectDeserializerContext} context context
  37. */
  38. deserialize(context) {
  39. this.request = context.read();
  40. const c1 = context.rest;
  41. this.range = c1.read();
  42. super.deserialize(c1.rest);
  43. }
  44. }
  45. makeSerializable(
  46. UnsupportedDependency,
  47. "webpack/lib/dependencies/UnsupportedDependency"
  48. );
  49. UnsupportedDependency.Template = class UnsupportedDependencyTemplate extends (
  50. NullDependency.Template
  51. ) {
  52. /**
  53. * Applies the plugin by registering its hooks on the compiler.
  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, { runtimeTemplate }) {
  60. const dep = /** @type {UnsupportedDependency} */ (dependency);
  61. source.replace(
  62. dep.range[0],
  63. dep.range[1],
  64. runtimeTemplate.missingModule({
  65. request: dep.request
  66. })
  67. );
  68. }
  69. };
  70. module.exports = UnsupportedDependency;