@@ -10,7 +10,7 @@ use crate::{
1010 helpers:: { get_map, GeneratedInfo , OnChunk , OnName , OnSource , StreamChunks } ,
1111 linear_map:: LinearMap ,
1212 source:: { Mapping , OriginalLocation } ,
13- BoxSource , MapOptions , Rope , Source , SourceExt , SourceMap ,
13+ BoxSource , MapOptions , Rope , Source , SourceExt , SourceMap , SourceValue ,
1414} ;
1515
1616/// Concatenate multiple [Source]s to a single [Source].
@@ -19,12 +19,12 @@ use crate::{
1919///
2020/// ```
2121/// use rspack_sources::{
22- /// BoxSource, ConcatSource, MapOptions, OriginalSource, RawSource , Source,
22+ /// BoxSource, ConcatSource, MapOptions, OriginalSource, RawStringSource , Source,
2323/// SourceExt, SourceMap,
2424/// };
2525///
2626/// let mut source = ConcatSource::new([
27- /// RawSource ::from("Hello World\n".to_string()).boxed(),
27+ /// RawStringSource ::from("Hello World\n".to_string()).boxed(),
2828/// OriginalSource::new(
2929/// "console.log('test');\nconsole.log('test2');\n",
3030/// "console.js",
@@ -35,7 +35,7 @@ use crate::{
3535///
3636/// assert_eq!(source.size(), 62);
3737/// assert_eq!(
38- /// source.source(),
38+ /// source.source().into_string_lossy() ,
3939/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
4040/// );
4141/// assert_eq!(
@@ -117,13 +117,16 @@ impl ConcatSource {
117117}
118118
119119impl Source for ConcatSource {
120- fn source ( & self ) -> Cow < str > {
120+ fn source ( & self ) -> SourceValue {
121121 let children = self . children ( ) ;
122122 if children. len ( ) == 1 {
123123 children[ 0 ] . source ( )
124124 } else {
125- let all = self . children ( ) . iter ( ) . map ( |child| child. source ( ) ) . collect ( ) ;
126- Cow :: Owned ( all)
125+ let mut content = String :: new ( ) ;
126+ for child in self . children ( ) {
127+ content. push_str ( child. source ( ) . into_string_lossy ( ) . as_ref ( ) ) ;
128+ }
129+ SourceValue :: String ( Cow :: Owned ( content) )
127130 }
128131 }
129132
@@ -355,14 +358,14 @@ impl StreamChunks for ConcatSource {
355358
356359#[ cfg( test) ]
357360mod tests {
358- use crate :: { OriginalSource , RawBufferSource , RawSource , RawStringSource } ;
361+ use crate :: { OriginalSource , RawBufferSource , RawStringSource } ;
359362
360363 use super :: * ;
361364
362365 #[ test]
363366 fn should_concat_two_sources ( ) {
364367 let mut source = ConcatSource :: new ( [
365- RawSource :: from ( "Hello World\n " . to_string ( ) ) . boxed ( ) ,
368+ RawStringSource :: from ( "Hello World\n " . to_string ( ) ) . boxed ( ) ,
366369 OriginalSource :: new (
367370 "console.log('test');\n console.log('test2');\n " ,
368371 "console.js" ,
@@ -374,7 +377,7 @@ mod tests {
374377 let expected_source =
375378 "Hello World\n console.log('test');\n console.log('test2');\n Hello2\n " ;
376379 assert_eq ! ( source. size( ) , 62 ) ;
377- assert_eq ! ( source. source( ) , expected_source) ;
380+ assert_eq ! ( source. source( ) . into_string_lossy ( ) , expected_source) ;
378381 assert_eq ! (
379382 source. map( & MapOptions :: new( false ) ) . unwrap( ) ,
380383 SourceMap :: from_json(
@@ -424,7 +427,7 @@ mod tests {
424427 let expected_source =
425428 "Hello World\n console.log('test');\n console.log('test2');\n Hello2\n " ;
426429 assert_eq ! ( source. size( ) , 62 ) ;
427- assert_eq ! ( source. source( ) , expected_source) ;
430+ assert_eq ! ( source. source( ) . into_string_lossy ( ) , expected_source) ;
428431 assert_eq ! (
429432 source. map( & MapOptions :: new( false ) ) . unwrap( ) ,
430433 SourceMap :: from_json(
@@ -474,7 +477,7 @@ mod tests {
474477 let expected_source =
475478 "Hello World\n console.log('test');\n console.log('test2');\n Hello2\n " ;
476479 assert_eq ! ( source. size( ) , 62 ) ;
477- assert_eq ! ( source. source( ) , expected_source) ;
480+ assert_eq ! ( source. source( ) . into_string_lossy ( ) , expected_source) ;
478481 assert_eq ! (
479482 source. map( & MapOptions :: new( false ) ) . unwrap( ) ,
480483 SourceMap :: from_json(
@@ -512,18 +515,21 @@ mod tests {
512515 #[ test]
513516 fn should_be_able_to_handle_strings_for_all_methods ( ) {
514517 let mut source = ConcatSource :: new ( [
515- RawSource :: from ( "Hello World\n " . to_string ( ) ) . boxed ( ) ,
518+ RawStringSource :: from ( "Hello World\n " . to_string ( ) ) . boxed ( ) ,
516519 OriginalSource :: new (
517520 "console.log('test');\n console.log('test2');\n " ,
518521 "console.js" ,
519522 )
520523 . boxed ( ) ,
521524 ] ) ;
522- let inner_source =
523- ConcatSource :: new ( [ RawSource :: from ( "(" ) , "'string'" . into ( ) , ")" . into ( ) ] ) ;
524- source. add ( RawSource :: from ( "console" ) ) ;
525- source. add ( RawSource :: from ( "." ) ) ;
526- source. add ( RawSource :: from ( "log" ) ) ;
525+ let inner_source = ConcatSource :: new ( [
526+ RawStringSource :: from ( "(" ) ,
527+ "'string'" . into ( ) ,
528+ ")" . into ( ) ,
529+ ] ) ;
530+ source. add ( RawStringSource :: from ( "console" ) ) ;
531+ source. add ( RawStringSource :: from ( "." ) ) ;
532+ source. add ( RawStringSource :: from ( "log" ) ) ;
527533 source. add ( inner_source) ;
528534 let expected_source =
529535 "Hello World\n console.log('test');\n console.log('test2');\n console.log('string')" ;
@@ -538,7 +544,7 @@ mod tests {
538544 )
539545 . unwrap ( ) ;
540546 assert_eq ! ( source. size( ) , 76 ) ;
541- assert_eq ! ( source. source( ) , expected_source) ;
547+ assert_eq ! ( source. source( ) . into_string_lossy ( ) , expected_source) ;
542548 assert_eq ! ( source. buffer( ) , expected_source. as_bytes( ) ) ;
543549
544550 let map = source. map ( & MapOptions :: new ( false ) ) . unwrap ( ) ;
@@ -550,16 +556,19 @@ mod tests {
550556 #[ test]
551557 fn should_return_null_as_map_when_only_generated_code_is_concatenated ( ) {
552558 let source = ConcatSource :: new ( [
553- RawSource :: from ( "Hello World\n " ) ,
554- RawSource :: from ( "Hello World\n " . to_string ( ) ) ,
555- RawSource :: from ( "" ) ,
559+ RawStringSource :: from ( "Hello World\n " ) ,
560+ RawStringSource :: from ( "Hello World\n " . to_string ( ) ) ,
561+ RawStringSource :: from ( "" ) ,
556562 ] ) ;
557563
558564 let result_text = source. source ( ) ;
559565 let result_map = source. map ( & MapOptions :: default ( ) ) ;
560566 let result_list_map = source. map ( & MapOptions :: new ( false ) ) ;
561567
562- assert_eq ! ( result_text, "Hello World\n Hello World\n " ) ;
568+ assert_eq ! (
569+ result_text. into_string_lossy( ) ,
570+ "Hello World\n Hello World\n "
571+ ) ;
563572 assert ! ( result_map. is_none( ) ) ;
564573 assert ! ( result_list_map. is_none( ) ) ;
565574 }
@@ -568,13 +577,13 @@ mod tests {
568577 fn should_allow_to_concatenate_in_a_single_line ( ) {
569578 let source = ConcatSource :: new ( [
570579 OriginalSource :: new ( "Hello" , "hello.txt" ) . boxed ( ) ,
571- RawSource :: from ( " " ) . boxed ( ) ,
580+ RawStringSource :: from ( " " ) . boxed ( ) ,
572581 OriginalSource :: new ( "World " , "world.txt" ) . boxed ( ) ,
573- RawSource :: from ( "is here\n " ) . boxed ( ) ,
582+ RawStringSource :: from ( "is here\n " ) . boxed ( ) ,
574583 OriginalSource :: new ( "Hello\n " , "hello.txt" ) . boxed ( ) ,
575- RawSource :: from ( " \n " ) . boxed ( ) ,
584+ RawStringSource :: from ( " \n " ) . boxed ( ) ,
576585 OriginalSource :: new ( "World\n " , "world.txt" ) . boxed ( ) ,
577- RawSource :: from ( "is here" ) . boxed ( ) ,
586+ RawStringSource :: from ( "is here" ) . boxed ( ) ,
578587 ] ) ;
579588
580589 assert_eq ! (
@@ -591,36 +600,44 @@ mod tests {
591600 . unwrap( ) ,
592601 ) ;
593602 assert_eq ! (
594- source. source( ) ,
603+ source. source( ) . into_string_lossy ( ) ,
595604 "Hello World is here\n Hello\n \n World\n is here" ,
596605 ) ;
597606 }
598607
599608 #[ test]
600609 fn should_allow_to_concat_buffer_sources ( ) {
601610 let source = ConcatSource :: new ( [
602- RawSource :: from ( "a" ) ,
603- RawSource :: from ( Vec :: from ( "b" ) ) ,
604- RawSource :: from ( "c" ) ,
611+ RawStringSource :: from ( "a" ) ,
612+ RawStringSource :: from ( "b" ) ,
613+ RawStringSource :: from ( "c" ) ,
605614 ] ) ;
606- assert_eq ! ( source. source( ) , "abc" ) ;
615+ assert_eq ! ( source. source( ) . into_string_lossy ( ) , "abc" ) ;
607616 assert ! ( source. map( & MapOptions :: default ( ) ) . is_none( ) ) ;
608617 }
609618
610619 #[ test]
611620 fn should_flatten_nested_concat_sources ( ) {
612- let inner_concat =
613- ConcatSource :: new ( [ RawSource :: from ( "Hello " ) , RawSource :: from ( "World" ) ] ) ;
621+ let inner_concat = ConcatSource :: new ( [
622+ RawStringSource :: from ( "Hello " ) ,
623+ RawStringSource :: from ( "World" ) ,
624+ ] ) ;
614625
615626 let outer_concat = ConcatSource :: new ( [
616627 inner_concat. boxed ( ) ,
617- RawSource :: from ( "!" ) . boxed ( ) ,
618- ConcatSource :: new ( [ RawSource :: from ( " How" ) , RawSource :: from ( " are" ) ] )
619- . boxed ( ) ,
620- RawSource :: from ( " you?" ) . boxed ( ) ,
628+ RawStringSource :: from ( "!" ) . boxed ( ) ,
629+ ConcatSource :: new ( [
630+ RawStringSource :: from ( " How" ) ,
631+ RawStringSource :: from ( " are" ) ,
632+ ] )
633+ . boxed ( ) ,
634+ RawStringSource :: from ( " you?" ) . boxed ( ) ,
621635 ] ) ;
622636
623- assert_eq ! ( outer_concat. source( ) , "Hello World! How are you?" ) ;
637+ assert_eq ! (
638+ outer_concat. source( ) . into_string_lossy( ) ,
639+ "Hello World! How are you?"
640+ ) ;
624641 // The key test: verify that nested ConcatSources are flattened
625642 // Should have 6 direct children instead of nested structure
626643 assert_eq ! ( outer_concat. children. len( ) , 6 ) ;
0 commit comments