[79a0317] | 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 { SyncWaterfallHook } = require("tapable");
|
---|
| 9 | const Compilation = require("../Compilation");
|
---|
| 10 | const Generator = require("../Generator");
|
---|
| 11 | const { tryRunOrWebpackError } = require("../HookWebpackError");
|
---|
| 12 | const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("../ModuleTypeConstants");
|
---|
| 13 | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
|
---|
| 14 | const { compareModulesByIdentifier } = require("../util/comparators");
|
---|
| 15 | const memoize = require("../util/memoize");
|
---|
| 16 |
|
---|
| 17 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 18 | /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
|
---|
| 19 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 20 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 21 | /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
|
---|
| 22 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 23 | /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
|
---|
| 24 | /** @typedef {import("../Module")} Module */
|
---|
| 25 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 26 | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
---|
| 27 | /** @typedef {import("../Template").RenderManifestEntry} RenderManifestEntry */
|
---|
| 28 | /** @typedef {import("../Template").RenderManifestOptions} RenderManifestOptions */
|
---|
| 29 | /** @typedef {import("../WebpackError")} WebpackError */
|
---|
| 30 |
|
---|
| 31 | const getAsyncWebAssemblyGenerator = memoize(() =>
|
---|
| 32 | require("./AsyncWebAssemblyGenerator")
|
---|
| 33 | );
|
---|
| 34 | const getAsyncWebAssemblyJavascriptGenerator = memoize(() =>
|
---|
| 35 | require("./AsyncWebAssemblyJavascriptGenerator")
|
---|
| 36 | );
|
---|
| 37 | const getAsyncWebAssemblyParser = memoize(() =>
|
---|
| 38 | require("./AsyncWebAssemblyParser")
|
---|
| 39 | );
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @typedef {object} WebAssemblyRenderContext
|
---|
| 43 | * @property {Chunk} chunk the chunk
|
---|
| 44 | * @property {DependencyTemplates} dependencyTemplates the dependency templates
|
---|
| 45 | * @property {RuntimeTemplate} runtimeTemplate the runtime template
|
---|
| 46 | * @property {ModuleGraph} moduleGraph the module graph
|
---|
| 47 | * @property {ChunkGraph} chunkGraph the chunk graph
|
---|
| 48 | * @property {CodeGenerationResults} codeGenerationResults results of code generation
|
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * @typedef {object} CompilationHooks
|
---|
| 53 | * @property {SyncWaterfallHook<[Source, Module, WebAssemblyRenderContext]>} renderModuleContent
|
---|
| 54 | */
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * @typedef {object} AsyncWebAssemblyModulesPluginOptions
|
---|
| 58 | * @property {boolean} [mangleImports] mangle imports
|
---|
| 59 | */
|
---|
| 60 |
|
---|
| 61 | /** @type {WeakMap<Compilation, CompilationHooks>} */
|
---|
| 62 | const compilationHooksMap = new WeakMap();
|
---|
| 63 |
|
---|
| 64 | const PLUGIN_NAME = "AsyncWebAssemblyModulesPlugin";
|
---|
| 65 |
|
---|
| 66 | class AsyncWebAssemblyModulesPlugin {
|
---|
| 67 | /**
|
---|
| 68 | * @param {Compilation} compilation the compilation
|
---|
| 69 | * @returns {CompilationHooks} the attached hooks
|
---|
| 70 | */
|
---|
| 71 | static getCompilationHooks(compilation) {
|
---|
| 72 | if (!(compilation instanceof Compilation)) {
|
---|
| 73 | throw new TypeError(
|
---|
| 74 | "The 'compilation' argument must be an instance of Compilation"
|
---|
| 75 | );
|
---|
| 76 | }
|
---|
| 77 | let hooks = compilationHooksMap.get(compilation);
|
---|
| 78 | if (hooks === undefined) {
|
---|
| 79 | hooks = {
|
---|
| 80 | renderModuleContent: new SyncWaterfallHook([
|
---|
| 81 | "source",
|
---|
| 82 | "module",
|
---|
| 83 | "renderContext"
|
---|
| 84 | ])
|
---|
| 85 | };
|
---|
| 86 | compilationHooksMap.set(compilation, hooks);
|
---|
| 87 | }
|
---|
| 88 | return hooks;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * @param {AsyncWebAssemblyModulesPluginOptions} options options
|
---|
| 93 | */
|
---|
| 94 | constructor(options) {
|
---|
| 95 | this.options = options;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Apply the plugin
|
---|
| 100 | * @param {Compiler} compiler the compiler instance
|
---|
| 101 | * @returns {void}
|
---|
| 102 | */
|
---|
| 103 | apply(compiler) {
|
---|
| 104 | compiler.hooks.compilation.tap(
|
---|
| 105 | PLUGIN_NAME,
|
---|
| 106 | (compilation, { normalModuleFactory }) => {
|
---|
| 107 | const hooks =
|
---|
| 108 | AsyncWebAssemblyModulesPlugin.getCompilationHooks(compilation);
|
---|
| 109 | compilation.dependencyFactories.set(
|
---|
| 110 | WebAssemblyImportDependency,
|
---|
| 111 | normalModuleFactory
|
---|
| 112 | );
|
---|
| 113 |
|
---|
| 114 | normalModuleFactory.hooks.createParser
|
---|
| 115 | .for(WEBASSEMBLY_MODULE_TYPE_ASYNC)
|
---|
| 116 | .tap(PLUGIN_NAME, () => {
|
---|
| 117 | const AsyncWebAssemblyParser = getAsyncWebAssemblyParser();
|
---|
| 118 |
|
---|
| 119 | return new AsyncWebAssemblyParser();
|
---|
| 120 | });
|
---|
| 121 | normalModuleFactory.hooks.createGenerator
|
---|
| 122 | .for(WEBASSEMBLY_MODULE_TYPE_ASYNC)
|
---|
| 123 | .tap(PLUGIN_NAME, () => {
|
---|
| 124 | const AsyncWebAssemblyJavascriptGenerator =
|
---|
| 125 | getAsyncWebAssemblyJavascriptGenerator();
|
---|
| 126 | const AsyncWebAssemblyGenerator = getAsyncWebAssemblyGenerator();
|
---|
| 127 |
|
---|
| 128 | return Generator.byType({
|
---|
| 129 | javascript: new AsyncWebAssemblyJavascriptGenerator(
|
---|
| 130 | compilation.outputOptions.webassemblyModuleFilename
|
---|
| 131 | ),
|
---|
| 132 | webassembly: new AsyncWebAssemblyGenerator(this.options)
|
---|
| 133 | });
|
---|
| 134 | });
|
---|
| 135 |
|
---|
| 136 | compilation.hooks.renderManifest.tap(
|
---|
| 137 | "WebAssemblyModulesPlugin",
|
---|
| 138 | (result, options) => {
|
---|
| 139 | const { moduleGraph, chunkGraph, runtimeTemplate } = compilation;
|
---|
| 140 | const {
|
---|
| 141 | chunk,
|
---|
| 142 | outputOptions,
|
---|
| 143 | dependencyTemplates,
|
---|
| 144 | codeGenerationResults
|
---|
| 145 | } = options;
|
---|
| 146 |
|
---|
| 147 | for (const module of chunkGraph.getOrderedChunkModulesIterable(
|
---|
| 148 | chunk,
|
---|
| 149 | compareModulesByIdentifier
|
---|
| 150 | )) {
|
---|
| 151 | if (module.type === WEBASSEMBLY_MODULE_TYPE_ASYNC) {
|
---|
| 152 | const filenameTemplate =
|
---|
| 153 | /** @type {NonNullable<OutputOptions["webassemblyModuleFilename"]>} */
|
---|
| 154 | (outputOptions.webassemblyModuleFilename);
|
---|
| 155 |
|
---|
| 156 | result.push({
|
---|
| 157 | render: () =>
|
---|
| 158 | this.renderModule(
|
---|
| 159 | module,
|
---|
| 160 | {
|
---|
| 161 | chunk,
|
---|
| 162 | dependencyTemplates,
|
---|
| 163 | runtimeTemplate,
|
---|
| 164 | moduleGraph,
|
---|
| 165 | chunkGraph,
|
---|
| 166 | codeGenerationResults
|
---|
| 167 | },
|
---|
| 168 | hooks
|
---|
| 169 | ),
|
---|
| 170 | filenameTemplate,
|
---|
| 171 | pathOptions: {
|
---|
| 172 | module,
|
---|
| 173 | runtime: chunk.runtime,
|
---|
| 174 | chunkGraph
|
---|
| 175 | },
|
---|
| 176 | auxiliary: true,
|
---|
| 177 | identifier: `webassemblyAsyncModule${chunkGraph.getModuleId(
|
---|
| 178 | module
|
---|
| 179 | )}`,
|
---|
| 180 | hash: chunkGraph.getModuleHash(module, chunk.runtime)
|
---|
| 181 | });
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | return result;
|
---|
| 186 | }
|
---|
| 187 | );
|
---|
| 188 | }
|
---|
| 189 | );
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * @param {Module} module the rendered module
|
---|
| 194 | * @param {WebAssemblyRenderContext} renderContext options object
|
---|
| 195 | * @param {CompilationHooks} hooks hooks
|
---|
| 196 | * @returns {Source} the newly generated source from rendering
|
---|
| 197 | */
|
---|
| 198 | renderModule(module, renderContext, hooks) {
|
---|
| 199 | const { codeGenerationResults, chunk } = renderContext;
|
---|
| 200 | try {
|
---|
| 201 | const moduleSource = codeGenerationResults.getSource(
|
---|
| 202 | module,
|
---|
| 203 | chunk.runtime,
|
---|
| 204 | "webassembly"
|
---|
| 205 | );
|
---|
| 206 | return tryRunOrWebpackError(
|
---|
| 207 | () =>
|
---|
| 208 | hooks.renderModuleContent.call(moduleSource, module, renderContext),
|
---|
| 209 | "AsyncWebAssemblyModulesPlugin.getCompilationHooks().renderModuleContent"
|
---|
| 210 | );
|
---|
| 211 | } catch (err) {
|
---|
| 212 | /** @type {WebpackError} */ (err).module = module;
|
---|
| 213 | throw err;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | module.exports = AsyncWebAssemblyModulesPlugin;
|
---|