File size: 2,609 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::fmt::Write;

use anyhow::Result;
use indoc::{formatdoc, writedoc};
use turbo_rcstr::RcStr;
use turbo_tasks::Vc;
use turbo_tasks_fs::File;
use turbopack_core::{asset::AssetContent, source::Source, virtual_source::VirtualSource};
use turbopack_ecmascript::utils::StringifyJs;

use crate::{analysis::analyze, source::WebAssemblySource, wasm_edge_var_name};

/// Create a javascript loader to instantiate the WebAssembly module with the
/// necessary imports and exports to be processed by [turbopack_ecmascript].
#[turbo_tasks::function]
pub(crate) async fn instantiating_loader_source(
    source: Vc<WebAssemblySource>,
) -> Result<Vc<Box<dyn Source>>> {
    let analysis = analyze(source).await?;

    let mut code = String::new();

    let mut imports_obj = "{".to_string();
    for (path, items) in &analysis.imports {
        writeln!(
            code,
            "import {{ {} }} from {};",
            items.join(", "),
            StringifyJs(path)
        )?;

        writeln!(imports_obj, "\n    {}: {{", StringifyJs(path))?;
        for item in items {
            writeln!(imports_obj, "        {}: {},", StringifyJs(item), item)?;
        }
        writeln!(imports_obj, "    }},")?;
    }
    writeln!(imports_obj, "}}")?;

    writeln!(code, "import wasmPath from \"WASM_PATH\";")?;

    writeln!(code)?;

    writedoc!(
        code,
        r#"
            const {{ {exports} }} = await __turbopack_wasm__(wasmPath, () => {edgeVariable}, {imports});

            export {{ {exports} }};
        "#,
        edgeVariable = wasm_edge_var_name(Vc::upcast(source)).await?,
        imports = imports_obj,
        exports = analysis.exports.join(", "),
    )?;

    let code: RcStr = code.into();

    Ok(Vc::upcast(VirtualSource::new(
        source.ident().path().await?.append("_.loader.mjs")?,
        AssetContent::file(File::from(code).into()),
    )))
}

/// Create a javascript loader to compile the WebAssembly module and export it
/// without instantiating.
#[turbo_tasks::function]
pub(crate) async fn compiling_loader_source(
    source: Vc<WebAssemblySource>,
) -> Result<Vc<Box<dyn Source>>> {
    let code: RcStr = formatdoc! {
        r#"
            import wasmPath from "WASM_PATH";

            const mod = await __turbopack_wasm_module__(wasmPath, () => {edgeVariable});

            export default mod;
        "#,
        edgeVariable = wasm_edge_var_name(Vc::upcast(source)).await?
    }
    .into();

    Ok(Vc::upcast(VirtualSource::new(
        source.ident().path().await?.append("_.loader.mjs")?,
        AssetContent::file(File::from(code).into()),
    )))
}