source: imaps-frontend/node_modules/webpack/lib/wasm-sync/WebAssemblyModulesPlugin.js@ 79a0317

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[79a0317]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Generator = require("../Generator");
9const { WEBASSEMBLY_MODULE_TYPE_SYNC } = require("../ModuleTypeConstants");
10const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
11const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
12const { compareModulesByIdentifier } = require("../util/comparators");
13const memoize = require("../util/memoize");
14const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError");
15
16/** @typedef {import("webpack-sources").Source} Source */
17/** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
18/** @typedef {import("../Compiler")} Compiler */
19/** @typedef {import("../Module")} Module */
20/** @typedef {import("../ModuleTemplate")} ModuleTemplate */
21/** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
22
23const getWebAssemblyGenerator = memoize(() =>
24 require("./WebAssemblyGenerator")
25);
26const getWebAssemblyJavascriptGenerator = memoize(() =>
27 require("./WebAssemblyJavascriptGenerator")
28);
29const getWebAssemblyParser = memoize(() => require("./WebAssemblyParser"));
30
31const PLUGIN_NAME = "WebAssemblyModulesPlugin";
32
33/**
34 * @typedef {object} WebAssemblyModulesPluginOptions
35 * @property {boolean} [mangleImports] mangle imports
36 */
37
38class WebAssemblyModulesPlugin {
39 /**
40 * @param {WebAssemblyModulesPluginOptions} options options
41 */
42 constructor(options) {
43 this.options = options;
44 }
45
46 /**
47 * Apply the plugin
48 * @param {Compiler} compiler the compiler instance
49 * @returns {void}
50 */
51 apply(compiler) {
52 compiler.hooks.compilation.tap(
53 PLUGIN_NAME,
54 (compilation, { normalModuleFactory }) => {
55 compilation.dependencyFactories.set(
56 WebAssemblyImportDependency,
57 normalModuleFactory
58 );
59
60 compilation.dependencyFactories.set(
61 WebAssemblyExportImportedDependency,
62 normalModuleFactory
63 );
64
65 normalModuleFactory.hooks.createParser
66 .for(WEBASSEMBLY_MODULE_TYPE_SYNC)
67 .tap(PLUGIN_NAME, () => {
68 const WebAssemblyParser = getWebAssemblyParser();
69
70 return new WebAssemblyParser();
71 });
72
73 normalModuleFactory.hooks.createGenerator
74 .for(WEBASSEMBLY_MODULE_TYPE_SYNC)
75 .tap(PLUGIN_NAME, () => {
76 const WebAssemblyJavascriptGenerator =
77 getWebAssemblyJavascriptGenerator();
78 const WebAssemblyGenerator = getWebAssemblyGenerator();
79
80 return Generator.byType({
81 javascript: new WebAssemblyJavascriptGenerator(),
82 webassembly: new WebAssemblyGenerator(this.options)
83 });
84 });
85
86 compilation.hooks.renderManifest.tap(PLUGIN_NAME, (result, options) => {
87 const { chunkGraph } = compilation;
88 const { chunk, outputOptions, codeGenerationResults } = options;
89
90 for (const module of chunkGraph.getOrderedChunkModulesIterable(
91 chunk,
92 compareModulesByIdentifier
93 )) {
94 if (module.type === WEBASSEMBLY_MODULE_TYPE_SYNC) {
95 const filenameTemplate =
96 /** @type {NonNullable<OutputOptions["webassemblyModuleFilename"]>} */
97 (outputOptions.webassemblyModuleFilename);
98
99 result.push({
100 render: () =>
101 codeGenerationResults.getSource(
102 module,
103 chunk.runtime,
104 "webassembly"
105 ),
106 filenameTemplate,
107 pathOptions: {
108 module,
109 runtime: chunk.runtime,
110 chunkGraph
111 },
112 auxiliary: true,
113 identifier: `webassemblyModule${chunkGraph.getModuleId(
114 module
115 )}`,
116 hash: chunkGraph.getModuleHash(module, chunk.runtime)
117 });
118 }
119 }
120
121 return result;
122 });
123
124 compilation.hooks.afterChunks.tap(PLUGIN_NAME, () => {
125 const chunkGraph = compilation.chunkGraph;
126 const initialWasmModules = new Set();
127 for (const chunk of compilation.chunks) {
128 if (chunk.canBeInitial()) {
129 for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
130 if (module.type === WEBASSEMBLY_MODULE_TYPE_SYNC) {
131 initialWasmModules.add(module);
132 }
133 }
134 }
135 }
136 for (const module of initialWasmModules) {
137 compilation.errors.push(
138 new WebAssemblyInInitialChunkError(
139 module,
140 compilation.moduleGraph,
141 compilation.chunkGraph,
142 compilation.requestShortener
143 )
144 );
145 }
146 });
147 }
148 );
149 }
150}
151
152module.exports = WebAssemblyModulesPlugin;
Note: See TracBrowser for help on using the repository browser.