Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

WebAssemblyModulesPlugin.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Generator = require("../Generator");
  7. const WebAssemblyParser = require("./WebAssemblyParser");
  8. const WebAssemblyGenerator = require("./WebAssemblyGenerator");
  9. const WebAssemblyJavascriptGenerator = require("./WebAssemblyJavascriptGenerator");
  10. const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
  11. const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
  12. const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError");
  13. /** @typedef {import("../Compiler")} Compiler */
  14. class WebAssemblyModulesPlugin {
  15. constructor(options) {
  16. this.options = options;
  17. }
  18. /**
  19. * @param {Compiler} compiler compiler
  20. * @returns {void}
  21. */
  22. apply(compiler) {
  23. compiler.hooks.compilation.tap(
  24. "WebAssemblyModulesPlugin",
  25. (compilation, { normalModuleFactory }) => {
  26. compilation.dependencyFactories.set(
  27. WebAssemblyImportDependency,
  28. normalModuleFactory
  29. );
  30. compilation.dependencyFactories.set(
  31. WebAssemblyExportImportedDependency,
  32. normalModuleFactory
  33. );
  34. normalModuleFactory.hooks.createParser
  35. .for("webassembly/experimental")
  36. .tap("WebAssemblyModulesPlugin", () => {
  37. return new WebAssemblyParser();
  38. });
  39. normalModuleFactory.hooks.createGenerator
  40. .for("webassembly/experimental")
  41. .tap("WebAssemblyModulesPlugin", () => {
  42. return Generator.byType({
  43. javascript: new WebAssemblyJavascriptGenerator(),
  44. webassembly: new WebAssemblyGenerator(this.options)
  45. });
  46. });
  47. compilation.chunkTemplate.hooks.renderManifest.tap(
  48. "WebAssemblyModulesPlugin",
  49. (result, options) => {
  50. const chunk = options.chunk;
  51. const outputOptions = options.outputOptions;
  52. const moduleTemplates = options.moduleTemplates;
  53. const dependencyTemplates = options.dependencyTemplates;
  54. for (const module of chunk.modulesIterable) {
  55. if (module.type && module.type.startsWith("webassembly")) {
  56. const filenameTemplate =
  57. outputOptions.webassemblyModuleFilename;
  58. result.push({
  59. render: () =>
  60. this.renderWebAssembly(
  61. module,
  62. moduleTemplates.webassembly,
  63. dependencyTemplates
  64. ),
  65. filenameTemplate,
  66. pathOptions: {
  67. module
  68. },
  69. identifier: `webassemblyModule${module.id}`,
  70. hash: module.hash
  71. });
  72. }
  73. }
  74. return result;
  75. }
  76. );
  77. compilation.hooks.afterChunks.tap("WebAssemblyModulesPlugin", () => {
  78. const initialWasmModules = new Set();
  79. for (const chunk of compilation.chunks) {
  80. if (chunk.canBeInitial()) {
  81. for (const module of chunk.modulesIterable) {
  82. if (module.type.startsWith("webassembly")) {
  83. initialWasmModules.add(module);
  84. }
  85. }
  86. }
  87. }
  88. for (const module of initialWasmModules) {
  89. compilation.errors.push(
  90. new WebAssemblyInInitialChunkError(
  91. module,
  92. compilation.requestShortener
  93. )
  94. );
  95. }
  96. });
  97. }
  98. );
  99. }
  100. renderWebAssembly(module, moduleTemplate, dependencyTemplates) {
  101. return moduleTemplate.render(module, dependencyTemplates, {});
  102. }
  103. }
  104. module.exports = WebAssemblyModulesPlugin;