UnsupportedDependency.js 2.1 KB

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