EnableWasmLoadingPlugin.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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: ${Array.from(
  51. getEnabledTypes(compiler)
  52. ).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. // TODO webpack 6 remove FetchCompileWasmPlugin
  72. const FetchCompileWasmPlugin = require("../web/FetchCompileWasmPlugin");
  73. new FetchCompileWasmPlugin({
  74. mangleImports: compiler.options.optimization.mangleWasmImports
  75. }).apply(compiler);
  76. }
  77. if (compiler.options.experiments.asyncWebAssembly) {
  78. const FetchCompileAsyncWasmPlugin = require("../web/FetchCompileAsyncWasmPlugin");
  79. new FetchCompileAsyncWasmPlugin().apply(compiler);
  80. }
  81. break;
  82. }
  83. case "async-node": {
  84. if (compiler.options.experiments.syncWebAssembly) {
  85. // TODO webpack 6 remove ReadFileCompileWasmPlugin
  86. const ReadFileCompileWasmPlugin = require("../node/ReadFileCompileWasmPlugin");
  87. new ReadFileCompileWasmPlugin({
  88. mangleImports: compiler.options.optimization.mangleWasmImports,
  89. import:
  90. compiler.options.output.environment.module &&
  91. compiler.options.output.environment.dynamicImport
  92. }).apply(compiler);
  93. }
  94. if (compiler.options.experiments.asyncWebAssembly) {
  95. const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
  96. new ReadFileCompileAsyncWasmPlugin({
  97. import:
  98. compiler.options.output.environment.module &&
  99. compiler.options.output.environment.dynamicImport
  100. }).apply(compiler);
  101. }
  102. break;
  103. }
  104. case "universal": {
  105. const UniversalCompileAsyncWasmPlugin = require("../wasm-async/UniversalCompileAsyncWasmPlugin");
  106. new UniversalCompileAsyncWasmPlugin().apply(compiler);
  107. break;
  108. }
  109. default:
  110. throw new Error(`Unsupported wasm loading type ${type}.
  111. Plugins which provide custom wasm loading types must call EnableWasmLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
  112. }
  113. } else {
  114. // TODO support plugin instances here
  115. // apply them to the compiler
  116. }
  117. }
  118. }
  119. module.exports = EnableWasmLoadingPlugin;