From c042c0c979508e05049aea1453dc5fff1b0c47e9 Mon Sep 17 00:00:00 2001 From: Scallop Ye Date: Thu, 7 Nov 2024 00:47:11 +0800 Subject: [PATCH] Fix deprecation warning for wasm-bindgen >= 0.2.93 --- src/pipelines/rust/initializer.js | 11 +++-------- src/pipelines/rust/mod.rs | 8 ++++++-- src/pipelines/rust/output.rs | 27 ++++++++++++++++++++++++--- src/pipelines/sass.rs | 2 +- src/pipelines/tailwind_css.rs | 2 +- src/tools.rs | 8 ++++---- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/pipelines/rust/initializer.js b/src/pipelines/rust/initializer.js index 86dc7f89..3c80eab0 100644 --- a/src/pipelines/rust/initializer.js +++ b/src/pipelines/rust/initializer.js @@ -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; @@ -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); diff --git a/src/pipelines/rust/mod.rs b/src/pipelines/rust/mod.rs index 6c88c712..305094c0 100644 --- a/src/pipelines/rust/mod.rs +++ b/src/pipelines/rust/mod.rs @@ -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, @@ -523,13 +524,15 @@ impl RustApp { #[tracing::instrument(level = "trace", skip(self))] async fn wasm_bindgen_build(&mut self, wasm_path: &Path) -> Result { 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(); @@ -747,6 +750,7 @@ impl RustApp { import_bindings: self.import_bindings, import_bindings_name: self.import_bindings_name.clone(), initializer, + wasm_bindgen_version, }) } @@ -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, diff --git a/src/pipelines/rust/output.rs b/src/pipelines/rust/output.rs index 5fe9b58d..f2cb4670 100644 --- a/src/pipelines/rust/output.rs +++ b/src/pipelines/rust/output.rs @@ -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. @@ -31,6 +32,8 @@ pub struct RustAppOutput { pub import_bindings_name: Option, /// The target of the initializer module pub initializer: Option, + /// The version of wasm-bindgen used + pub wasm_bindgen_version: Version, } pub fn pattern_evaluate(template: &str, params: &HashMap) -> String { @@ -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` 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#" "# +"#, + init_arg = if pass_object_to_init { + format!("{{ module_or_path: '{base}{wasm}' }}") + } else { + format!("'{base}{wasm}'") + } ), Some(initializer) => format!( r#" @@ -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} diff --git a/src/pipelines/sass.rs b/src/pipelines/sass.rs index 8fb8ea9e..b8871043 100644 --- a/src/pipelines/sass.rs +++ b/src/pipelines/sass.rs @@ -79,7 +79,7 @@ impl Sass { #[tracing::instrument(level = "trace", skip(self))] async fn run(self) -> Result { let version = self.cfg.tools.sass.as_deref(); - let sass = tools::get( + let (sass, _version) = tools::get( Application::Sass, version, self.cfg.offline, diff --git a/src/pipelines/tailwind_css.rs b/src/pipelines/tailwind_css.rs index c8c0340f..114866e9 100644 --- a/src/pipelines/tailwind_css.rs +++ b/src/pipelines/tailwind_css.rs @@ -78,7 +78,7 @@ impl TailwindCss { #[tracing::instrument(level = "trace", skip(self))] async fn run(self) -> Result { let version = self.cfg.tools.tailwindcss.as_deref(); - let tailwind = tools::get( + let (tailwind, _version) = tools::get( Application::TailwindCss, version, self.cfg.offline, diff --git a/src/tools.rs b/src/tools.rs index 0974148b..f02c3263 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -253,7 +253,7 @@ pub async fn get( version: Option<&str>, offline: bool, client_options: &HttpClientOptions, -) -> Result { +) -> Result<(PathBuf, String)> { tracing::debug!("Getting tool"); if let Some((path, detected_version)) = find_system(app).await { @@ -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!( @@ -277,7 +277,7 @@ pub async fn get( } } else { // we don't require any specific version - return Ok(path); + return Ok((path, detected_version)); } } @@ -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.