ModuleWarning.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 makeSerializable = require("../util/makeSerializable");
  8. const WebpackError = require("./WebpackError");
  9. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Error]>} ObjectDeserializerContext */
  10. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Error]>} 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. /** @type {Error} */
  29. this.warning = warning;
  30. /** @type {string | undefined} */
  31. this.details =
  32. warning && typeof warning === "object" && warning.stack
  33. ? cleanUp(warning.stack, this.message)
  34. : undefined;
  35. }
  36. /**
  37. * Serializes this instance into the provided serializer context.
  38. * @param {ObjectSerializerContext} context context
  39. */
  40. serialize(context) {
  41. context.write(this.warning);
  42. super.serialize(context);
  43. }
  44. /**
  45. * Restores this instance from the provided deserializer context.
  46. * @param {ObjectDeserializerContext} context context
  47. */
  48. deserialize(context) {
  49. this.warning = context.read();
  50. super.deserialize(context.rest);
  51. }
  52. }
  53. makeSerializable(ModuleWarning, "webpack/lib/errors/ModuleWarning");
  54. /** @type {typeof ModuleWarning} */
  55. module.exports = ModuleWarning;