Fix javascript asset bundling errors and fallback (#3935)

This commit is contained in:
Evan Almloff authored and GitHub committed 2025-04-15 14:38:11 -07:00
1 parent 487fa25dc8
commit bcc1dc020c
1 file changed
+11 -6
+11 -6
View File
@@ -103,8 +103,10 @@ fn bundle_js_to_writer_inside_handler(
parser.parse_module().map_err(|err| { parser.parse_module().map_err(|err| {
HANDLER.with(|handler| { HANDLER.with(|handler| {
let message = err.into_diagnostic(handler).message(); let mut error = err.into_diagnostic(handler);
anyhow::anyhow!("{}", message) // swc errors panic on drop if you don't cancel them
error.cancel();
anyhow::anyhow!("{}", error.message())
}) })
})? })?
}; };
@@ -165,8 +167,10 @@ impl Load for PathLoader {
&mut Vec::new(), &mut Vec::new(),
) )
.map_err(|err| { .map_err(|err| {
let message = err.into_diagnostic(handler).message(); let mut error = err.into_diagnostic(handler);
anyhow::anyhow!("{}", message) // swc errors panic on drop if you don't cancel them
error.cancel();
anyhow::anyhow!("{}", error.message())
}) })
.context("Failed to parse javascript") .context("Failed to parse javascript")
})?; })?;
@@ -228,8 +232,10 @@ pub(crate) fn process_js(
if js_options.minified() { if js_options.minified() {
if let Err(err) = bundle_js_to_writer(source.to_path_buf(), bundle, true, &mut writer) { if let Err(err) = bundle_js_to_writer(source.to_path_buf(), bundle, true, &mut writer) {
tracing::error!("Failed to minify js. Falling back to non-minified: {err}"); tracing::error!("Failed to minify js. Falling back to non-minified: {err}");
}
} else { } else {
return Ok(());
}
}
let mut source_file = std::fs::File::open(source)?; let mut source_file = std::fs::File::open(source)?;
std::io::copy(&mut source_file, &mut writer).with_context(|| { std::io::copy(&mut source_file, &mut writer).with_context(|| {
format!( format!(
@@ -237,7 +243,6 @@ pub(crate) fn process_js(
output_path.display() output_path.display()
) )
})?; })?;
}
Ok(()) Ok(())
} }