Skip to content

Commit

Permalink
Fix deprecation warning for wasm-bindgen >= 0.2.93
Browse files Browse the repository at this point in the history
  • Loading branch information
yescallop committed Nov 6, 2024
1 parent 84a1e6b commit c042c0c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
11 changes: 3 additions & 8 deletions src/pipelines/rust/initializer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
async function __trunkInitializer(init, source, sourceSize, initializer) {
async function __trunkInitializer(init, source, sourceSize, initializer, passObjectToInit) {
if (initializer === undefined) {
return await init(source);
return await init(passObjectToInit ? { module_or_path: source } : source);
}

return await __trunkInitWithProgress(init, source, sourceSize, initializer);
}

async function __trunkInitWithProgress(init, source, sourceSize, initializer) {

const {
onStart, onProgress, onComplete, onSuccess, onFailure
} = initializer;
Expand Down Expand Up @@ -55,7 +50,7 @@ async function __trunkInitWithProgress(init, source, sourceSize, initializer) {
new Response(stream, init),
);

return init(response)
return init(passObjectToInit ? { module_or_path: response } : response)
.then((value) => {
onComplete?.();
onSuccess?.(value);
Expand Down
8 changes: 6 additions & 2 deletions src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use anyhow::{anyhow, bail, ensure, Context, Result};
use cargo_metadata::Artifact;
use minify_js::TopLevelMode;
use seahash::SeaHasher;
use semver::Version;
use std::{
collections::HashSet,
hash::Hasher,
Expand Down Expand Up @@ -523,13 +524,15 @@ impl RustApp {
#[tracing::instrument(level = "trace", skip(self))]
async fn wasm_bindgen_build(&mut self, wasm_path: &Path) -> Result<RustAppOutput> {
let version = find_wasm_bindgen_version(&self.cfg.tools, &self.manifest);
let wasm_bindgen = tools::get(
let (wasm_bindgen, version) = tools::get(
Application::WasmBindgen,
version.as_deref(),
self.cfg.offline,
&self.cfg.client_options(),
)
.await?;
let wasm_bindgen_version =
Version::parse(&version).context("error parsing wasm-bindgen version")?;

// Ensure our output dir is in place.
let wasm_bindgen_name = Application::WasmBindgen.name();
Expand Down Expand Up @@ -747,6 +750,7 @@ impl RustApp {
import_bindings: self.import_bindings,
import_bindings_name: self.import_bindings_name.clone(),
initializer,
wasm_bindgen_version,
})
}

Expand Down Expand Up @@ -875,7 +879,7 @@ impl RustApp {
}

let version = self.cfg.tools.wasm_opt.as_deref();
let wasm_opt = tools::get(
let (wasm_opt, _version) = tools::get(
Application::WasmOpt,
version,
self.cfg.offline,
Expand Down
27 changes: 24 additions & 3 deletions src/pipelines/rust/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
pipelines::rust::{sri::SriBuilder, RustAppType},
};
use anyhow::bail;
use semver::{Comparator, Op, Prerelease, Version};
use std::{collections::HashMap, sync::Arc};

/// The output of a cargo build pipeline.
Expand All @@ -31,6 +32,8 @@ pub struct RustAppOutput {
pub import_bindings_name: Option<String>,
/// The target of the initializer module
pub initializer: Option<String>,
/// The version of wasm-bindgen used
pub wasm_bindgen_version: Version,
}

pub fn pattern_evaluate(template: &str, params: &HashMap<String, String>) -> String {
Expand Down Expand Up @@ -133,16 +136,34 @@ window.{bindings} = bindings;
dispatchEvent(new CustomEvent("TrunkApplicationStarted", {detail: {wasm}}));
"#;

// In wasm-bindgen 0.2.93, parameters to `init` were deprecated in favor of an object
// (see https://github.com/rustwasm/wasm-bindgen/pull/3995). For versions >= 0.2.93,
// passing a `string` or `Promise<Response>` to `init` causes a deprecation warning,
// so we wrap it in an object and pass the object instead.
let pass_object_to_init = Comparator {
op: Op::GreaterEq,
major: 0,
minor: Some(2),
patch: Some(93),
pre: Prerelease::EMPTY,
}
.matches(&self.wasm_bindgen_version);

match &self.initializer {
None => format!(
r#"
<script type="module" nonce="{nonce}">
import init{import} from '{base}{js}';
const wasm = await init('{base}{wasm}');
const wasm = await init({init_arg});
{bind}
{fire}
</script>"#
</script>"#,
init_arg = if pass_object_to_init {
format!("{{ module_or_path: '{base}{wasm}' }}")
} else {
format!("'{base}{wasm}'")
}
),
Some(initializer) => format!(
r#"
Expand All @@ -152,7 +173,7 @@ const wasm = await init('{base}{wasm}');
import init{import} from '{base}{js}';
import initializer from '{base}{initializer}';
const wasm = await __trunkInitializer(init, '{base}{wasm}', {size}, initializer());
const wasm = await __trunkInitializer(init, '{base}{wasm}', {size}, initializer(), {pass_object_to_init});
{bind}
{fire}
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/sass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Sass {
#[tracing::instrument(level = "trace", skip(self))]
async fn run(self) -> Result<TrunkAssetPipelineOutput> {
let version = self.cfg.tools.sass.as_deref();
let sass = tools::get(
let (sass, _version) = tools::get(
Application::Sass,
version,
self.cfg.offline,
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/tailwind_css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl TailwindCss {
#[tracing::instrument(level = "trace", skip(self))]
async fn run(self) -> Result<TrunkAssetPipelineOutput> {
let version = self.cfg.tools.tailwindcss.as_deref();
let tailwind = tools::get(
let (tailwind, _version) = tools::get(
Application::TailwindCss,
version,
self.cfg.offline,
Expand Down
8 changes: 4 additions & 4 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub async fn get(
version: Option<&str>,
offline: bool,
client_options: &HttpClientOptions,
) -> Result<PathBuf> {
) -> Result<(PathBuf, String)> {
tracing::debug!("Getting tool");

if let Some((path, detected_version)) = find_system(app).await {
Expand All @@ -264,7 +264,7 @@ pub async fn get(
if required_version == detected_version {
// and a match, so return early
tracing::debug!(%detected_version, "using system installed binary: {}", path.display());
return Ok(path);
return Ok((path, detected_version));
} else if offline {
// a mismatch, in offline mode, we can't help here
bail!(
Expand All @@ -277,7 +277,7 @@ pub async fn get(
}
} else {
// we don't require any specific version
return Ok(path);
return Ok((path, detected_version));
}
}

Expand Down Expand Up @@ -308,7 +308,7 @@ pub async fn get(
bin_path.display()
);

Ok(bin_path)
Ok((bin_path, version.to_owned()))
}

/// Try to find a global system installed version of the application.
Expand Down

0 comments on commit c042c0c

Please sign in to comment.