webpack/lib/ModuleWarning.js

71 lines
1.6 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const { cleanUp } = require("./ErrorHelpers");
2018-07-30 23:08:51 +08:00
const WebpackError = require("./WebpackError");
2018-10-25 16:47:52 +08:00
const makeSerializable = require("./util/makeSerializable");
2023-04-12 02:57:43 +08:00
/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
class ModuleWarning extends WebpackError {
2018-11-07 21:03:25 +08:00
/**
* @param {Error} warning error thrown
2020-08-03 02:09:36 +08:00
* @param {{from?: string|null}} info additional info
2018-11-07 21:03:25 +08:00
*/
constructor(warning, { from = null } = {}) {
let message = "Module Warning";
2018-10-25 16:47:52 +08:00
2018-03-02 17:56:34 +08:00
if (from) {
2018-06-05 16:43:10 +08:00
message += ` (from ${from}):\n`;
} else {
message += ": ";
}
2018-10-25 16:47:52 +08:00
2018-03-02 17:56:34 +08:00
if (warning && typeof warning === "object" && warning.message) {
2018-06-05 16:43:10 +08:00
message += warning.message;
2018-03-02 17:56:34 +08:00
} else if (warning) {
2018-11-07 21:03:25 +08:00
message += String(warning);
}
2018-10-25 16:47:52 +08:00
super(message);
2018-10-25 16:47:52 +08:00
this.name = "ModuleWarning";
this.warning = warning;
2018-02-25 09:00:20 +08:00
this.details =
warning && typeof warning === "object" && warning.stack
? cleanUp(warning.stack, this.message)
: undefined;
}
2018-10-25 16:47:52 +08:00
2023-04-12 02:57:43 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2018-10-25 16:47:52 +08:00
serialize(context) {
const { write } = context;
write(this.warning);
super.serialize(context);
}
2023-04-12 02:57:43 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2018-10-25 16:47:52 +08:00
deserialize(context) {
const { read } = context;
this.warning = read();
super.deserialize(context);
}
}
2018-10-25 16:47:52 +08:00
makeSerializable(ModuleWarning, "webpack/lib/ModuleWarning");
module.exports = ModuleWarning;