AsyncWebAssemblyGenerator.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { RawSource } = require("webpack-sources");
  7. const Generator = require("../Generator");
  8. const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypeConstants");
  9. /** @typedef {import("webpack-sources").Source} Source */
  10. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  11. /** @typedef {import("../Module").SourceType} SourceType */
  12. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  13. /** @typedef {import("../NormalModule")} NormalModule */
  14. /**
  15. * @typedef {object} AsyncWebAssemblyGeneratorOptions
  16. * @property {boolean=} mangleImports mangle imports
  17. */
  18. class AsyncWebAssemblyGenerator extends Generator {
  19. /**
  20. * @param {AsyncWebAssemblyGeneratorOptions} options options
  21. */
  22. constructor(options) {
  23. super();
  24. this.options = options;
  25. }
  26. /**
  27. * @param {NormalModule} module fresh module
  28. * @returns {SourceTypes} available types (do not mutate)
  29. */
  30. getTypes(module) {
  31. return WEBASSEMBLY_TYPES;
  32. }
  33. /**
  34. * @param {NormalModule} module the module
  35. * @param {SourceType=} type source type
  36. * @returns {number} estimate size of the module
  37. */
  38. getSize(module, type) {
  39. const originalSource = module.originalSource();
  40. if (!originalSource) {
  41. return 0;
  42. }
  43. return originalSource.size();
  44. }
  45. /**
  46. * @param {NormalModule} module module for which the code should be generated
  47. * @param {GenerateContext} generateContext context for generate
  48. * @returns {Source | null} generated code
  49. */
  50. generate(module, generateContext) {
  51. return /** @type {Source} */ (module.originalSource());
  52. }
  53. /**
  54. * @param {Error} error the error
  55. * @param {NormalModule} module module for which the code should be generated
  56. * @param {GenerateContext} generateContext context for generate
  57. * @returns {Source | null} generated code
  58. */
  59. generateError(error, module, generateContext) {
  60. return new RawSource(error.message);
  61. }
  62. }
  63. module.exports = AsyncWebAssemblyGenerator;