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 $zopfli = which
'zopfli';
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;
32 my ($name) = $_[0] =~ m
,/(.*)\
.svg
,;
37 my @logos = <logos
/*>;
38 my $logos = make_sprite \
&sprite_name
, @logos;
41 my $light = s/logos/logos-light/r;
42 push @logos_light, -f
$light ?
$light : $_;
44 my $logos_light = make_sprite \
&sprite_name
, @logos_light;
46 write_gzfile
'static/logos/dark.svg', $logos->render;
47 write_gzfile
'static/logos/light.svg', $logos_light->render;
50 sub read_css_into_blocks
{
53 for (read_file
$file) {
63 my $default_theme = 'cyborg';
66 my ($theme, $decl, $default) = @_;
67 return $decl if $theme eq $default_theme || !$decl;
68 return '' if $decl eq $default;
70 $default =~ s/[^{]*{\n//;
71 $default =~ s/\n}[^}]*//;
72 $decl =~ s/^$_$//m for split "\n", $default;
75 my $prefix = "html.$theme";
76 my ($first_line) = $decl =~ /([^{]*){/;
77 $first_line =~ s/(,\s+)/$1 $prefix /g;
78 $first_line = "$prefix $first_line";
79 $decl =~ s/([^{]*){/$first_line\{/;
84 my $css = join '', map { read_file
$_ } <css
/*.css
>;
87 for (<css
/themes/*>) {
88 my ($theme) = m
,themes
/(.*)\
.css
,;
89 $themes{$theme} = read_css_into_blocks
$_;
91 my @themes = sort grep { $_ ne $default_theme } keys %themes;
93 while (grep { scalar @
$_ } values %themes) {
94 my %blocks = map { $_ => (shift @
{$themes{$_}}) // '' } keys %themes;
95 $css .= $blocks{$default_theme};
96 $css .= theme_prefix
$_, $blocks{$_}, $blocks{$default_theme} for @themes
99 write_gzfile
"static/css/all.css", minify
$css
103 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/*>;
104 my $js = read_file
'static/js/all.js';
105 write_gzfile
'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
106 system 'cp', '-rp', 'js', 'static/';
109 my $sprite_mtime = -M
'static/logos/dark.svg' // 0;
110 for (<logos
/*>, <logos-light/*>) {
111 if (!$sprite_mtime || $sprite_mtime > -M
) {
117 my $css_mtime = -M
'static/css/all.css' // 0;
118 for (<css
/*>, <css
/themes
/*>) {
119 if (!$css_mtime || $css_mtime > -M
) {
125 my $js_mtime = -M
'static/js/all.js' // 0;
127 if (!$js_mtime || $js_mtime > -M
) {
134 my ($file) = m
,(static
.*\
.(?
:css
|js
)),;
136 my $hash = sha256_base64
scalar read_file
$file;
137 s/integrity=".*"/integrity="sha256-$hash="/;