UnsupportedDependency.js 2.4 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. /** @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 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. this.request = request;
  23. this.range = range;
  24. }
  25. /**
  26. * Serializes this instance into the provided serializer context.
  27. * @param {ObjectSerializerContext} context context
  28. */
  29. serialize(context) {
  30. const { write } = context;
  31. write(this.request);
  32. write(this.range);
  33. super.serialize(context);
  34. }
  35. /**
  36. * Restores this instance from the provided deserializer context.
  37. * @param {ObjectDeserializerContext} context context
  38. */
  39. deserialize(context) {
  40. const { read } = context;
  41. this.request = read();
  42. this.range = read();
  43. super.deserialize(context);
  44. }
  45. }
  46. makeSerializable(
  47. UnsupportedDependency,
  48. "webpack/lib/dependencies/UnsupportedDependency"
  49. );
  50. UnsupportedDependency.Template = class UnsupportedDependencyTemplate extends (
  51. NullDependency.Template
  52. ) {
  53. /**
  54. * Applies the plugin by registering its hooks on the compiler.
  55. * @param {Dependency} dependency the dependency for which the template should be applied
  56. * @param {ReplaceSource} source the current replace source which can be modified
  57. * @param {DependencyTemplateContext} templateContext the context object
  58. * @returns {void}
  59. */
  60. apply(dependency, source, { runtimeTemplate }) {
  61. const dep = /** @type {UnsupportedDependency} */ (dependency);
  62. source.replace(
  63. dep.range[0],
  64. dep.range[1],
  65. runtimeTemplate.missingModule({
  66. request: dep.request
  67. })
  68. );
  69. }
  70. };
  71. module.exports = UnsupportedDependency;