Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
remove webpack specific change
Browse files Browse the repository at this point in the history
We used to hook directly into webpack internals to rewrite the runtime
that loads Wasm to make it point to the Worker binding instead of a
network fetch.

This change removes the webpack specific change and injects a generic
runtime to load Wasm from the binding.
  • Loading branch information
xtuc committed Jan 14, 2021
1 parent 94ebe72 commit 472faa1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
18 changes: 17 additions & 1 deletion src/wranglerjs/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ impl Bundle {
wasm_file.write_all(&wasm)?;
}

script_file.write_all(
format!(
r#"
WebAssembly.instantiateStreaming =
async function instantiateStreaming(req, importObject) {{
const module = {};
return {{
module,
instance: new WebAssembly.Instance(module, importObject)
}}
}};
"#,
self.get_wasm_binding()
)
.as_bytes(),
)?;
script_file.write_all(wranglerjs_output.script.as_bytes())?;

Ok(())
Expand All @@ -56,7 +72,7 @@ impl Bundle {
}

pub fn get_wasm_binding(&self) -> String {
"wasm".to_string()
"WASM_MODULE".to_string()
}

pub fn script_path(&self) -> PathBuf {
Expand Down
23 changes: 0 additions & 23 deletions wranglerjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,6 @@ function filterByExtension(ext) {
const compiler = webpack(config);
const fullConfig = compiler.options;

// Override the {FetchCompileWasmTemplatePlugin} and inject our new runtime.
const [
fetchCompileWasmTemplatePlugin
] = compiler.hooks.thisCompilation.taps.filter(
tap => tap.name === "FetchCompileWasmTemplatePlugin"
);
fetchCompileWasmTemplatePlugin.fn = function(compilation) {
const mainTemplate = compilation.mainTemplate;
const generateLoadBinaryCode = () => `
// Fake fetch response
Promise.resolve({
arrayBuffer() { return Promise.resolve(${args["wasm-binding"]}); }
});
`;

const plugin = new WasmMainTemplatePlugin({
generateLoadBinaryCode,
mangleImports: false,
supportsStreaming: false
});
plugin.apply(mainTemplate);
};

let lastHash = "";
const compilerCallback = (err, stats) => {
if (err) {
Expand Down

0 comments on commit 472faa1

Please sign in to comment.