| 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 makeSerializable = require("../util/makeSerializable");
|
|---|
| 9 | const UnsupportedWebAssemblyFeatureError = require("../wasm-sync/UnsupportedWebAssemblyFeatureError");
|
|---|
| 10 | const ModuleDependency = require("./ModuleDependency");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("@webassemblyjs/ast").ModuleImportDescription} ModuleImportDescription */
|
|---|
| 13 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 14 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 15 | /** @typedef {import("../errors/WebpackError")} WebpackError */
|
|---|
| 16 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 18 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 19 |
|
|---|
| 20 | class WebAssemblyImportDependency extends ModuleDependency {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of WebAssemblyImportDependency.
|
|---|
| 23 | * @param {string} request the request
|
|---|
| 24 | * @param {string} name the imported name
|
|---|
| 25 | * @param {ModuleImportDescription} description the WASM ast node
|
|---|
| 26 | * @param {false | string} onlyDirectImport if only direct imports are allowed
|
|---|
| 27 | */
|
|---|
| 28 | constructor(request, name, description, onlyDirectImport) {
|
|---|
| 29 | super(request);
|
|---|
| 30 | /** @type {string} */
|
|---|
| 31 | this.name = name;
|
|---|
| 32 | /** @type {ModuleImportDescription} */
|
|---|
| 33 | this.description = description;
|
|---|
| 34 | /** @type {false | string} */
|
|---|
| 35 | this.onlyDirectImport = onlyDirectImport;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | get type() {
|
|---|
| 39 | return "wasm import";
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | get category() {
|
|---|
| 43 | return "wasm";
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Returns list of exports referenced by this dependency
|
|---|
| 48 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 49 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 50 | * @returns {ReferencedExports} referenced exports
|
|---|
| 51 | */
|
|---|
| 52 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 53 | return [[this.name]];
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Returns errors.
|
|---|
| 58 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 59 | * @returns {WebpackError[] | null | undefined} errors
|
|---|
| 60 | */
|
|---|
| 61 | getErrors(moduleGraph) {
|
|---|
| 62 | const module = moduleGraph.getModule(this);
|
|---|
| 63 |
|
|---|
| 64 | if (
|
|---|
| 65 | this.onlyDirectImport &&
|
|---|
| 66 | module &&
|
|---|
| 67 | !module.type.startsWith("webassembly")
|
|---|
| 68 | ) {
|
|---|
| 69 | return [
|
|---|
| 70 | new UnsupportedWebAssemblyFeatureError(
|
|---|
| 71 | `Import "${this.name}" from "${this.request}" with ${this.onlyDirectImport} can only be used for direct wasm to wasm dependencies`
|
|---|
| 72 | )
|
|---|
| 73 | ];
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Serializes this instance into the provided serializer context.
|
|---|
| 79 | * @param {ObjectSerializerContext} context context
|
|---|
| 80 | */
|
|---|
| 81 | serialize(context) {
|
|---|
| 82 | const { write } = context;
|
|---|
| 83 |
|
|---|
| 84 | write(this.name);
|
|---|
| 85 | write(this.description);
|
|---|
| 86 | write(this.onlyDirectImport);
|
|---|
| 87 |
|
|---|
| 88 | super.serialize(context);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Restores this instance from the provided deserializer context.
|
|---|
| 93 | * @param {ObjectDeserializerContext} context context
|
|---|
| 94 | */
|
|---|
| 95 | deserialize(context) {
|
|---|
| 96 | const { read } = context;
|
|---|
| 97 |
|
|---|
| 98 | this.name = read();
|
|---|
| 99 | this.description = read();
|
|---|
| 100 | this.onlyDirectImport = read();
|
|---|
| 101 |
|
|---|
| 102 | super.deserialize(context);
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | makeSerializable(
|
|---|
| 107 | WebAssemblyImportDependency,
|
|---|
| 108 | "webpack/lib/dependencies/WebAssemblyImportDependency"
|
|---|
| 109 | );
|
|---|
| 110 |
|
|---|
| 111 | module.exports = WebAssemblyImportDependency;
|
|---|