@@ -13,8 +13,10 @@ use super::{BundleConfig, BundleEntry, BundleMode, BundleResult};
1313/// Builds bundles from input files
1414pub struct BundleBuilder {
1515 config : BundleConfig ,
16+
1617 /// Cached module graph
1718 module_graph : HashMap < String , Vec < String > > ,
19+
1820 /// Processed files
1921 processed : Vec < String > ,
2022}
@@ -36,8 +38,11 @@ impl BundleBuilder {
3638
3739 match self . config . mode {
3840 BundleMode :: SingleFile => self . build_single_file ( ) ,
41+
3942 BundleMode :: Bundle => self . build_bundle ( ) ,
43+
4044 BundleMode :: Watch => self . build_watch ( ) ,
45+
4146 BundleMode :: Esbuild => self . build_with_esbuild ( ) ,
4247 }
4348 }
@@ -65,6 +70,7 @@ impl BundleBuilder {
6570 . replace ( ".ts" , ".js" ) ;
6671
6772 let output_path = Path :: new ( & self . config . output_dir ) . join ( & output_filename) ;
73+
6874 std:: fs:: write ( & output_path, & output) ?;
6975
7076 bundled_files. push ( output_path. to_string_lossy ( ) . to_string ( ) ) ;
@@ -81,6 +87,7 @@ impl BundleBuilder {
8187 /// Build a bundle from multiple files
8288 fn build_bundle ( & mut self ) -> anyhow:: Result < BundleResult > {
8389 let mut bundled_files = Vec :: new ( ) ;
90+
8491 let mut all_content = String :: new ( ) ;
8592
8693 // Collect paths first to avoid borrow issues
@@ -95,7 +102,9 @@ impl BundleBuilder {
95102
96103 // Compile and collect content
97104 let content = self . compile_file ( source_path) ?;
105+
98106 all_content. push_str ( & content) ;
107+
99108 all_content. push_str ( "\n " ) ;
100109
101110 bundled_files. push ( entry) ;
@@ -108,6 +117,7 @@ impl BundleBuilder {
108117
109118 // Generate output filename
110119 let output_filename = self . generate_output_filename ( ) ;
120+
111121 let output_path = Path :: new ( & self . config . output_dir ) . join ( & output_filename) ;
112122
113123 // Write bundle
@@ -117,6 +127,7 @@ impl BundleBuilder {
117127 let source_map_path = if self . config . source_map {
118128 let map_path =
119129 Path :: new ( & self . config . output_dir ) . join ( format ! ( "{}.map" , output_filename. replace( ".js" , "" ) ) ) ;
130+
120131 Some ( map_path. to_string_lossy ( ) . to_string ( ) )
121132 } else {
122133 None
@@ -141,6 +152,7 @@ impl BundleBuilder {
141152 fn build_with_esbuild ( & mut self ) -> anyhow:: Result < BundleResult > {
142153 // Import and use the esbuild wrapper
143154 let wrapper = super :: ESBuild :: EsbuildWrapper :: new ( ) ;
155+
144156 wrapper. build ( & self . config )
145157 }
146158
@@ -162,15 +174,20 @@ impl BundleBuilder {
162174
163175 // Extract imports (simplified)
164176 let mut deps = Vec :: new ( ) ;
177+
165178 for line in content. lines ( ) {
166179 let trimmed = line. trim ( ) ;
180+
167181 if trimmed. starts_with ( "import " ) {
168182 if let Some ( from_idx) = trimmed. find ( "from" ) {
169183 let path_part = & trimmed[ from_idx + 4 ..] ;
184+
170185 if let Some ( quote_start) = path_part. find ( '"' ) {
171186 let path_end = path_part[ quote_start + 1 ..] . find ( '"' ) ;
187+
172188 if let Some ( end) = path_end {
173189 let import_path = & path_part[ quote_start + 1 ..quote_start + 1 + end] ;
190+
174191 deps. push ( import_path. to_string ( ) ) ;
175192 }
176193 }
@@ -198,6 +215,7 @@ impl BundleBuilder {
198215 }
199216
200217 result. push_str ( line) ;
218+
201219 result. push ( '\n' ) ;
202220 }
203221
@@ -239,16 +257,19 @@ impl BundleBuilder {
239257/// Convenience function to create and build a bundle
240258pub fn build_bundle ( config : BundleConfig ) -> anyhow:: Result < BundleResult > {
241259 let mut builder = BundleBuilder :: new ( config) ;
260+
242261 builder. build ( )
243262}
244263
245264#[ cfg( test) ]
246265mod tests {
266+
247267 use super :: * ;
248268
249269 #[ test]
250270 fn test_builder_creation ( ) {
251271 let config = BundleConfig :: single_file ( ) ;
272+
252273 let builder = BundleBuilder :: new ( config) ;
253274
254275 assert ! ( builder. processed. is_empty( ) ) ;
@@ -257,6 +278,7 @@ mod tests {
257278 #[ test]
258279 fn test_add_entry ( ) {
259280 let config = BundleConfig :: bundle ( ) ;
281+
260282 let mut builder = BundleBuilder :: new ( config) ;
261283
262284 builder. add_entry ( BundleEntry :: entry ( "src/index.ts" ) ) ;
@@ -269,6 +291,7 @@ mod tests {
269291 let config = BundleConfig :: bundle ( ) . with_output_file ( "{name}.bundle.js" ) ;
270292
271293 let builder = BundleBuilder :: new ( config) ;
294+
272295 let filename = builder. generate_output_filename ( ) ;
273296
274297 assert_eq ! ( filename, "index.bundle.js" ) ;
@@ -279,6 +302,7 @@ mod tests {
279302 let config = BundleConfig :: bundle ( ) . add_entry ( "src/index.ts" ) . add_entry ( "src/util.ts" ) ;
280303
281304 let builder = BundleBuilder :: new ( config) ;
305+
282306 let hash = builder. compute_hash ( ) ;
283307
284308 assert ! ( !hash. is_empty( ) ) ;
0 commit comments