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/;
11 use List
::Util qw
/first/;
20 my $brotli = which
'brotli';
21 my $zopfli = which
'zopfli';
22 system $brotli => '--input', $file, '--output', "$file.br" if $brotli;
23 system $zopfli => $file if $zopfli;
24 gzip
$file => "$file.gz", -Level
=> 9, Minimal
=> 1 unless $zopfli;
28 my ($file, @content) = @_;
29 write_file
$file, @content;
34 my ($name) = $_[0] =~ m
,/(.*)\
.svg
,;
39 my @logos = <logos
/*>;
40 my $logos = make_sprite \
&sprite_name
, @logos;
43 my $light = s/logos/logos-light/r;
44 push @logos_light, -f
$light ?
$light : $_;
46 my $logos_light = make_sprite \
&sprite_name
, @logos_light;
48 write_gzfile
'static/logos/dark.svg', $logos->render;
49 write_gzfile
'static/logos/light.svg', $logos_light->render;
52 sub read_css_into_blocks
{
55 for (read_file
$file) {
65 my $default_theme = 'cyborg';
68 my ($theme, $decl, $default) = @_;
69 return $decl if $theme eq $default_theme || !$decl;
70 return '' if $decl eq $default;
72 $default =~ s/[^{]*{\n//;
73 $default =~ s/\n}[^}]*//;
74 $decl =~ s/^$_$//m for split "\n", $default;
77 my $prefix = "html.$theme";
78 my ($first_line) = $decl =~ /([^{]*){/;
79 $first_line =~ s/(,\s+)/$1 $prefix /g;
80 $first_line = "$prefix $first_line";
81 $decl =~ s/([^{]*){/$first_line\{/;
86 my $css = join '', map { read_file
$_ } <css
/*.css
>;
89 for (<css
/themes/*>) {
90 my ($theme) = m
,themes
/(.*)\
.css
,;
91 $themes{$theme} = read_css_into_blocks
$_;
93 my @themes = sort grep { $_ ne $default_theme } keys %themes;
95 while (grep { scalar @
$_ } values %themes) {
96 my %blocks = map { $_ => (shift @
{$themes{$_}}) // '' } keys %themes;
97 $css .= $blocks{$default_theme};
98 $css .= theme_prefix
$_, $blocks{$_}, $blocks{$default_theme} for @themes
101 write_gzfile
"static/css/all.css", minify
$css
105 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/*>;
106 my $js = read_file
'static/js/all.js';
107 write_gzfile
'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
108 system 'cp', '-rp', 'js', 'static/';
111 my $sprite_mtime = -M
'static/logos/dark.svg' // 0;
112 for (<logos
/*>, <logos-light/*>) {
113 if (!$sprite_mtime || $sprite_mtime > -M
) {
119 my $css_mtime = -M
'static/css/all.css' // 0;
120 for (<css
/*>, <css
/themes
/*>) {
121 if (!$css_mtime || $css_mtime > -M
) {
127 my $js_mtime = -M
'static/js/all.js' // 0;
129 if (!$js_mtime || $js_mtime > -M
) {
136 my ($file) = m
,(static
.*\
.(?
:css
|js
)),;
138 my $hash = sha256_base64
scalar read_file
$file;
139 s/integrity=".*"/integrity="sha256-$hash="/;