| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const makeSerializable = require("../util/makeSerializable");
- const UnsupportedWebAssemblyFeatureError = require("../wasm-sync/UnsupportedWebAssemblyFeatureError");
- const ModuleDependency = require("./ModuleDependency");
- /** @import { ModuleImportDescription } from "@webassemblyjs/ast" */
- /** @import { ReferencedExports } from "../Dependency" */
- /** @import ModuleGraph from "../ModuleGraph" */
- /** @import WebpackError from "../errors/WebpackError" */
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, ModuleImportDescription, false | string]>} ObjectDeserializerContext */
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, ModuleImportDescription, false | string]>} ObjectSerializerContext */
- /** @import { RuntimeSpec } from "../util/runtime" */
- class WebAssemblyImportDependency extends ModuleDependency {
- /**
- * Creates an instance of WebAssemblyImportDependency.
- * @param {string} request the request
- * @param {string} name the imported name
- * @param {ModuleImportDescription} description the WASM ast node
- * @param {false | string} onlyDirectImport if only direct imports are allowed
- */
- constructor(request, name, description, onlyDirectImport) {
- super(request);
- /** @type {string} */
- this.name = name;
- /** @type {ModuleImportDescription} */
- this.description = description;
- /** @type {false | string} */
- this.onlyDirectImport = onlyDirectImport;
- }
- get type() {
- return "wasm import";
- }
- get category() {
- return "wasm";
- }
- /**
- * Returns list of exports referenced by this dependency
- * @param {ModuleGraph} moduleGraph module graph
- * @param {RuntimeSpec} runtime the runtime for which the module is analysed
- * @returns {ReferencedExports} referenced exports
- */
- getReferencedExports(moduleGraph, runtime) {
- // wasm import glue needs the real binding, never an inlined literal
- return [{ name: [this.name], canInline: false }];
- }
- /**
- * Returns errors.
- * @param {ModuleGraph} moduleGraph module graph
- * @returns {WebpackError[] | null | undefined} errors
- */
- getErrors(moduleGraph) {
- const module = moduleGraph.getModule(this);
- if (
- this.onlyDirectImport &&
- module &&
- !module.type.startsWith("webassembly")
- ) {
- return [
- new UnsupportedWebAssemblyFeatureError(
- `Import "${this.name}" from "${this.request}" with ${this.onlyDirectImport} can only be used for direct wasm to wasm dependencies`
- )
- ];
- }
- }
- /**
- * Serializes this instance into the provided serializer context.
- * @param {ObjectSerializerContext} context context
- */
- serialize(context) {
- context
- .write(this.name)
- .write(this.description)
- .write(this.onlyDirectImport);
- super.serialize(context);
- }
- /**
- * Restores this instance from the provided deserializer context.
- * @param {ObjectDeserializerContext} context context
- */
- deserialize(context) {
- this.name = context.read();
- const c1 = context.rest;
- this.description = c1.read();
- const c2 = c1.rest;
- this.onlyDirectImport = c2.read();
- super.deserialize(c2.rest);
- }
- }
- makeSerializable(
- WebAssemblyImportDependency,
- "webpack/lib/dependencies/WebAssemblyImportDependency"
- );
- module.exports = WebAssemblyImportDependency;
|