From 8798626b0e96a4d1eb423f7f70ece78a2fcb4ae7 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Mon, 2 Mar 2015 17:05:58 +0200 Subject: [PATCH] Generate minified js/css during build --- Makefile.PL | 10 +++++++--- js/90-themes.js | 2 +- lib/Plack/App/Gruntmaster.pm | 20 -------------------- make_static.PL | 24 ++++++++++++++++++++++++ tmpl/skel.en | 4 ++-- 5 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 make_static.PL diff --git a/Makefile.PL b/Makefile.PL index 0407ab7..2f3a50f 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -9,8 +9,14 @@ WriteMakefile( MIN_PERL_VERSION => '5.14.0', LICENSE => 'AGPL_3', SIGN => 1, + clean => { + FILES => 'static/css/ static/js.js' + }, BUILD_REQUIRES => { - qw/Test::MockTime 0 + qw/CSS::Minifier::XS 0 + File::Slurp 0 + JavaScript::Minifier::XS 0 + Test::MockTime 0 Test::More 0 Test::WWW::Mechanize::PSGI 0/, }, @@ -25,13 +31,11 @@ WriteMakefile( strict 0 warnings 0 - CSS::Minifier::XS 0 Email::Simple 0 Email::Sender::Simple 0 File::Slurp 0 Gruntmaster::Data 0 HTML::Seamstress 0 - JavaScript::Minifier::XS 0 JSON::MaybeXS 0 Log::Log4perl 0 PerlX::Maybe 0 diff --git a/js/90-themes.js b/js/90-themes.js index caefa39..1e6fdce 100644 --- a/js/90-themes.js +++ b/js/90-themes.js @@ -1,7 +1,7 @@ (function(){ 'use strict'; function set_style(name){ - $('#stylesheet').attr("href", "/css/" + name + ".css"); + $('#stylesheet').attr("href", "/static/css/" + name + ".css"); localStorage.setItem("theme", name); } diff --git a/lib/Plack/App/Gruntmaster.pm b/lib/Plack/App/Gruntmaster.pm index 87e9e5a..35d788e 100644 --- a/lib/Plack/App/Gruntmaster.pm +++ b/lib/Plack/App/Gruntmaster.pm @@ -4,10 +4,8 @@ use 5.014000; use strict; our $VERSION = '5999.000_001'; -use CSS::Minifier::XS; use Encode qw/encode decode/; use File::Slurp qw/read_file/; -use JavaScript::Minifier::XS; use JSON::MaybeXS qw/encode_json/; use PerlX::Maybe; use Scope::Upper qw/unwind SUB UP/; @@ -55,8 +53,6 @@ use constant FORMAT_EXTENSION => { use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']]; -sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' } - my ($env, $privacy); sub db { $env->{'gruntmaster.dbic'} } @@ -98,22 +94,6 @@ sub dispatch_request{ $privacy = 'public'; sub (GET) { - sub (/css/:theme) { - my $theme = $_{theme}; - return NOT_FOUND unless -e "css/themes/$theme.css"; - my $css = read_file "css/themes/$theme.css"; - $css .= read_file $_ for ; - my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css; charset=utf-8'); - [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]] - }, - - sub (/js.js) { - my $js; - $js .= read_file $_ for ; - my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript; charset=utf-8'); - [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]] - }, - sub (/robots.txt) { NOT_FOUND }, sub (/src/:job) { diff --git a/make_static.PL b/make_static.PL new file mode 100644 index 0000000..a47df2d --- /dev/null +++ b/make_static.PL @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use v5.14; +use warnings; + +use CSS::Minifier::XS qw//; +use JavaScript::Minifier::XS qw//; + +use File::Slurp qw/read_file write_file/; + +mkdir 'static'; +mkdir 'static/css'; + +my $common_css; +$common_css .= read_file $_ for ; +for () { + my ($theme) = m,themes/(.*)\.css,; + my $css = read_file $_; + $css .= $common_css; + write_file "static/css/$theme.css", CSS::Minifier::XS::minify $css; +} + +my $js; +$js .= read_file $_ for ; +write_file 'static/js.js', JavaScript::Minifier::XS::minify $js; diff --git a/tmpl/skel.en b/tmpl/skel.en index fd549a1..93e07b8 100644 --- a/tmpl/skel.en +++ b/tmpl/skel.en @@ -3,8 +3,8 @@ - - + +