ModuleWarning.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * @param {Error} warning error thrown
  14. * @param {{ from?: string | null }} info additional info
  15. */
  16. constructor(warning, { from = null } = {}) {
  17. let message = "Module Warning";
  18. message += from ? ` (from ${from}):\n` : ": ";
  19. if (warning && typeof warning === "object" && warning.message) {
  20. message += warning.message;
  21. } else if (warning) {
  22. message += String(warning);
  23. }
  24. super(message);
  25. /** @type {string} */
  26. this.name = "ModuleWarning";
  27. this.warning = warning;
  28. this.details =
  29. warning && typeof warning === "object" && warning.stack
  30. ? cleanUp(warning.stack, this.message)
  31. : undefined;
  32. }
  33. /**
  34. * @param {ObjectSerializerContext} context context
  35. */
  36. serialize(context) {
  37. const { write } = context;
  38. write(this.warning);
  39. super.serialize(context);
  40. }
  41. /**
  42. * @param {ObjectDeserializerContext} context context
  43. */
  44. deserialize(context) {
  45. const { read } = context;
  46. this.warning = read();
  47. super.deserialize(context);
  48. }
  49. }
  50. makeSerializable(ModuleWarning, "webpack/lib/ModuleWarning");
  51. /** @type {typeof ModuleWarning} */
  52. module.exports = ModuleWarning;