WebAssemblyUtils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /** @typedef {Object} UsedWasmDependency
  11. * @property {WebAssemblyImportDependency} dependency the dependency
  12. * @property {string} name the export name
  13. * @property {string} module the module name
  14. */
  15. const MANGLED_MODULE = "a";
  16. /**
  17. * @param {ModuleGraph} moduleGraph the module graph
  18. * @param {Module} module the module
  19. * @param {boolean} mangle mangle module and export names
  20. * @returns {UsedWasmDependency[]} used dependencies and (mangled) name
  21. */
  22. const getUsedDependencies = (moduleGraph, module, mangle) => {
  23. /** @type {UsedWasmDependency[]} */
  24. const array = [];
  25. let importIndex = 0;
  26. for (const dep of module.dependencies) {
  27. if (dep instanceof WebAssemblyImportDependency) {
  28. if (
  29. dep.description.type === "GlobalType" ||
  30. moduleGraph.getModule(dep) === null
  31. ) {
  32. continue;
  33. }
  34. const exportName = dep.name;
  35. // TODO add the following 3 lines when removing of ModuleExport is possible
  36. // const importedModule = moduleGraph.getModule(dep);
  37. // const usedName = importedModule && moduleGraph.getExportsInfo(importedModule).getUsedName(exportName, runtime);
  38. // if (usedName !== false) {
  39. if (mangle) {
  40. array.push({
  41. dependency: dep,
  42. name: Template.numberToIdentifier(importIndex++),
  43. module: MANGLED_MODULE
  44. });
  45. } else {
  46. array.push({
  47. dependency: dep,
  48. name: exportName,
  49. module: dep.request
  50. });
  51. }
  52. }
  53. }
  54. return array;
  55. };
  56. exports.getUsedDependencies = getUsedDependencies;
  57. exports.MANGLED_MODULE = MANGLED_MODULE;