ModuleWarning.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (from) {
  19. message += ` (from ${from}):\n`;
  20. } else {
  21. message += ": ";
  22. }
  23. if (warning && typeof warning === "object" && warning.message) {
  24. message += warning.message;
  25. } else if (warning) {
  26. message += String(warning);
  27. }
  28. super(message);
  29. this.name = "ModuleWarning";
  30. this.warning = warning;
  31. this.details =
  32. warning && typeof warning === "object" && warning.stack
  33. ? cleanUp(warning.stack, this.message)
  34. : undefined;
  35. }
  36. /**
  37. * @param {ObjectSerializerContext} context context
  38. */
  39. serialize(context) {
  40. const { write } = context;
  41. write(this.warning);
  42. super.serialize(context);
  43. }
  44. /**
  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. module.exports = ModuleWarning;