EnableWasmLoadingPlugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  7. /** @typedef {import("../../declarations/WebpackOptions").WasmLoadingType} WasmLoadingType */
  8. /** @typedef {import("../Compiler")} Compiler */
  9. /** @type {WeakMap<Compiler, Set<WasmLoadingType>>} */
  10. const enabledTypes = new WeakMap();
  11. /**
  12. * @param {Compiler} compiler compiler instance
  13. * @returns {Set<WasmLoadingType>} enabled types
  14. */
  15. const getEnabledTypes = compiler => {
  16. let set = enabledTypes.get(compiler);
  17. if (set === undefined) {
  18. set = new Set();
  19. enabledTypes.set(compiler, set);
  20. }
  21. return set;
  22. };
  23. class EnableWasmLoadingPlugin {
  24. /**
  25. * @param {WasmLoadingType} type library type that should be available
  26. */
  27. constructor(type) {
  28. this.type = type;
  29. }
  30. /**
  31. * @param {Compiler} compiler the compiler instance
  32. * @param {WasmLoadingType} type type of library
  33. * @returns {void}
  34. */
  35. static setEnabled(compiler, type) {
  36. getEnabledTypes(compiler).add(type);
  37. }
  38. /**
  39. * @param {Compiler} compiler the compiler instance
  40. * @param {WasmLoadingType} type type of library
  41. * @returns {void}
  42. */
  43. static checkEnabled(compiler, type) {
  44. if (!getEnabledTypes(compiler).has(type)) {
  45. throw new Error(
  46. `Library type "${type}" is not enabled. ` +
  47. "EnableWasmLoadingPlugin need to be used to enable this type of wasm loading. " +
  48. 'This usually happens through the "output.enabledWasmLoadingTypes" option. ' +
  49. 'If you are using a function as entry which sets "wasmLoading", you need to add all potential library types to "output.enabledWasmLoadingTypes". ' +
  50. "These types are enabled: " +
  51. Array.from(getEnabledTypes(compiler)).join(", ")
  52. );
  53. }
  54. }
  55. /**
  56. * Apply the plugin
  57. * @param {Compiler} compiler the compiler instance
  58. * @returns {void}
  59. */
  60. apply(compiler) {
  61. const { type } = this;
  62. // Only enable once
  63. const enabled = getEnabledTypes(compiler);
  64. if (enabled.has(type)) return;
  65. enabled.add(type);
  66. if (typeof type === "string") {
  67. switch (type) {
  68. case "fetch": {
  69. // TODO webpack 6 remove FetchCompileWasmPlugin
  70. const FetchCompileWasmPlugin = require("../web/FetchCompileWasmPlugin");
  71. const FetchCompileAsyncWasmPlugin = require("../web/FetchCompileAsyncWasmPlugin");
  72. new FetchCompileWasmPlugin({
  73. mangleImports: compiler.options.optimization.mangleWasmImports
  74. }).apply(compiler);
  75. new FetchCompileAsyncWasmPlugin().apply(compiler);
  76. break;
  77. }
  78. case "async-node": {
  79. // TODO webpack 6 remove ReadFileCompileWasmPlugin
  80. const ReadFileCompileWasmPlugin = require("../node/ReadFileCompileWasmPlugin");
  81. // @ts-expect-error typescript bug for duplicate require
  82. const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
  83. new ReadFileCompileWasmPlugin({
  84. mangleImports: compiler.options.optimization.mangleWasmImports
  85. }).apply(compiler);
  86. new ReadFileCompileAsyncWasmPlugin({ type }).apply(compiler);
  87. break;
  88. }
  89. case "async-node-module": {
  90. // @ts-expect-error typescript bug for duplicate require
  91. const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
  92. new ReadFileCompileAsyncWasmPlugin({ type, import: true }).apply(
  93. compiler
  94. );
  95. break;
  96. }
  97. case "universal":
  98. throw new Error(
  99. "Universal WebAssembly Loading is not implemented yet"
  100. );
  101. default:
  102. throw new Error(`Unsupported wasm loading type ${type}.
  103. Plugins which provide custom wasm loading types must call EnableWasmLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
  104. }
  105. } else {
  106. // TODO support plugin instances here
  107. // apply them to the compiler
  108. }
  109. }
  110. }
  111. module.exports = EnableWasmLoadingPlugin;