EnableWasmLoadingPlugin.js 3.7 KB

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