AsyncWebAssemblyGenerator.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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("../ModuleSourceTypesConstants");
  9. /** @typedef {import("webpack-sources").Source} Source */
  10. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  11. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  12. /** @typedef {import("../NormalModule")} NormalModule */
  13. /**
  14. * @typedef {object} AsyncWebAssemblyGeneratorOptions
  15. * @property {boolean} [mangleImports] mangle imports
  16. */
  17. class AsyncWebAssemblyGenerator extends Generator {
  18. /**
  19. * @param {AsyncWebAssemblyGeneratorOptions} options options
  20. */
  21. constructor(options) {
  22. super();
  23. this.options = options;
  24. }
  25. /**
  26. * @param {NormalModule} module fresh module
  27. * @returns {SourceTypes} available types (do not mutate)
  28. */
  29. getTypes(module) {
  30. return WEBASSEMBLY_TYPES;
  31. }
  32. /**
  33. * @param {NormalModule} module the module
  34. * @param {string=} type source type
  35. * @returns {number} estimate size of the module
  36. */
  37. getSize(module, type) {
  38. const originalSource = module.originalSource();
  39. if (!originalSource) {
  40. return 0;
  41. }
  42. return originalSource.size();
  43. }
  44. /**
  45. * @param {NormalModule} module module for which the code should be generated
  46. * @param {GenerateContext} generateContext context for generate
  47. * @returns {Source | null} generated code
  48. */
  49. generate(module, generateContext) {
  50. return /** @type {Source} */ (module.originalSource());
  51. }
  52. /**
  53. * @param {Error} error the error
  54. * @param {NormalModule} module module for which the code should be generated
  55. * @param {GenerateContext} generateContext context for generate
  56. * @returns {Source | null} generated code
  57. */
  58. generateError(error, module, generateContext) {
  59. return new RawSource(error.message);
  60. }
  61. }
  62. module.exports = AsyncWebAssemblyGenerator;