]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl | |
2 | use v5.14; | |
3 | use warnings; | |
4 | ||
5 | use CSS::Minifier::XS qw//; | |
6 | use CSS::SpriteMaker; | |
7 | ||
8 | use Digest::SHA qw/sha256_base64/; | |
9 | use IO::Compress::Gzip qw/gzip/; | |
10 | use File::Slurp qw/read_file write_file edit_file_lines/; | |
11 | ||
12 | mkdir 'static'; | |
13 | mkdir 'static/css'; | |
14 | mkdir 'static/js'; | |
15 | ||
16 | sub make_sprite { | |
17 | my $maker = Local::CSS::SpriteMaker->new( | |
18 | css_class_prefix => 'logo-', | |
19 | rc_override_classname => sub { | |
20 | my ($name) = @_; | |
21 | $name =~ s/-light/.logo-light/r; | |
22 | } | |
23 | ); | |
24 | ||
25 | $maker->make_sprite( | |
26 | source_images => ['logos/'], | |
27 | target_file => 'static/logos.png', | |
28 | add_extra_padding => 10, | |
29 | ); | |
30 | ||
31 | $maker->print_css( | |
32 | filename => 'css/logos.css', | |
33 | sprite_filename => '/static/logos.png', | |
34 | ); | |
35 | ||
36 | system 'pngnq-s9', '-s1', 'static/logos.png'; | |
37 | system 'optipng', '-o7', '-zm1-9', 'static/logos-nq8.png'; | |
38 | rename 'static/logos-nq8.png', 'static/logos.png'; | |
39 | } | |
40 | ||
41 | sub gzip_file { | |
42 | my ($file) = @_; | |
43 | gzip $file => "$file.gz", -Level => 9, Minimal => 1; | |
44 | } | |
45 | ||
46 | sub make_css { | |
47 | my $common_css; | |
48 | $common_css .= read_file $_ for <css/*.css>; | |
49 | for (<css/themes/*>) { | |
50 | my ($theme) = m,themes/(.*)\.css,; | |
51 | my $css = read_file $_; | |
52 | $css .= $common_css; | |
53 | write_file "static/css/$theme.css", CSS::Minifier::XS::minify $css; | |
54 | gzip_file "static/css/$theme.css"; | |
55 | } | |
56 | } | |
57 | ||
58 | sub make_js { | |
59 | 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/*>; | |
60 | my $js = read_file 'static/js/all.js'; | |
61 | write_file 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js; | |
62 | system 'cp', '-rp', 'js', 'static/'; | |
63 | gzip_file 'static/js/all.js'; | |
64 | } | |
65 | ||
66 | my $sprite_mtime = -M 'static/logos.png' // 0; | |
67 | for (<logos/*>) { | |
68 | if (!$sprite_mtime || $sprite_mtime > -M) { | |
69 | make_sprite; | |
70 | last | |
71 | } | |
72 | } | |
73 | ||
74 | my $css_mtime = -M 'static/css/slate.css' // 0; | |
75 | for (<css/*>, <css/themes/*>) { | |
76 | if (!$css_mtime || $css_mtime > -M) { | |
77 | make_css; | |
78 | last | |
79 | } | |
80 | } | |
81 | ||
82 | my $js_mtime = -M 'static/js.js' // 0; | |
83 | for (<js/*>) { | |
84 | if (!$js_mtime || $js_mtime > -M) { | |
85 | make_js; | |
86 | last | |
87 | } | |
88 | } | |
89 | ||
90 | edit_file_lines { | |
91 | my ($file) = m,(static.*\.(?:css|js)),; | |
92 | return unless $file; | |
93 | my $hash = sha256_base64 scalar read_file $file; | |
94 | s/integrity=".*"/integrity="sha256-$hash="/; | |
95 | } 'tmpl/skel.en'; | |
96 | ||
97 | package | |
98 | Local::CSS::SpriteMaker; | |
99 | ||
100 | use parent qw/CSS::SpriteMaker/; | |
101 | ||
102 | sub _get_stylesheet_string { | |
103 | my $self = shift; | |
104 | my @ret = split "\n", $self->SUPER::_get_stylesheet_string(@_); | |
105 | shift @ret; | |
106 | @ret = sort @ret; | |
107 | unshift @ret, <<EOF; | |
108 | a.logo { | |
109 | background-image: url("/static/logos.png"); | |
110 | background-repeat: no-repeat; | |
111 | display: inline-block; | |
112 | vertical-align: middle; | |
113 | } | |
114 | EOF | |
115 | join "\n", @ret; | |
116 | } |