TransformModuleErrorsPlugin.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const WebpackError = require('webpack/lib/WebpackError');
  2. const ModuleError = require('webpack/lib/ModuleError');
  3. const ModuleWarning = require('webpack/lib/ModuleWarning');
  4. const pluginCompat = require('./util/plugin-compat');
  5. class TransformModuleErrorsPlugin {
  6. apply(compiler) {
  7. let freeze;
  8. let thaw;
  9. let mapFreeze;
  10. let mapThaw;
  11. pluginCompat.tap(
  12. compiler,
  13. '_hardSourceMethods',
  14. 'TransformModuleErrorsPlugin',
  15. methods => {
  16. // store = methods.store;
  17. // fetch = methods.fetch;
  18. freeze = methods.freeze;
  19. thaw = methods.thaw;
  20. mapFreeze = methods.mapFreeze;
  21. mapThaw = methods.mapThaw;
  22. },
  23. );
  24. function freezeErrorWarning(
  25. frozen,
  26. { message, details, originLoc, dependencies, name, loc, constructor },
  27. extra,
  28. ) {
  29. return {
  30. constructor: constructor.name,
  31. message: message,
  32. details: details,
  33. originLoc: originLoc,
  34. dependencies:
  35. dependencies && mapFreeze('Dependency', null, dependencies, extra),
  36. name: name,
  37. loc: loc,
  38. };
  39. }
  40. pluginCompat.tap(
  41. compiler,
  42. '_hardSourceFreezeModuleError',
  43. 'TransformModuleErrorsPlugin',
  44. freezeErrorWarning,
  45. );
  46. pluginCompat.tap(
  47. compiler,
  48. '_hardSourceFreezeModuleWarning',
  49. 'TransformModuleErrorsPlugin',
  50. freezeErrorWarning,
  51. );
  52. function thawError(
  53. ErrorClass,
  54. error,
  55. { constructor, message, details, originLoc, dependencies, name, loc },
  56. extra,
  57. ) {
  58. const module = extra.module;
  59. error = new ErrorClass(module, message);
  60. if (constructor === 'SystemImportDeprecationWarning') {
  61. error = new WebpackError(message);
  62. error.module = module;
  63. }
  64. if (details) {
  65. error.details = details;
  66. }
  67. if (extra.origin) {
  68. error.origin = extra.origin;
  69. }
  70. if (originLoc) {
  71. error.originLoc = originLoc;
  72. }
  73. if (dependencies) {
  74. error.dependencies = mapThaw('Dependency', null, dependencies, extra);
  75. }
  76. if (name) {
  77. error.name = name;
  78. }
  79. if (loc) {
  80. error.loc = loc;
  81. }
  82. return error;
  83. }
  84. pluginCompat.tap(
  85. compiler,
  86. '_hardSourceThawModuleError',
  87. 'TransformModuleErrorsPlugin',
  88. (error, frozen, extra) => thawError(ModuleError, error, frozen, extra),
  89. );
  90. pluginCompat.tap(
  91. compiler,
  92. '_hardSourceThawModuleWarning',
  93. 'TransformModuleErrorsPlugin',
  94. (warning, frozen, extra) =>
  95. thawError(ModuleWarning, warning, frozen, extra),
  96. );
  97. }
  98. }
  99. module.exports = TransformModuleErrorsPlugin;