Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/rspack_plugin_wasm/src/parser_and_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
borrow::Cow,
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
path::Path,
};

use indexmap::IndexMap;
Expand Down Expand Up @@ -261,10 +262,16 @@ impl ParserAndGenerator for AsyncWasmParserAndGenerator {
None
};

let origin_filename = Path::new(&normal_module.resource_resolved_data().resource)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("");

let instantiate_call = format!(
"{}(exports, module.id, {} {})",
"{}(exports, module.id, {}, {} {})",
RuntimeGlobals::INSTANTIATE_WASM,
serde_json::to_string(&hash).expect("should be ok"),
serde_json::to_string(origin_filename).expect("should be ok"),
imports_obj.unwrap_or_default()
);

Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_plugin_wasm/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl RuntimeModule for AsyncWasmLoadingRuntimeModule {
.hash(&hash)
.content_hash(&hash)
.id(&PathData::prepare_id("\" + wasmModuleId + \""))
.filename("\" + wasmModuleName + \"")
.runtime(chunk.runtime().as_str()),
)
.await?;
Expand Down Expand Up @@ -109,7 +110,7 @@ fn get_async_wasm_loading(req: &str, supports_streaming: bool) -> String {
if supports_streaming {
format!(
r#"
__webpack_require__.v = function(exports, wasmModuleId, wasmModuleHash, importsObj) {{
__webpack_require__.v = function(exports, wasmModuleId, wasmModuleHash, wasmModuleName, importsObj) {{
var req = {req};
var fallback = function() {{
return req{fallback_code}
Expand All @@ -122,7 +123,7 @@ fn get_async_wasm_loading(req: &str, supports_streaming: bool) -> String {
let req = req.trim_end_matches(';');
format!(
r#"
__webpack_require__.v = function(exports, wasmModuleId, wasmModuleHash, importsObj) {{
__webpack_require__.v = function(exports, wasmModuleId, wasmModuleHash, wasmModuleName, importsObj) {{
return {req}{fallback_code}
}};
"#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
```js title=main.js
"use strict";
(self["webpackChunkwebpack"] = self["webpackChunkwebpack"] || []).push([["main"], {
"./index.js": (function (module, __webpack_exports__, __webpack_require__) {
__webpack_require__.a(module, async function (__webpack_handle_async_dependencies__, __webpack_async_result__) { try {
__webpack_require__.r(__webpack_exports__);
/* ESM import */var _factorial_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./factorial.wasm");
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([_factorial_wasm__WEBPACK_IMPORTED_MODULE_0__]);
_factorial_wasm__WEBPACK_IMPORTED_MODULE_0__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];

const factorial = _factorial_wasm__WEBPACK_IMPORTED_MODULE_0__._Z4facti;

document.querySelector("#root").innerHTML = factorial(3);

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

}),
"./factorial.wasm": (function (module, exports, __webpack_require__) {
module.exports = __webpack_require__.v(exports, module.id, "4f244726a350135b", "factorial" );

}),

},function(__webpack_require__) {
var __webpack_exec__ = function(moduleId) { return __webpack_require__(__webpack_require__.s = moduleId) }
var __webpack_exports__ = (__webpack_exec__("./index.js"));

}
]);
```
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { _Z4facti } from "./factorial.wasm";

const factorial = _Z4facti;

document.querySelector("#root").innerHTML = factorial(3);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
context: __dirname,
entry: {
main: "./index.js"
},
output: {
webassemblyModuleFilename: "[name].wasm"
},
experiments: {
asyncWebAssembly: true
}
};
Loading