rs-web 0.4.2

A fast, Lua-driven static site generator with encryption support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
//! Build orchestrator for static site generation

use anyhow::{Context, Result};
use log::{debug, info, trace};
use rayon::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::config::{Config, PageDef};
use crate::templates::Templates;
use crate::tracker::{
    AssetRef, BuildTracker, CachedDeps, SharedTracker, extract_html_asset_refs,
    extract_markdown_asset_refs, resolve_url_to_source,
};

/// Cache file name
const CACHE_FILE: &str = ".rs-web-cache/deps.bin";

/// Main build orchestrator
pub struct Builder {
    config: Config,
    output_dir: PathBuf,
    project_dir: PathBuf,
    /// Build dependency tracker
    tracker: SharedTracker,
    /// Cached dependency info from previous build
    cached_deps: Option<CachedDeps>,
    /// Cached global data from last build
    cached_global_data: Option<serde_json::Value>,
    /// Cached page definitions from last build
    cached_pages: Option<Vec<PageDef>>,
}

impl Builder {
    pub fn new(config: Config, output_dir: PathBuf, project_dir: PathBuf) -> Self {
        // Load cached deps from previous build
        let cache_path = project_dir.join(CACHE_FILE);
        let cached_deps = CachedDeps::load(&cache_path);
        if cached_deps.is_some() {
            debug!("Loaded cached dependency info from {:?}", cache_path);
        }

        // Get the tracker from config (it was created during config loading)
        let tracker = config.tracker().clone();

        Self {
            config,
            output_dir,
            project_dir,
            tracker,
            cached_deps,
            cached_global_data: None,
            cached_pages: None,
        }
    }

    /// Create a new builder with a fresh tracker (for full rebuilds)
    pub fn new_with_tracker(project_dir: PathBuf, output_dir: PathBuf) -> Result<Self> {
        let tracker = Arc::new(BuildTracker::new());
        let config = Config::load_with_tracker(&project_dir, tracker.clone())?;

        // Load cached deps from previous build
        let cache_path = project_dir.join(CACHE_FILE);
        let cached_deps = CachedDeps::load(&cache_path);
        if cached_deps.is_some() {
            debug!("Loaded cached dependency info from {:?}", cache_path);
        }

        Ok(Self {
            config,
            output_dir,
            project_dir,
            tracker,
            cached_deps,
            cached_global_data: None,
            cached_pages: None,
        })
    }

    /// Resolve a path relative to the project directory
    fn resolve_path(&self, path: &str) -> PathBuf {
        let p = Path::new(path);
        if p.is_absolute() {
            p.to_path_buf()
        } else {
            self.project_dir.join(path)
        }
    }

    pub fn build(&mut self) -> Result<()> {
        info!("Starting build");
        debug!("Output directory: {:?}", self.output_dir);
        debug!("Project directory: {:?}", self.project_dir);

        // Stage 1: Clean output directory
        trace!("Stage 1: Cleaning output directory");
        self.clean()?;

        // Run before_build hook (after clean, so it can write to output_dir)
        trace!("Running before_build hook");
        self.config.call_before_build()?;

        // Stage 2: Call data() to get global data
        trace!("Stage 2: Calling data() function");
        let global_data = self.config.call_data()?;
        debug!("Global data loaded");

        // Stage 3: Call pages(global) to get page definitions
        trace!("Stage 3: Calling pages() function");
        let pages = self.config.call_pages(&global_data)?;
        debug!("Found {} pages to generate", pages.len());

        // Cache for incremental builds
        self.cached_global_data = Some(global_data.clone());
        self.cached_pages = Some(pages.clone());

        // Stage 4: Load templates
        trace!("Stage 5: Loading templates");
        let templates = Templates::new(
            &self.resolve_path(&self.config.paths.templates),
            Some(self.tracker.clone()),
            Some(self.config.asset_manifest.clone()),
        )?;

        // Stage 6: Render all pages in parallel
        trace!("Stage 6: Rendering {} pages", pages.len());
        self.render_pages(&pages, &global_data, &templates)?;

        info!("Build complete: {} pages generated", pages.len());
        rs_print!("Generated {} pages", pages.len());

        trace!("Running after_build hook");
        self.config.call_after_build()?;

        // Merge all thread-local tracking data and save
        self.tracker.merge_all_threads();
        self.save_cached_deps()?;

        Ok(())
    }

    /// Save tracked dependencies to cache file
    fn save_cached_deps(&self) -> Result<()> {
        // Clean up stale files before saving
        self.cleanup_stale_writes();

        let cache_path = self.project_dir.join(CACHE_FILE);
        let deps = CachedDeps::from_tracker(&self.tracker);
        deps.save(&cache_path)
            .with_context(|| format!("Failed to save dependency cache to {:?}", cache_path))?;
        debug!(
            "Saved dependency cache: {} reads, {} writes",
            deps.reads.len(),
            deps.writes.len()
        );
        Ok(())
    }

    /// Remove files that were written in previous build but not in current build
    fn cleanup_stale_writes(&self) {
        let Some(old_deps) = &self.cached_deps else {
            return;
        };

        let new_writes = self.tracker.get_writes();
        let mut removed = 0;

        for old_path in old_deps.writes.keys() {
            // Only clean up files in output directory
            if !old_path.starts_with(&self.output_dir) {
                continue;
            }

            // If file was written before but not now, it's stale
            if !new_writes.contains_key(old_path) && old_path.exists() {
                if let Err(e) = std::fs::remove_file(old_path) {
                    debug!("Failed to remove stale file {:?}: {}", old_path, e);
                } else {
                    debug!("Removed stale file: {:?}", old_path);
                    removed += 1;
                }
            }
        }

        if removed > 0 {
            debug!("Cleaned up {} stale files", removed);
        }
    }

    /// Remove files that exist in old_writes but not in new_writes
    fn cleanup_stale_files(
        &self,
        old_writes: &std::collections::HashMap<PathBuf, crate::tracker::FileState>,
        new_writes: &std::collections::HashMap<PathBuf, crate::tracker::FileState>,
    ) {
        let mut removed = 0;

        for old_path in old_writes.keys() {
            // Only clean up files in output directory
            if !old_path.starts_with(&self.output_dir) {
                continue;
            }

            // If file was written before but not now, it's stale
            if !new_writes.contains_key(old_path) && old_path.exists() {
                if let Err(e) = std::fs::remove_file(old_path) {
                    debug!("Failed to remove stale file {:?}: {}", old_path, e);
                } else {
                    debug!("Removed stale file: {:?}", old_path);
                    removed += 1;
                }
            }
        }

        if removed > 0 {
            rs_print!("Cleaned up {} stale files", removed);
        }
    }

    /// Get files that have changed since last build
    pub fn get_changed_files(&self) -> Vec<PathBuf> {
        match &self.cached_deps {
            Some(cached) => self.tracker.get_changed_files(cached),
            None => Vec::new(), // No cache means full rebuild needed
        }
    }

    /// Check if a full rebuild is needed (no cache or config changed)
    pub fn needs_full_rebuild(&self) -> bool {
        self.cached_deps.is_none()
    }

    /// Check if a file was tracked as a dependency in the last build
    /// This includes both explicit reads (via Lua API) and implicit refs (via HTML/markdown)
    pub fn is_tracked_file(&self, path: &Path) -> bool {
        if let Some(ref cached) = self.cached_deps {
            // Canonicalize path for comparison
            let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
            // Check if it was read via Lua API (copy_file, read_file, etc.)
            if cached.reads.contains_key(&path) {
                return true;
            }
            // Check if it's referenced in any page's HTML/markdown
            if cached.asset_to_pages.contains_key(&path) {
                return true;
            }
            false
        } else {
            // No cache, assume all files are relevant
            true
        }
    }

    /// Check if any tracked files have changed since last build
    pub fn has_tracked_changes(&self) -> bool {
        if let Some(ref cached) = self.cached_deps {
            !self.tracker.get_changed_files(cached).is_empty()
        } else {
            true // No cache means we need to build
        }
    }

    fn clean(&self) -> Result<()> {
        if self.output_dir.exists() {
            debug!("Removing existing output directory: {:?}", self.output_dir);
            fs::remove_dir_all(&self.output_dir).with_context(|| {
                format!("Failed to clean output directory: {:?}", self.output_dir)
            })?;
        }
        trace!("Creating output directories");
        fs::create_dir_all(&self.output_dir)?;
        fs::create_dir_all(self.output_dir.join("static"))?;
        Ok(())
    }

    /// Remove pages that existed in the old build but not in the new one
    fn remove_stale_pages(&self, old_pages: &[PageDef], new_pages: &[PageDef]) -> Result<()> {
        use std::collections::HashSet;

        // Collect new page paths
        let new_paths: HashSet<&str> = new_pages.iter().map(|p| p.path.as_str()).collect();

        // Find and remove stale pages
        for old_page in old_pages {
            if !new_paths.contains(old_page.path.as_str()) {
                let relative_path = old_page.path.trim_matches('/');

                // Check if path has a file extension
                let has_extension = relative_path.contains('.')
                    && !relative_path.ends_with('/')
                    && relative_path
                        .rsplit('/')
                        .next()
                        .map(|s| s.contains('.'))
                        .unwrap_or(false);

                let file_path = if has_extension {
                    self.output_dir.join(relative_path)
                } else if relative_path.is_empty() {
                    self.output_dir.join("index.html")
                } else {
                    self.output_dir.join(relative_path).join("index.html")
                };

                if file_path.exists() {
                    rs_print!("  Removed: {}", old_page.path);
                    fs::remove_file(&file_path)?;

                    // Try to remove empty parent directory
                    if let Some(parent) = file_path.parent()
                        && parent != self.output_dir
                        && parent.read_dir()?.next().is_none()
                    {
                        let _ = fs::remove_dir(parent);
                    }
                }
            }
        }

        Ok(())
    }

    fn render_pages(
        &self,
        pages: &[PageDef],
        global_data: &serde_json::Value,
        templates: &Templates,
    ) -> Result<()> {
        // Render all pages in parallel
        pages
            .par_iter()
            .try_for_each(|page| self.render_single_page(page, global_data, templates))?;

        Ok(())
    }

    fn render_single_page(
        &self,
        page: &PageDef,
        global_data: &serde_json::Value,
        templates: &Templates,
    ) -> Result<()> {
        trace!("Rendering page: {}", page.path);

        // Process content through markdown using Lua rs.markdown.render if provided
        let html_content = if let Some(ref markdown) = page.content {
            Some(self.config.render_markdown(markdown)?)
        } else {
            page.html.clone()
        };

        // If no template, output html directly (for raw text/xml files)
        let html = if page.template.is_none() {
            html_content.unwrap_or_default()
        } else {
            templates.render_page(&self.config, page, global_data, html_content.as_deref())?
        };

        // Extract asset references from the generated HTML and markdown content
        self.extract_and_record_asset_refs(page, &html);

        // Minify HTML if enabled (default: true)
        let html = if page.minify {
            minify_html(&html)
        } else {
            html
        };

        // Write output file
        let relative_path = page.path.trim_matches('/');

        // Check if path has a file extension (e.g., feed.xml, sitemap.json)
        let has_extension = relative_path.contains('.')
            && !relative_path.ends_with('/')
            && relative_path
                .rsplit('/')
                .next()
                .map(|s| s.contains('.'))
                .unwrap_or(false);

        if has_extension {
            // Write directly to file path (e.g., /feed.xml -> dist/feed.xml)
            let file_path = self.output_dir.join(relative_path);
            if let Some(parent) = file_path.parent() {
                fs::create_dir_all(parent)?;
            }
            fs::write(&file_path, &html)?;
            // Track the write for CSS purging
            self.tracker.record_write(file_path, html.as_bytes());
        } else {
            // Write to directory with index.html (e.g., /about/ -> dist/about/index.html)
            let page_dir = if relative_path.is_empty() {
                self.output_dir.clone()
            } else {
                self.output_dir.join(relative_path)
            };
            fs::create_dir_all(&page_dir)?;
            let file_path = page_dir.join("index.html");
            fs::write(&file_path, &html)?;
            // Track the write for CSS purging
            self.tracker.record_write(file_path, html.as_bytes());
        }

        Ok(())
    }

    /// Extract asset references from HTML and markdown, record them in tracker
    fn extract_and_record_asset_refs(&self, page: &PageDef, html: &str) {
        // Extract from HTML
        let mut url_paths: Vec<String> = extract_html_asset_refs(html);

        // Also extract from markdown content if present
        if let Some(ref markdown) = page.content {
            let md_refs = extract_markdown_asset_refs(markdown);
            url_paths.extend(md_refs);
        }

        if url_paths.is_empty() {
            return;
        }

        // Get writes to resolve URL paths to source files
        let writes = self.tracker.get_writes();

        // Convert URL paths to AssetRefs with source resolution
        let asset_refs: Vec<AssetRef> = url_paths
            .into_iter()
            .map(|url_path| {
                let source_path =
                    resolve_url_to_source(&url_path, &self.output_dir, &writes, &self.project_dir);
                AssetRef {
                    url_path,
                    source_path,
                }
            })
            .collect();

        // Record in tracker
        let page_path = PathBuf::from(&page.path);
        self.tracker.record_html_refs(page_path, asset_refs);
    }

    /// Perform an incremental build based on what changed
    /// Uses tracker data to filter changes to only files that were actually used
    pub fn incremental_build(&mut self, changes: &crate::watch::ChangeSet) -> Result<()> {
        debug!("Starting incremental build");
        trace!("Change set: {:?}", changes);

        // Config changed - full rebuild needed (Lua functions may have changed)
        if changes.full_rebuild {
            return self.build();
        }

        // Filter content changes to only files that were tracked as dependencies
        let relevant_content: Vec<PathBuf> = changes
            .content_files
            .iter()
            .filter(|p| {
                let full_path = self.project_dir.join(p);
                let is_tracked = self.is_tracked_file(&full_path);
                if !is_tracked {
                    trace!("Skipping untracked content file: {:?}", p);
                }
                is_tracked
            })
            .map(|p| self.project_dir.join(p))
            .collect();

        // Filter asset changes to only files that were tracked as dependencies
        let relevant_assets: Vec<PathBuf> = changes
            .asset_files
            .iter()
            .filter(|p| {
                let full_path = self.project_dir.join(p);
                let is_tracked = self.is_tracked_file(&full_path);
                if !is_tracked {
                    trace!("Skipping untracked asset file: {:?}", p);
                }
                is_tracked
            })
            .map(|p| self.project_dir.join(p))
            .collect();

        // Log skipped files
        if !changes.content_files.is_empty() && relevant_content.is_empty() {
            debug!(
                "All {} content files were untracked, skipping",
                changes.content_files.len()
            );
        }
        if !changes.asset_files.is_empty() && relevant_assets.is_empty() {
            debug!(
                "All {} asset files were untracked, skipping",
                changes.asset_files.len()
            );
        }

        // Handle asset changes first (before_build runs copy_file, etc.)
        if !relevant_assets.is_empty() {
            debug!(
                "{} tracked assets changed (out of {} total)",
                relevant_assets.len(),
                changes.asset_files.len()
            );
            self.rebuild_assets_only(&relevant_assets)?;
        }

        // Handle CSS changes
        if changes.rebuild_css {
            self.rebuild_css_only()?;
        }

        // Content files changed - try incremental update
        if !relevant_content.is_empty() {
            debug!(
                "{} tracked content files changed (out of {} total)",
                relevant_content.len(),
                changes.content_files.len()
            );
            return self.rebuild_content_only(&relevant_content);
        }

        // Template changes - re-render affected pages with cached data (skip Lua calls)
        if changes.has_template_changes() {
            for path in &changes.template_files {
                rs_print!("  Changed: {}", path.display());
            }
            return self.rebuild_templates_only(&changes.template_files);
        }

        Ok(())
    }

    /// Rebuild content - use incremental update if available, otherwise full data reload
    fn rebuild_content_only(&mut self, changed_paths: &[PathBuf]) -> Result<()> {
        debug!(
            "Content-only rebuild for {} changed files",
            changed_paths.len()
        );

        // Print changed files
        for path in changed_paths {
            if let Ok(rel) = path.strip_prefix(&self.project_dir) {
                rs_print!("  Changed: {}", rel.display());
            } else {
                rs_print!("  Changed: {}", path.display());
            }
        }

        // Try incremental update if update_data function exists and we have cached data
        let global_data = if let Some(cached) = self.cached_global_data.as_ref()
            && self.config.has_update_data()
        {
            debug!("Using incremental update_data()");
            // Convert absolute paths to relative paths for Lua
            let relative_paths: Vec<PathBuf> = changed_paths
                .iter()
                .filter_map(|p| {
                    p.strip_prefix(&self.project_dir)
                        .ok()
                        .map(|r| r.to_path_buf())
                })
                .collect();
            self.config.call_update_data(cached, &relative_paths)?
        } else {
            debug!("Using full data() reload");
            self.config.call_data()?
        };

        let pages = self.config.call_pages(&global_data)?;

        // Remove stale pages that no longer exist in the new page list
        if let Some(ref old_pages) = self.cached_pages {
            self.remove_stale_pages(old_pages, &pages)?;
        }

        // Update cache
        self.cached_global_data = Some(global_data.clone());
        self.cached_pages = Some(pages.clone());

        // Reload templates and re-render
        let templates = Templates::new(
            &self.resolve_path(&self.config.paths.templates),
            Some(self.tracker.clone()),
            Some(self.config.asset_manifest.clone()),
        )?;
        self.render_pages(&pages, &global_data, &templates)?;

        // Merge thread-local tracking data and save
        self.tracker.merge_all_threads();
        self.save_cached_deps()?;

        self.config.call_after_build()?;

        rs_print!("Re-rendered {} pages (content changed)", pages.len());
        Ok(())
    }

    /// Rebuild only by re-rendering templates with cached data
    fn rebuild_templates_only(
        &mut self,
        changed_template_files: &std::collections::HashSet<PathBuf>,
    ) -> Result<()> {
        let (global_data, all_pages) = match (&self.cached_global_data, &self.cached_pages) {
            (Some(data), Some(pages)) => (data.clone(), pages.clone()),
            _ => {
                // No cache available, do a full build to populate it
                log::info!("No cached data available, performing full build");
                return self.build();
            }
        };

        // Reload templates and get dependency graph
        let template_dir = self.resolve_path(&self.config.paths.templates);
        let templates = Templates::new(
            &template_dir,
            Some(self.tracker.clone()),
            Some(self.config.asset_manifest.clone()),
        )?;
        let deps = templates.deps();

        // Find all affected templates (transitively)
        let mut affected_templates = std::collections::HashSet::new();
        for changed_path in changed_template_files {
            // Find template name from path
            if let Some(template_name) = deps.find_template_by_path(changed_path) {
                let transitive = deps.get_affected_templates(template_name);
                affected_templates.extend(transitive);
            } else if let Ok(rel_path) = changed_path.strip_prefix(&template_dir) {
                // Try relative path as template name
                let template_name = rel_path.to_string_lossy().to_string();
                let transitive = deps.get_affected_templates(&template_name);
                affected_templates.extend(transitive);
            }
        }

        debug!("Affected templates: {:?}", affected_templates);

        // Filter pages to only those using affected templates
        let pages_to_rebuild: Vec<_> = all_pages
            .iter()
            .filter(|page| {
                if let Some(ref template) = page.template {
                    affected_templates.contains(template)
                } else {
                    false
                }
            })
            .cloned()
            .collect();

        if pages_to_rebuild.is_empty() {
            rs_print!("No pages affected by template changes");
            return Ok(());
        }

        debug!(
            "Template rebuild: {} of {} pages affected",
            pages_to_rebuild.len(),
            all_pages.len()
        );

        // Re-render only affected pages with cached data
        self.render_pages(&pages_to_rebuild, &global_data, &templates)?;

        // Merge thread-local tracking data and save (cleanup stale files)
        self.tracker.merge_all_threads();
        self.save_cached_deps()?;

        self.config.call_after_build()?;

        rs_print!(
            "Re-rendered {} of {} pages (templates changed)",
            pages_to_rebuild.len(),
            all_pages.len()
        );
        Ok(())
    }

    /// Rebuild CSS by calling before_build hook (CSS is now handled via Lua)
    fn rebuild_css_only(&self) -> Result<()> {
        rs_print!("  Changed: styles");
        self.config.call_before_build()?;
        rs_print!("Rebuilt CSS");

        // Merge thread-local tracking data and save
        self.tracker.merge_all_threads();
        self.save_cached_deps()?;

        self.config.call_after_build()?;

        Ok(())
    }

    /// Rebuild assets by calling before_build hook and re-rendering pages
    /// Pages need to be re-rendered because asset hashes may have changed
    fn rebuild_assets_only(&mut self, changed_paths: &[PathBuf]) -> Result<()> {
        // Save old writes before clearing to find stale files later
        let old_writes = self.tracker.get_writes();
        self.tracker.clear_writes();

        for path in changed_paths {
            if let Ok(rel) = path.strip_prefix(&self.project_dir) {
                rs_print!("  Changed: {}", rel.display());
            } else {
                rs_print!("  Changed: {}", path.display());
            }
        }
        // Run before_build which rebuilds assets and updates the manifest
        self.config.call_before_build()?;

        // Re-render all pages since asset paths may have changed
        // (the asset filter uses the manifest which was just updated)
        if let (Some(global_data), Some(pages)) = (&self.cached_global_data, &self.cached_pages) {
            let templates = Templates::new(
                &self.resolve_path(&self.config.paths.templates),
                Some(self.tracker.clone()),
                Some(self.config.asset_manifest.clone()),
            )?;
            self.render_pages(pages, global_data, &templates)?;
            rs_print!(
                "Rebuilt {} assets, re-rendered {} pages",
                changed_paths.len(),
                pages.len()
            );
        } else {
            rs_print!("Rebuilt {} assets", changed_paths.len());
        }

        // Merge thread-local tracking data
        self.tracker.merge_all_threads();

        // Find and delete stale files (in old_writes but not in new_writes)
        let new_writes = self.tracker.get_writes();
        self.cleanup_stale_files(&old_writes, &new_writes);

        self.save_cached_deps()?;

        self.config.call_after_build()?;

        Ok(())
    }

    /// Reload config from disk
    pub fn reload_config(&mut self) -> Result<()> {
        debug!("Reloading config from {:?}", self.project_dir);
        self.config = crate::config::Config::load(&self.project_dir)?;
        // Clear cache since Lua functions might produce different output
        self.cached_global_data = None;
        self.cached_pages = None;
        info!("Config reloaded successfully");
        Ok(())
    }

    /// Get a reference to the current config
    pub fn config(&self) -> &Config {
        &self.config
    }
}

/// Minify HTML content with OXC-based inline JS minification
fn minify_html(html: &str) -> String {
    // First, minify inline JS with OXC (minify-js has bugs)
    let html = minify_inline_js(html);

    let cfg = minify_html::Cfg {
        minify_js: false,
        minify_css: true,
        ..Default::default()
    };
    let minified = minify_html::minify(html.as_bytes(), &cfg);
    String::from_utf8(minified).unwrap_or_else(|_| html.to_string())
}

/// Minify inline <script> tags using OXC
fn minify_inline_js(html: &str) -> String {
    use oxc_allocator::Allocator;
    use oxc_codegen::{Codegen, CodegenOptions};
    use oxc_minifier::{CompressOptions, MangleOptions, Minifier, MinifierOptions};
    use oxc_parser::Parser;
    use oxc_span::SourceType;
    use regex::Regex;

    let re = Regex::new(r"(?s)(<script(?:\s[^>]*)?>)(.*?)(</script>)").unwrap();

    re.replace_all(html, |caps: &regex::Captures| {
        let open_tag = &caps[1];
        let content = &caps[2];
        let close_tag = &caps[3];

        // Skip external scripts (src=) or empty scripts
        if open_tag.contains("src=") || content.trim().is_empty() {
            return format!("{}{}{}", open_tag, content, close_tag);
        }

        // Try to minify with OXC
        let allocator = Allocator::default();
        let source_type = SourceType::mjs();
        let ret = Parser::new(&allocator, content, source_type).parse();

        if !ret.errors.is_empty() {
            // Parse error - return original
            return format!("{}{}{}", open_tag, content, close_tag);
        }

        let mut program = ret.program;
        let options = MinifierOptions {
            mangle: Some(MangleOptions::default()),
            compress: Some(CompressOptions::default()),
        };

        Minifier::new(options).minify(&allocator, &mut program);
        let minified = Codegen::new()
            .with_options(CodegenOptions::minify())
            .build(&program)
            .code;

        format!("{}{}{}", open_tag, minified, close_tag)
    })
    .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_minify_html_basic() {
        let input = "<html>  <body>   <p>Hello</p>  </body>  </html>";
        let result = minify_html(input);
        assert!(result.len() <= input.len());
        assert!(result.contains("Hello"));
    }

    #[test]
    fn test_minify_html_preserves_pre() {
        let input = "<pre>  code  with  spaces  </pre>";
        let result = minify_html(input);
        // Pre tags should preserve whitespace
        assert!(result.contains("code  with  spaces"));
    }

    #[test]
    fn test_minify_inline_js_basic() {
        let input = r#"<script>
            function hello() {
                console.log("hi");
            }
        </script>"#;
        let result = minify_inline_js(input);
        assert!(
            !result.contains('\n') || result.matches('\n').count() < input.matches('\n').count()
        );
        assert!(result.contains("<script>"));
        assert!(result.contains("</script>"));
    }

    #[test]
    fn test_minify_inline_js_skips_external() {
        let input = r#"<script src="/js/app.js"></script>"#;
        let result = minify_inline_js(input);
        assert_eq!(result, input);
    }

    #[test]
    fn test_minify_inline_js_skips_empty() {
        let input = "<script></script>";
        let result = minify_inline_js(input);
        assert_eq!(result, input);
    }

    #[test]
    fn test_minify_inline_js_multiple_scripts() {
        // Use console.log to prevent DCE
        let input = r#"<script>console.log(1);</script><script>console.log(2);</script>"#;
        let result = minify_inline_js(input);
        assert!(
            result.contains("console.log(1)") && result.contains("console.log(2)"),
            "Result: {}",
            result
        );
    }

    #[test]
    fn test_minify_inline_js_preserves_on_parse_error() {
        let input = "<script>function { broken</script>";
        let result = minify_inline_js(input);
        // Should preserve original on parse error
        assert!(result.contains("function { broken"));
    }

    #[test]
    fn test_minify_inline_js_with_attributes() {
        // Use console.log to prevent DCE
        let input = r#"<script type="text/javascript">console.log(1);</script>"#;
        let result = minify_inline_js(input);
        assert!(result.contains(r#"type="text/javascript""#));
    }

    #[test]
    fn test_minify_html_with_inline_js() {
        // Use console.log to prevent DCE
        let input = r#"<html><head><script>console.log(true);</script></head></html>"#;
        let result = minify_html(input);
        // Should minify JS (true -> !0)
        assert!(
            result.contains("!0") || result.contains("true"),
            "Result: {}",
            result
        );
    }

    #[test]
    fn test_minify_html_css_minification() {
        let input = r#"<style>  body  {  color:  red;  }  </style>"#;
        let result = minify_html(input);
        assert!(result.len() < input.len());
    }
}