2017-12-15 22:39:53 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-12-15 22:39:53 +08:00
|
|
|
"use strict";
|
2018-04-04 15:17:10 +08:00
|
|
|
|
2018-10-12 00:37:32 +08:00
|
|
|
const makeSerializable = require("../util/makeSerializable");
|
2020-09-09 15:23:01 +08:00
|
|
|
const UnsupportedWebAssemblyFeatureError = require("../wasm-sync/UnsupportedWebAssemblyFeatureError");
|
2017-12-15 22:39:53 +08:00
|
|
|
const ModuleDependency = require("./ModuleDependency");
|
|
|
|
|
2018-05-11 20:13:06 +08:00
|
|
|
/** @typedef {import("@webassemblyjs/ast").ModuleImportDescription} ModuleImportDescription */
|
2020-05-28 02:34:55 +08:00
|
|
|
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
2018-07-24 23:35:36 +08:00
|
|
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("../WebpackError")} WebpackError */
|
2023-04-12 03:22:51 +08:00
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
2020-07-28 00:09:48 +08:00
|
|
|
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
2018-07-25 15:33:48 +08:00
|
|
|
|
2017-12-15 22:39:53 +08:00
|
|
|
class WebAssemblyImportDependency extends ModuleDependency {
|
2018-05-11 20:13:06 +08:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2018-04-28 17:09:13 +08:00
|
|
|
constructor(request, name, description, onlyDirectImport) {
|
2017-12-15 22:39:53 +08:00
|
|
|
super(request);
|
2018-05-11 00:07:24 +08:00
|
|
|
/** @type {string} */
|
2017-12-15 22:39:53 +08:00
|
|
|
this.name = name;
|
2018-05-11 20:13:06 +08:00
|
|
|
/** @type {ModuleImportDescription} */
|
2018-03-09 00:54:06 +08:00
|
|
|
this.description = description;
|
2018-05-11 00:07:24 +08:00
|
|
|
/** @type {false | string} */
|
2018-04-28 17:09:13 +08:00
|
|
|
this.onlyDirectImport = onlyDirectImport;
|
2017-12-15 22:39:53 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 05:03:02 +08:00
|
|
|
get type() {
|
|
|
|
return "wasm import";
|
|
|
|
}
|
|
|
|
|
|
|
|
get category() {
|
|
|
|
return "wasm";
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
2019-10-30 05:28:42 +08:00
|
|
|
* Returns list of exports referenced by this dependency
|
2018-07-24 23:35:36 +08:00
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
2020-07-28 00:09:48 +08:00
|
|
|
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
2020-05-28 02:34:55 +08:00
|
|
|
* @returns {(string[] | ReferencedExport)[]} referenced exports
|
2018-07-25 15:33:48 +08:00
|
|
|
*/
|
2020-07-28 00:09:48 +08:00
|
|
|
getReferencedExports(moduleGraph, runtime) {
|
2019-10-30 05:28:42 +08:00
|
|
|
return [[this.name]];
|
2017-12-15 22:39:53 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Returns errors
|
2018-07-24 23:35:36 +08:00
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {WebpackError[]} errors
|
|
|
|
*/
|
2018-07-24 23:35:36 +08:00
|
|
|
getErrors(moduleGraph) {
|
2018-07-24 21:30:37 +08:00
|
|
|
const module = moduleGraph.getModule(this);
|
2018-10-12 00:37:32 +08:00
|
|
|
|
2018-04-28 17:09:13 +08:00
|
|
|
if (
|
|
|
|
this.onlyDirectImport &&
|
2018-07-24 21:30:37 +08:00
|
|
|
module &&
|
|
|
|
!module.type.startsWith("webassembly")
|
2018-04-28 17:09:13 +08:00
|
|
|
) {
|
|
|
|
return [
|
|
|
|
new UnsupportedWebAssemblyFeatureError(
|
2019-06-09 17:23:42 +08:00
|
|
|
`Import "${this.name}" from "${this.request}" with ${this.onlyDirectImport} can only be used for direct wasm to wasm dependencies`
|
2018-04-28 17:09:13 +08:00
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectSerializerContext} context context
|
|
|
|
*/
|
2018-10-12 00:37:32 +08:00
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
|
|
|
|
|
|
|
write(this.name);
|
|
|
|
write(this.description);
|
|
|
|
write(this.onlyDirectImport);
|
|
|
|
|
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectDeserializerContext} context context
|
|
|
|
*/
|
2018-10-12 00:37:32 +08:00
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
|
|
|
|
this.name = read();
|
|
|
|
this.description = read();
|
|
|
|
this.onlyDirectImport = read();
|
|
|
|
|
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2017-12-15 22:39:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-12 00:37:32 +08:00
|
|
|
makeSerializable(
|
|
|
|
WebAssemblyImportDependency,
|
|
|
|
"webpack/lib/dependencies/WebAssemblyImportDependency"
|
|
|
|
);
|
|
|
|
|
2017-12-15 22:39:53 +08:00
|
|
|
module.exports = WebAssemblyImportDependency;
|