EnableWasmLoadingPlugin.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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: ${[...getEnabledTypes(compiler)].join(", ")}`
  51. );
  52. }
  53. }
  54. /**
  55. * Apply the plugin
  56. * @param {Compiler} compiler the compiler instance
  57. * @returns {void}
  58. */
  59. apply(compiler) {
  60. const { type } = this;
  61. // Only enable once
  62. const enabled = getEnabledTypes(compiler);
  63. if (enabled.has(type)) return;
  64. enabled.add(type);
  65. if (typeof type === "string") {
  66. switch (type) {
  67. case "fetch": {
  68. if (compiler.options.experiments.syncWebAssembly) {
  69. // TODO webpack 6 remove FetchCompileWasmPlugin
  70. const FetchCompileWasmPlugin = require("../web/FetchCompileWasmPlugin");
  71. new FetchCompileWasmPlugin({
  72. mangleImports: compiler.options.optimization.mangleWasmImports
  73. }).apply(compiler);
  74. }
  75. if (compiler.options.experiments.asyncWebAssembly) {
  76. const FetchCompileAsyncWasmPlugin = require("../web/FetchCompileAsyncWasmPlugin");
  77. new FetchCompileAsyncWasmPlugin().apply(compiler);
  78. }
  79. break;
  80. }
  81. case "async-node": {
  82. if (compiler.options.experiments.syncWebAssembly) {
  83. // TODO webpack 6 remove ReadFileCompileWasmPlugin
  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. const UniversalCompileAsyncWasmPlugin = require("../wasm-async/UniversalCompileAsyncWasmPlugin");
  104. new UniversalCompileAsyncWasmPlugin().apply(compiler);
  105. break;
  106. }
  107. default:
  108. throw new Error(`Unsupported wasm loading type ${type}.
  109. Plugins which provide custom wasm loading types must call EnableWasmLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
  110. }
  111. } else {
  112. // TODO support plugin instances here
  113. // apply them to the compiler
  114. }
  115. }
  116. }
  117. module.exports = EnableWasmLoadingPlugin;