Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: return a string when transpiling #283

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ visit = ["swc_ecma_visit", "swc_visit", "swc_visit_macros", "swc_macros_common"]

[dependencies]
base64 = { version = "0.21.6", optional = true }
deno_media_type = "0.1.4"
deno_media_type = "0.2.1"
deno_terminal = "0.1.1"

dprint-swc-ext = "0.20.0"
Expand Down
38 changes: 14 additions & 24 deletions src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::string::FromUtf8Error;

use base64::Engine;
use thiserror::Error;

Expand Down Expand Up @@ -52,24 +50,6 @@ impl Default for EmitOptions {
}
}

/// Source emitted based on the emit options.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
pub struct EmittedSourceBytes {
/// Emitted text as utf8 bytes.
pub source: Vec<u8>,
/// Source map back to the original file.
pub source_map: Option<Vec<u8>>,
}

impl EmittedSourceBytes {
/// Convenience method to convert the emitted source bytes into a string.
pub fn into_string(self) -> Result<EmittedSourceText, FromUtf8Error> {
let text = String::from_utf8(self.source)?;
let source_map = self.source_map.map(String::from_utf8).transpose()?;
Ok(EmittedSourceText { text, source_map })
}
}

/// Source emitted based on the emit options.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
pub struct EmittedSourceText {
Expand All @@ -96,7 +76,7 @@ pub fn emit(
comments: &dyn crate::swc::common::comments::Comments,
source_map: &SourceMap,
emit_options: &EmitOptions,
) -> Result<EmittedSourceBytes, EmitError> {
) -> Result<EmittedSourceText, EmitError> {
let source_map = source_map.inner();
let mut src_map_buf = vec![];
let mut src_buf = vec![];
Expand Down Expand Up @@ -174,9 +154,19 @@ pub fn emit(
}
}

Ok(EmittedSourceBytes {
source: src_buf,
source_map: map,
debug_assert!(std::str::from_utf8(&src_buf).is_ok(), "valid utf-8");
if let Some(map) = &map {
debug_assert!(std::str::from_utf8(map).is_ok(), "valid utf-8");
}

// It's better to return a string here because then we can pass this to deno_core/v8
// as a known string, so it doesn't need to spend any time analyzing it.
Ok(EmittedSourceText {
// SAFETY: swc appends UTF-8 bytes to the JsWriter, so we can safely assume
// that the final string is UTF-8 (unchecked for performance reasons)
text: unsafe { String::from_utf8_unchecked(src_buf) },
// SAFETY: see above comment
source_map: map.map(|b| unsafe { String::from_utf8_unchecked(b) }),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ pub fn get_syntax(media_type: MediaType) -> Syntax {
| MediaType::Jsx
| MediaType::Json
| MediaType::Wasm
| MediaType::TsBuildInfo
| MediaType::SourceMap
| MediaType::Css
| MediaType::Unknown => Syntax::Es(EsSyntax {
allow_return_outside_function: true,
allow_super_outside_method: true,
Expand Down
2 changes: 0 additions & 2 deletions src/transpiling/jsx_precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3049,8 +3049,6 @@ const a = _jsxTemplate($$_tpl_1, _jsxAttr("class", "foo"), _jsxAttr("className",
},
)
.unwrap()
.into_string()
.unwrap()
.text
}
}
Loading