@@ -12,8 +12,8 @@ use crate::{
1212 linear_map:: LinearMap ,
1313 object_pool:: ObjectPool ,
1414 source_content_lines:: SourceContentLines ,
15- BoxSource , MapOptions , Mapping , OriginalLocation , Source , SourceExt ,
16- SourceMap , SourceValue ,
15+ BoxSource , MapOptions , Mapping , OriginalLocation , OriginalSource , Source ,
16+ SourceExt , SourceMap , SourceValue ,
1717} ;
1818
1919/// Decorates a Source with replacements and insertions of source code,
@@ -244,6 +244,10 @@ impl Source for ReplaceSource {
244244 get_map ( & ObjectPool :: default ( ) , chunks. as_ref ( ) , options)
245245 }
246246
247+ fn write_to_string ( & self , string : & mut String ) {
248+ string. push_str ( & self . source ( ) . into_string_lossy ( ) ) ;
249+ }
250+
247251 fn to_writer ( & self , writer : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
248252 writer. write_all ( self . source ( ) . as_bytes ( ) )
249253 }
@@ -312,13 +316,17 @@ fn check_content_at_position(
312316}
313317
314318struct ReplaceSourceChunks < ' a > {
319+ is_original_source : bool ,
315320 chunks : Box < dyn Chunks + ' a > ,
316321 replacements : & ' a [ Replacement ] ,
317322}
318323
319324impl < ' a > ReplaceSourceChunks < ' a > {
320325 pub fn new ( source : & ' a ReplaceSource ) -> Self {
326+ let is_original_source =
327+ source. inner . as_ref ( ) . as_any ( ) . is :: < OriginalSource > ( ) ;
321328 Self {
329+ is_original_source,
322330 chunks : source. inner . stream_chunks ( ) ,
323331 replacements : & source. replacements ,
324332 }
@@ -378,6 +386,13 @@ impl Chunks for ReplaceSourceChunks<'_> {
378386 // webpack-sources also have this function, refer https://github.com/webpack/webpack-sources/blob/main/lib/ReplaceSource.js#L158
379387 let check_original_content =
380388 |source_index : u32 , line : u32 , column : u32 , expected_chunk : & str | {
389+ // Performance optimization: Skip content validation for OriginalSourceChunks.
390+ // Since OriginalSourceChunks guarantees that the source content matches the actual source,
391+ // we can safely bypass the expensive content checking process.
392+ if self . is_original_source {
393+ return true ;
394+ }
395+
381396 if let Some ( Some ( source_content) ) =
382397 source_content_lines. borrow_mut ( ) . get_mut ( & source_index)
383398 {
0 commit comments