WebAssemblyUtils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("../Template");
  7. const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
  8. /** @typedef {import("../Module")} Module */
  9. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  10. /**
  11. * @typedef {object} UsedWasmDependency
  12. * @property {WebAssemblyImportDependency} dependency the dependency
  13. * @property {string} name the export name
  14. * @property {string} module the module name
  15. */
  16. const MANGLED_MODULE = "a";
  17. /**
  18. * @param {ModuleGraph} moduleGraph the module graph
  19. * @param {Module} module the module
  20. * @param {boolean | undefined} mangle mangle module and export names
  21. * @returns {UsedWasmDependency[]} used dependencies and (mangled) name
  22. */
  23. const getUsedDependencies = (moduleGraph, module, mangle) => {
  24. /** @type {UsedWasmDependency[]} */
  25. const array = [];
  26. let importIndex = 0;
  27. for (const dep of module.dependencies) {
  28. if (dep instanceof WebAssemblyImportDependency) {
  29. if (
  30. dep.description.type === "GlobalType" ||
  31. moduleGraph.getModule(dep) === null
  32. ) {
  33. continue;
  34. }
  35. const exportName = dep.name;
  36. // TODO add the following 3 lines when removing of ModuleExport is possible
  37. // const importedModule = moduleGraph.getModule(dep);
  38. // const usedName = importedModule && moduleGraph.getExportsInfo(importedModule).getUsedName(exportName, runtime);
  39. // if (usedName !== false) {
  40. if (mangle) {
  41. array.push({
  42. dependency: dep,
  43. name: Template.numberToIdentifier(importIndex++),
  44. module: MANGLED_MODULE
  45. });
  46. } else {
  47. array.push({
  48. dependency: dep,
  49. name: exportName,
  50. module: dep.request
  51. });
  52. }
  53. }
  54. }
  55. return array;
  56. };
  57. exports.getUsedDependencies = getUsedDependencies;
  58. exports.MANGLED_MODULE = MANGLED_MODULE;