EnvironmentNotSupportAsyncWarning.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Gengkun He @ahabhgk
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. const makeSerializable = require("./util/makeSerializable");
  8. /** @typedef {import("./Module")} Module */
  9. /** @typedef {import("./Compilation")} Compilation */
  10. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  11. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  12. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. /** @typedef {"asyncWebAssembly" | "topLevelAwait" | "external promise" | "external script" | "external import" | "external module"} Feature */
  14. class EnvironmentNotSupportAsyncWarning extends WebpackError {
  15. /**
  16. * Creates an instance of EnvironmentNotSupportAsyncWarning.
  17. * @param {Module} module module
  18. * @param {Feature} feature feature
  19. */
  20. constructor(module, feature) {
  21. const message = `The generated code contains 'async/await' because this module is using "${feature}".
  22. However, your target environment does not appear to support 'async/await'.
  23. As a result, the code may not run as expected or may cause runtime errors.`;
  24. super(message);
  25. this.name = "EnvironmentNotSupportAsyncWarning";
  26. this.module = module;
  27. }
  28. /**
  29. * Creates an instance of EnvironmentNotSupportAsyncWarning.
  30. * @param {Module} module module
  31. * @param {RuntimeTemplate} runtimeTemplate compilation
  32. * @param {Feature} feature feature
  33. */
  34. static check(module, runtimeTemplate, feature) {
  35. if (!runtimeTemplate.supportsAsyncFunction()) {
  36. module.addWarning(new EnvironmentNotSupportAsyncWarning(module, feature));
  37. }
  38. }
  39. }
  40. makeSerializable(
  41. EnvironmentNotSupportAsyncWarning,
  42. "webpack/lib/EnvironmentNotSupportAsyncWarning"
  43. );
  44. module.exports = EnvironmentNotSupportAsyncWarning;