123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
-
- const Generator = require("../Generator");
- const WebAssemblyParser = require("./WebAssemblyParser");
- const WebAssemblyGenerator = require("./WebAssemblyGenerator");
- const WebAssemblyJavascriptGenerator = require("./WebAssemblyJavascriptGenerator");
- const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
- const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
- const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError");
-
- /** @typedef {import("../Compiler")} Compiler */
-
- class WebAssemblyModulesPlugin {
- constructor(options) {
- this.options = options;
- }
-
- /**
- * @param {Compiler} compiler compiler
- * @returns {void}
- */
- apply(compiler) {
- compiler.hooks.compilation.tap(
- "WebAssemblyModulesPlugin",
- (compilation, { normalModuleFactory }) => {
- compilation.dependencyFactories.set(
- WebAssemblyImportDependency,
- normalModuleFactory
- );
-
- compilation.dependencyFactories.set(
- WebAssemblyExportImportedDependency,
- normalModuleFactory
- );
-
- normalModuleFactory.hooks.createParser
- .for("webassembly/experimental")
- .tap("WebAssemblyModulesPlugin", () => {
- return new WebAssemblyParser();
- });
-
- normalModuleFactory.hooks.createGenerator
- .for("webassembly/experimental")
- .tap("WebAssemblyModulesPlugin", () => {
- return Generator.byType({
- javascript: new WebAssemblyJavascriptGenerator(),
- webassembly: new WebAssemblyGenerator(this.options)
- });
- });
-
- compilation.chunkTemplate.hooks.renderManifest.tap(
- "WebAssemblyModulesPlugin",
- (result, options) => {
- const chunk = options.chunk;
- const outputOptions = options.outputOptions;
- const moduleTemplates = options.moduleTemplates;
- const dependencyTemplates = options.dependencyTemplates;
-
- for (const module of chunk.modulesIterable) {
- if (module.type && module.type.startsWith("webassembly")) {
- const filenameTemplate =
- outputOptions.webassemblyModuleFilename;
-
- result.push({
- render: () =>
- this.renderWebAssembly(
- module,
- moduleTemplates.webassembly,
- dependencyTemplates
- ),
- filenameTemplate,
- pathOptions: {
- module
- },
- identifier: `webassemblyModule${module.id}`,
- hash: module.hash
- });
- }
- }
-
- return result;
- }
- );
-
- compilation.hooks.afterChunks.tap("WebAssemblyModulesPlugin", () => {
- const initialWasmModules = new Set();
- for (const chunk of compilation.chunks) {
- if (chunk.canBeInitial()) {
- for (const module of chunk.modulesIterable) {
- if (module.type.startsWith("webassembly")) {
- initialWasmModules.add(module);
- }
- }
- }
- }
- for (const module of initialWasmModules) {
- compilation.errors.push(
- new WebAssemblyInInitialChunkError(
- module,
- compilation.requestShortener
- )
- );
- }
- });
- }
- );
- }
-
- renderWebAssembly(module, moduleTemplate, dependencyTemplates) {
- return moduleTemplate.render(module, dependencyTemplates, {});
- }
- }
-
- module.exports = WebAssemblyModulesPlugin;
|