source: imaps-frontend/node_modules/webpack/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { SyncWaterfallHook } = require("tapable");
9const Compilation = require("../Compilation");
10const Generator = require("../Generator");
11const { tryRunOrWebpackError } = require("../HookWebpackError");
12const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("../ModuleTypeConstants");
13const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
14const { compareModulesByIdentifier } = require("../util/comparators");
15const 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
31const getAsyncWebAssemblyGenerator = memoize(() =>
32 require("./AsyncWebAssemblyGenerator")
33);
34const getAsyncWebAssemblyJavascriptGenerator = memoize(() =>
35 require("./AsyncWebAssemblyJavascriptGenerator")
36);
37const 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>} */
62const compilationHooksMap = new WeakMap();
63
64const PLUGIN_NAME = "AsyncWebAssemblyModulesPlugin";
65
66class 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
218module.exports = AsyncWebAssemblyModulesPlugin;
Note: See TracBrowser for help on using the repository browser.