EnableWasmLoadingPlugin.js 4.2 KB

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