Skip to content

Commit

Permalink
Don't force TextDe/Encoder (#3329)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Feb 27, 2023
1 parent c92104c commit 78421dc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
41 changes: 33 additions & 8 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,26 +1434,35 @@ 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> {
if !self.should_write_global("text_decoder") {
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 {
Expand Down Expand Up @@ -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(())
}

Expand Down
7 changes: 2 additions & 5 deletions examples/wasm-audio-worklet/src/dependent_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ extern "C" {
}

pub fn on_the_fly(code: &str) -> Result<String, JsValue> {
// 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(),
);

Expand Down
23 changes: 0 additions & 23 deletions examples/wasm-audio-worklet/src/polyfill.js

This file was deleted.

0 comments on commit 78421dc

Please sign in to comment.