AsyncWebAssemblyGenerator.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /** @type {AsyncWebAssemblyGeneratorOptions} */
  25. this.options = options;
  26. }
  27. /**
  28. * @param {NormalModule} module fresh module
  29. * @returns {SourceTypes} available types (do not mutate)
  30. */
  31. getTypes(module) {
  32. return WEBASSEMBLY_TYPES;
  33. }
  34. /**
  35. * @param {NormalModule} module the module
  36. * @param {SourceType=} type source type
  37. * @returns {number} estimate size of the module
  38. */
  39. getSize(module, type) {
  40. const originalSource = module.originalSource();
  41. if (!originalSource) {
  42. return 0;
  43. }
  44. return originalSource.size();
  45. }
  46. /**
  47. * @param {NormalModule} module module for which the code should be generated
  48. * @param {GenerateContext} generateContext context for generate
  49. * @returns {Source | null} generated code
  50. */
  51. generate(module, generateContext) {
  52. return /** @type {Source} */ (module.originalSource());
  53. }
  54. /**
  55. * @param {Error} error the error
  56. * @param {NormalModule} module module for which the code should be generated
  57. * @param {GenerateContext} generateContext context for generate
  58. * @returns {Source | null} generated code
  59. */
  60. generateError(error, module, generateContext) {
  61. return new RawSource(error.message);
  62. }
  63. }
  64. module.exports = AsyncWebAssemblyGenerator;