| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const Generator = require("../Generator");
|
|---|
| 9 | const {
|
|---|
| 10 | JAVASCRIPT_TYPE,
|
|---|
| 11 | WEBASSEMBLY_TYPE
|
|---|
| 12 | } = require("../ModuleSourceTypeConstants");
|
|---|
| 13 | const { WEBASSEMBLY_MODULE_TYPE_SYNC } = require("../ModuleTypeConstants");
|
|---|
| 14 | const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
|
|---|
| 15 | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
|
|---|
| 16 | const { compareModulesByFullName } = require("../util/comparators");
|
|---|
| 17 | const memoize = require("../util/memoize");
|
|---|
| 18 | const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError");
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 21 | /** @typedef {import("../Module")} Module */
|
|---|
| 22 |
|
|---|
| 23 | const getWebAssemblyGenerator = memoize(() =>
|
|---|
| 24 | require("./WebAssemblyGenerator")
|
|---|
| 25 | );
|
|---|
| 26 | const getWebAssemblyJavascriptGenerator = memoize(() =>
|
|---|
| 27 | require("./WebAssemblyJavascriptGenerator")
|
|---|
| 28 | );
|
|---|
| 29 | const getWebAssemblyParser = memoize(() => require("./WebAssemblyParser"));
|
|---|
| 30 |
|
|---|
| 31 | const PLUGIN_NAME = "WebAssemblyModulesPlugin";
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Options that influence how synchronous WebAssembly modules are transformed
|
|---|
| 35 | * and emitted.
|
|---|
| 36 | * @typedef {object} WebAssemblyModulesPluginOptions
|
|---|
| 37 | * @property {boolean=} mangleImports mangle imports
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Adds parser, generator, manifest, and validation support for synchronous
|
|---|
| 42 | * WebAssembly modules in the compilation pipeline.
|
|---|
| 43 | */
|
|---|
| 44 | class WebAssemblyModulesPlugin {
|
|---|
| 45 | /**
|
|---|
| 46 | * Stores options that affect generated synchronous WebAssembly output.
|
|---|
| 47 | * @param {WebAssemblyModulesPluginOptions} options options
|
|---|
| 48 | */
|
|---|
| 49 | constructor(options) {
|
|---|
| 50 | this.options = options;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Registers compilation hooks that parse and generate sync WebAssembly
|
|---|
| 55 | * modules, emit their binary assets, and report invalid placement in initial
|
|---|
| 56 | * chunks.
|
|---|
| 57 | * @param {Compiler} compiler the compiler instance
|
|---|
| 58 | * @returns {void}
|
|---|
| 59 | */
|
|---|
| 60 | apply(compiler) {
|
|---|
| 61 | compiler.hooks.compilation.tap(
|
|---|
| 62 | PLUGIN_NAME,
|
|---|
| 63 | (compilation, { normalModuleFactory }) => {
|
|---|
| 64 | compilation.dependencyFactories.set(
|
|---|
| 65 | WebAssemblyImportDependency,
|
|---|
| 66 | normalModuleFactory
|
|---|
| 67 | );
|
|---|
| 68 |
|
|---|
| 69 | compilation.dependencyFactories.set(
|
|---|
| 70 | WebAssemblyExportImportedDependency,
|
|---|
| 71 | normalModuleFactory
|
|---|
| 72 | );
|
|---|
| 73 |
|
|---|
| 74 | normalModuleFactory.hooks.createParser
|
|---|
| 75 | .for(WEBASSEMBLY_MODULE_TYPE_SYNC)
|
|---|
| 76 | .tap(PLUGIN_NAME, () => {
|
|---|
| 77 | const WebAssemblyParser = getWebAssemblyParser();
|
|---|
| 78 |
|
|---|
| 79 | return new WebAssemblyParser();
|
|---|
| 80 | });
|
|---|
| 81 |
|
|---|
| 82 | normalModuleFactory.hooks.createGenerator
|
|---|
| 83 | .for(WEBASSEMBLY_MODULE_TYPE_SYNC)
|
|---|
| 84 | .tap(PLUGIN_NAME, () => {
|
|---|
| 85 | const WebAssemblyJavascriptGenerator =
|
|---|
| 86 | getWebAssemblyJavascriptGenerator();
|
|---|
| 87 | const WebAssemblyGenerator = getWebAssemblyGenerator();
|
|---|
| 88 |
|
|---|
| 89 | return Generator.byType({
|
|---|
| 90 | [JAVASCRIPT_TYPE]: new WebAssemblyJavascriptGenerator(),
|
|---|
| 91 | [WEBASSEMBLY_TYPE]: new WebAssemblyGenerator(this.options)
|
|---|
| 92 | });
|
|---|
| 93 | });
|
|---|
| 94 |
|
|---|
| 95 | compilation.hooks.renderManifest.tap(PLUGIN_NAME, (result, options) => {
|
|---|
| 96 | const { chunkGraph } = compilation;
|
|---|
| 97 | const { chunk, outputOptions, codeGenerationResults } = options;
|
|---|
| 98 |
|
|---|
| 99 | for (const module of chunkGraph.getOrderedChunkModulesIterable(
|
|---|
| 100 | chunk,
|
|---|
| 101 | compareModulesByFullName(compiler)
|
|---|
| 102 | )) {
|
|---|
| 103 | if (module.type === WEBASSEMBLY_MODULE_TYPE_SYNC) {
|
|---|
| 104 | const filenameTemplate = outputOptions.webassemblyModuleFilename;
|
|---|
| 105 |
|
|---|
| 106 | result.push({
|
|---|
| 107 | render: () =>
|
|---|
| 108 | codeGenerationResults.getSource(
|
|---|
| 109 | module,
|
|---|
| 110 | chunk.runtime,
|
|---|
| 111 | "webassembly"
|
|---|
| 112 | ),
|
|---|
| 113 | filenameTemplate,
|
|---|
| 114 | pathOptions: {
|
|---|
| 115 | module,
|
|---|
| 116 | runtime: chunk.runtime,
|
|---|
| 117 | chunkGraph
|
|---|
| 118 | },
|
|---|
| 119 | auxiliary: true,
|
|---|
| 120 | identifier: `webassemblyModule${chunkGraph.getModuleId(
|
|---|
| 121 | module
|
|---|
| 122 | )}`,
|
|---|
| 123 | hash: chunkGraph.getModuleHash(module, chunk.runtime)
|
|---|
| 124 | });
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return result;
|
|---|
| 129 | });
|
|---|
| 130 |
|
|---|
| 131 | compilation.hooks.afterChunks.tap(PLUGIN_NAME, () => {
|
|---|
| 132 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 133 | /** @type {Set<Module>} */
|
|---|
| 134 | const initialWasmModules = new Set();
|
|---|
| 135 | for (const chunk of compilation.chunks) {
|
|---|
| 136 | if (chunk.canBeInitial()) {
|
|---|
| 137 | for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
|
|---|
| 138 | if (module.type === WEBASSEMBLY_MODULE_TYPE_SYNC) {
|
|---|
| 139 | initialWasmModules.add(module);
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | for (const module of initialWasmModules) {
|
|---|
| 145 | compilation.errors.push(
|
|---|
| 146 | new WebAssemblyInInitialChunkError(
|
|---|
| 147 | module,
|
|---|
| 148 | compilation.moduleGraph,
|
|---|
| 149 | compilation.chunkGraph,
|
|---|
| 150 | compilation.requestShortener
|
|---|
| 151 | )
|
|---|
| 152 | );
|
|---|
| 153 | }
|
|---|
| 154 | });
|
|---|
| 155 | }
|
|---|
| 156 | );
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | module.exports = WebAssemblyModulesPlugin;
|
|---|