5 use CSS
::Minifier
::XS qw
/minify/;
6 use Digest
::SHA qw
/sha256_base64/;
7 use IO
::Compress
::Gzip qw
/gzip/;
8 use File
::Slurp qw
/read_file write_file edit_file_lines/;
10 use List
::Util qw
/first/;
18 my $brotli = which
'brotli';
19 my $zopfli = which
'zopfli';
20 system $brotli => '--input', $file, '--output', "$file.br" if $brotli;
21 system $zopfli => $file if $zopfli;
22 gzip
$file => "$file.gz", -Level
=> 9, Minimal
=> 1 unless $zopfli;
26 my ($file, @content) = @_;
27 write_file
$file, @content;
31 sub read_css_into_blocks
{
34 for (read_file
$file) {
44 my $default_theme = 'cyborg';
47 my ($theme, $decl, $default) = @_;
48 return $decl if $theme eq $default_theme || !$decl;
49 return '' if $decl eq $default;
51 $default =~ s/[^{]*{\n//;
52 $default =~ s/\n}[^}]*//;
53 $decl =~ s/^$_$//m for split "\n", $default;
56 my $prefix = "html.$theme";
57 my ($first_line) = $decl =~ /([^{]*){/;
58 $first_line =~ s/(,\s+)/$1 $prefix /g;
59 $first_line = "$prefix $first_line";
60 $decl =~ s/([^{]*){/$first_line\{/;
65 my $css = join '', map { read_file
$_ } <css
/*.css
>;
68 for (<css
/themes/*>) {
69 my ($theme) = m
,themes
/(.*)\
.css
,;
70 $themes{$theme} = read_css_into_blocks
$_;
72 my @themes = sort grep { $_ ne $default_theme } keys %themes;
74 while (grep { scalar @
$_ } values %themes) {
75 my %blocks = map { $_ => (shift @
{$themes{$_}}) // '' } keys %themes;
76 $css .= $blocks{$default_theme};
77 $css .= theme_prefix
$_, $blocks{$_}, $blocks{$default_theme} for @themes
80 write_gzfile
"static/css/all.css", minify
$css
84 system java
=> -jar
=> 'compiler.jar', qw
,-O SIMPLE
--create_source_map static
/js/js
.map --js_output_file static
/js/all.js
--language_in ECMASCRIPT6_STRICT
--language_out ECMASCRIPT5_STRICT
--source_map_location_mapping js
/|/static
/js
/,, <js/*>;
85 my $js = read_file
'static/js/all.js';
86 write_gzfile
'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
87 system 'cp', '-rp', 'js', 'static/';
90 my $css_mtime = -M
'static/css/all.css' // 0;
91 for (<css
/*>, <css
/themes
/*>) {
92 if (!$css_mtime || $css_mtime > -M
) {
98 my $js_mtime = -M
'static/js/all.js' // 0;
100 if (!$js_mtime || $js_mtime > -M
) {
107 my ($file) = m
,(static
.*\
.(?
:css
|js
)),;
109 my $hash = sha256_base64
scalar read_file
$file;
110 s/integrity=".*"/integrity="sha256-$hash="/;