Fix 95-login.js
[plack-app-gruntmaster.git] / make_static.PL
CommitLineData
8798626b
MG
1#!/usr/bin/perl
2use v5.14;
3use warnings;
4
5eae1f77 5use CSS::Minifier::XS qw/minify/;
7e8f9a5c 6use Digest::SHA qw/sha256_base64/;
3c9bcb47 7use IO::Compress::Gzip qw/gzip/;
7e8f9a5c 8use File::Slurp qw/read_file write_file edit_file_lines/;
fa8b7f99 9use File::Which;
5eae1f77 10use List::Util qw/first/;
8798626b
MG
11
12mkdir 'static';
13mkdir 'static/css';
f57a9178 14mkdir 'static/js';
8798626b 15
3c9bcb47
MG
16sub gzip_file {
17 my ($file) = @_;
fa8b7f99
MG
18 my $zopfli = which 'zopfli';
19 system $zopfli => $file if $zopfli;
20 gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli;
21}
22
23sub write_gzfile {
24 my ($file, @content) = @_;
25 write_file $file, @content;
26 gzip_file $file
3c9bcb47
MG
27}
28
5eae1f77
MG
29sub read_css_into_blocks {
30 my ($file) = @_;
31 my (@blocks, $block);
32 for (read_file $file) {
33 $block .= $_;
34 if (/^}/) {
35 push @blocks, $block;
36 $block = '';
37 }
38 }
39 \@blocks
40}
41
cf2b0139 42sub make_css {
5eae1f77
MG
43 my %css;
44 $css{common} .= read_file $_ for <css/*.css>;
45
46 my (%themes, $rndtheme);
cf2b0139 47 for (<css/themes/*>) {
5eae1f77
MG
48 ($rndtheme) = m,themes/(.*)\.css,;
49 $themes{$rndtheme} = read_css_into_blocks $_;
50 }
51
52 while (grep { scalar @$_ } values %themes) {
53 my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes;
54 if (grep { $_ ne $blocks{$rndtheme} } values %blocks) {
55 $css{$_} .= $blocks{$_} for keys %themes;
56 } else {
57 $css{common} .= $blocks{$rndtheme};
58 }
59 }
60
fa8b7f99 61 write_gzfile "static/css/$_.css", minify $css{$_} for keys %css
8798626b
MG
62}
63
cf2b0139 64sub make_js {
8345760a 65 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/*>;
f57a9178 66 my $js = read_file 'static/js/all.js';
fa8b7f99 67 write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
f57a9178 68 system 'cp', '-rp', 'js', 'static/';
cf2b0139
MG
69}
70
71my $css_mtime = -M 'static/css/slate.css' // 0;
72for (<css/*>, <css/themes/*>) {
73 if (!$css_mtime || $css_mtime > -M) {
74 make_css;
75 last
76 }
77}
78
a90fc634 79my $js_mtime = -M 'static/js/all.js' // 0;
cf2b0139
MG
80for (<js/*>) {
81 if (!$js_mtime || $js_mtime > -M) {
82 make_js;
83 last
84 }
9cad7bdd 85}
7e8f9a5c
MG
86
87edit_file_lines {
88 my ($file) = m,(static.*\.(?:css|js)),;
89 return unless $file;
90 my $hash = sha256_base64 scalar read_file $file;
91 s/integrity=".*"/integrity="sha256-$hash="/;
92} 'tmpl/skel.en'
This page took 0.017564 seconds and 4 git commands to generate.