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