diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 1a1de40e909..b53997c1b1a 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -1434,7 +1434,7 @@ impl<'a> Context<'a> { if !self.should_write_global("text_encoder") { return Ok(()); } - self.expose_text_processor("TextEncoder", "('utf-8')") + self.expose_text_processor("TextEncoder", "('utf-8')", None) } fn expose_text_decoder(&mut self) -> Result<(), Error> { @@ -1442,18 +1442,27 @@ impl<'a> Context<'a> { return Ok(()); } - // `ignoreBOM` is needed so that the BOM will be preserved when sending a string from Rust to JS - // `fatal` is needed to catch any weird encoding bugs when sending a string from Rust to JS - self.expose_text_processor("TextDecoder", "('utf-8', { ignoreBOM: true, fatal: true })")?; - // This is needed to workaround a bug in Safari // See: https://github.com/rustwasm/wasm-bindgen/issues/1825 - self.global("cachedTextDecoder.decode();"); + let init = Some("cachedTextDecoder.decode();"); + + // `ignoreBOM` is needed so that the BOM will be preserved when sending a string from Rust to JS + // `fatal` is needed to catch any weird encoding bugs when sending a string from Rust to JS + self.expose_text_processor( + "TextDecoder", + "('utf-8', { ignoreBOM: true, fatal: true })", + init, + )?; Ok(()) } - fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> { + fn expose_text_processor( + &mut self, + s: &str, + args: &str, + init: Option<&str>, + ) -> Result<(), Error> { match &self.config.mode { OutputMode::Node { .. } => { let name = self.import_name(&JsImport { @@ -1481,10 +1490,26 @@ impl<'a> Context<'a> { | OutputMode::Web | OutputMode::NoModules { .. } | OutputMode::Bundler { browser_only: true } => { - self.global(&format!("const cached{0} = new {0}{1};", s, args)) + self.global(&format!("const cached{0} = (typeof {0} !== 'undefined' ? new {0}{1} : {{ decode: () => {{ throw Error('{0} not available') }} }} );", s, args)) } }; + if let Some(init) = init { + match &self.config.mode { + OutputMode::Node { .. } + | OutputMode::Bundler { + browser_only: false, + } => self.global(init), + OutputMode::Deno + | OutputMode::Web + | OutputMode::NoModules { .. } + | OutputMode::Bundler { browser_only: true } => self.global(&format!( + "if (typeof {} !== 'undefined') {{ {} }};", + s, init + )), + } + } + Ok(()) } diff --git a/examples/wasm-audio-worklet/src/dependent_module.rs b/examples/wasm-audio-worklet/src/dependent_module.rs index fa01b0acb23..a1706d46ccf 100644 --- a/examples/wasm-audio-worklet/src/dependent_module.rs +++ b/examples/wasm-audio-worklet/src/dependent_module.rs @@ -17,12 +17,9 @@ extern "C" { } pub fn on_the_fly(code: &str) -> Result { - // Generate the import of the bindgen ES module, assuming `--target web`, - // preluded by the TextEncoder/TextDecoder polyfill needed inside worklets. + // Generate the import of the bindgen ES module, assuming `--target web`. let header = format!( - "import '{}';\n\ - import init, * as bindgen from '{}';\n\n", - wasm_bindgen::link_to!(module = "/src/polyfill.js"), + "import init, * as bindgen from '{}';\n\n", IMPORT_META.url(), ); diff --git a/examples/wasm-audio-worklet/src/polyfill.js b/examples/wasm-audio-worklet/src/polyfill.js deleted file mode 100644 index d4d57e9fa67..00000000000 --- a/examples/wasm-audio-worklet/src/polyfill.js +++ /dev/null @@ -1,23 +0,0 @@ -if (!globalThis.TextDecoder) { - globalThis.TextDecoder = class TextDecoder { - decode(arg) { - if (typeof arg !== 'undefined') { - throw Error('TextDecoder stub called'); - } else { - return ''; - } - } - }; -} - -if (!globalThis.TextEncoder) { - globalThis.TextEncoder = class TextEncoder { - encode(arg) { - if (typeof arg !== 'undefined') { - throw Error('TextEncoder stub called'); - } else { - return new Uint8Array(0); - } - } - }; -}