feat(sourcemap): support SourceMapBuilder#token_chunks (#4220)

Because the `token_chunks` need to pre-visit tokens and collect, it
could be done at add tokens phase. So here export it let rolldown could
be improve `renderChunks` sourcemap encode.
This commit is contained in:
underfin 2024-07-13 10:11:14 +08:00 committed by GitHub
parent c65198fa15
commit 205c259119
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

View file

@ -14,4 +14,4 @@ pub use error::Error;
pub use sourcemap::SourceMap;
pub use sourcemap_builder::SourceMapBuilder;
pub use sourcemap_visualizer::SourcemapVisualizer;
pub use token::{SourceViewToken, Token};
pub use token::{SourceViewToken, Token, TokenChunk};

View file

@ -2,7 +2,10 @@ use std::sync::Arc;
use rustc_hash::FxHashMap;
use crate::{token::Token, SourceMap};
use crate::{
token::{Token, TokenChunk},
SourceMap,
};
/// The `SourceMapBuilder` is a helper to generate sourcemap.
#[derive(Debug, Default)]
@ -14,6 +17,7 @@ pub struct SourceMapBuilder {
pub(crate) sources_map: FxHashMap<Arc<str>, u32>,
pub(crate) source_contents: Vec<Arc<str>>,
pub(crate) tokens: Vec<Token>,
pub(crate) token_chunks: Option<Vec<TokenChunk>>,
}
#[allow(clippy::cast_possible_truncation)]
@ -66,6 +70,11 @@ impl SourceMapBuilder {
self.file = Some(file.into());
}
/// Set the `SourceMap::token_chunks` to make the sourcemap to vlq mapping at parallel.
pub fn set_token_chunks(&mut self, token_chunks: Vec<TokenChunk>) {
self.token_chunks = Some(token_chunks);
}
pub fn into_sourcemap(self) -> SourceMap {
SourceMap::new(
self.file,
@ -74,7 +83,7 @@ impl SourceMapBuilder {
self.sources,
Some(self.source_contents),
self.tokens,
None,
self.token_chunks,
)
}
}