ModuleWarning.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { cleanUp } = require("./ErrorHelpers");
  7. const WebpackError = require("./WebpackError");
  8. const makeSerializable = require("./util/makeSerializable");
  9. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  10. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  11. class ModuleWarning extends WebpackError {
  12. /**
  13. * Creates an instance of ModuleWarning.
  14. * @param {Error} warning error thrown
  15. * @param {{ from?: string | null }} info additional info
  16. */
  17. constructor(warning, { from = null } = {}) {
  18. let message = "Module Warning";
  19. message += from ? ` (from ${from}):\n` : ": ";
  20. if (warning && typeof warning === "object" && warning.message) {
  21. message += warning.message;
  22. } else if (warning) {
  23. message += String(warning);
  24. }
  25. super(message);
  26. /** @type {string} */
  27. this.name = "ModuleWarning";
  28. this.warning = warning;
  29. this.details =
  30. warning && typeof warning === "object" && warning.stack
  31. ? cleanUp(warning.stack, this.message)
  32. : undefined;
  33. }
  34. /**
  35. * Serializes this instance into the provided serializer context.
  36. * @param {ObjectSerializerContext} context context
  37. */
  38. serialize(context) {
  39. const { write } = context;
  40. write(this.warning);
  41. super.serialize(context);
  42. }
  43. /**
  44. * Restores this instance from the provided deserializer context.
  45. * @param {ObjectDeserializerContext} context context
  46. */
  47. deserialize(context) {
  48. const { read } = context;
  49. this.warning = read();
  50. super.deserialize(context);
  51. }
  52. }
  53. makeSerializable(ModuleWarning, "webpack/lib/ModuleWarning");
  54. /** @type {typeof ModuleWarning} */
  55. module.exports = ModuleWarning;