1+ # Regenerate CSS only when HTML/Markdown files change
2+
3+ module Jekyll
4+ module PostcssTrigger
5+ class << self
6+ attr_accessor :last_check_time , :css_touched
7+ end
8+ end
9+ end
10+
11+ Jekyll ::Hooks . register :site , :post_read do |site |
12+ # Skip if not in incremental mode
13+ next unless site . config [ 'incremental' ]
14+
15+ # On first build, only record the check time
16+ if Jekyll ::PostcssTrigger . last_check_time . nil?
17+ Jekyll ::PostcssTrigger . last_check_time = Time . now
18+ Jekyll ::PostcssTrigger . css_touched = false
19+ next
20+ end
21+
22+ # Skip if CSS was already touched in this build
23+ next if Jekyll ::PostcssTrigger . css_touched
24+
25+ # Check if any HTML/Markdown files have changed
26+ content_patterns = [
27+ "_layouts/**/*.html" ,
28+ "_includes/**/*.html" ,
29+ "*.html" ,
30+ "*.md" ,
31+ "*/**/*.html" ,
32+ "*/**/*.md"
33+ ]
34+
35+ html_changed = false
36+ last_check = Jekyll ::PostcssTrigger . last_check_time
37+
38+ content_patterns . each do |pattern |
39+ Dir . glob ( site . in_source_dir ( pattern ) ) . each do |file |
40+ next if file . start_with? ( site . dest ) # Exclude _site directory
41+
42+ # Check if file was modified since last check
43+ if File . exist? ( file ) && File . mtime ( file ) > last_check
44+ html_changed = true
45+ Jekyll . logger . info "PostCSS Trigger:" , "Detected change in #{ File . basename ( file ) } "
46+ break
47+ end
48+ end
49+ break if html_changed
50+ end
51+
52+ # Touch CSS file if HTML has changed
53+ if html_changed
54+ css_file = site . in_source_dir ( "stylesheets/main.css" )
55+ if File . exist? ( css_file )
56+ Jekyll . logger . info "PostCSS Trigger:" , "Marking CSS for rebuild"
57+ FileUtils . touch ( css_file )
58+ Jekyll ::PostcssTrigger . css_touched = true
59+ end
60+ end
61+
62+ # Update check time
63+ Jekyll ::PostcssTrigger . last_check_time = Time . now
64+ end
65+
66+ # Reset flag after build completes
67+ Jekyll ::Hooks . register :site , :post_write do |site |
68+ Jekyll ::PostcssTrigger . css_touched = false
69+ end
0 commit comments