]> iEval git - gruntmaster-page.git/blob - make_static.PL
Use Subresource Integrity
[gruntmaster-page.git] / make_static.PL
1 #!/usr/bin/perl
2 use v5.14;
3 use warnings;
4
5 use CSS::Minifier::XS qw//;
6 use JavaScript::Minifier::XS qw//;
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
15 sub gzip_file {
16 my ($file) = @_;
17 gzip $file => "$file.gz", -Level => 9, Minimal => 1;
18 }
19
20 sub make_css {
21 my $common_css;
22 $common_css .= read_file $_ for <css/*.css>;
23 for (<css/themes/*>) {
24 my ($theme) = m,themes/(.*)\.css,;
25 my $css = read_file $_;
26 $css .= $common_css;
27 write_file "static/css/$theme.css", CSS::Minifier::XS::minify $css;
28 gzip_file "static/css/$theme.css";
29 }
30 }
31
32 sub make_js {
33 if (-f 'compiler.jar') {
34 system java => -jar => 'compiler.jar', qw,-O SIMPLE --create_source_map static/js.map --js_output_file static/js.js --language_in ECMASCRIPT5_STRICT --source_map_location_mapping js/|/static/js/,, <js/*>;
35 my $js = read_file 'static/js.js';
36 write_file 'static/js.js', '//# sourceMappingURL=/static/js.map', "\n", $js;
37 system 'cp', '-rp', 'js', 'static/';
38 } else {
39 my $js;
40 $js .= read_file $_ for <js/*.js>;
41 write_file 'static/js.js', JavaScript::Minifier::XS::minify $js;
42 }
43 gzip_file 'static/js.js';
44 }
45
46 my $css_mtime = -M 'static/css/slate.css' // 0;
47 for (<css/*>, <css/themes/*>) {
48 if (!$css_mtime || $css_mtime > -M) {
49 make_css;
50 last
51 }
52 }
53
54 my $js_mtime = -M 'static/js.js' // 0;
55 for (<js/*>) {
56 if (!$js_mtime || $js_mtime > -M) {
57 make_js;
58 last
59 }
60 }
61
62 edit_file_lines {
63 my ($file) = m,(static.*\.(?:css|js)),;
64 return unless $file;
65 my $hash = sha256_base64 scalar read_file $file;
66 s/integrity=".*"/integrity="sha256-$hash="/;
67 } 'tmpl/skel.en'
This page took 0.052431 seconds and 4 git commands to generate.