Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Crude attempt to use lessc instead of CSS::LESSp #1

Merged
merged 5 commits into from

2 participants

@semafor

No description provided.

@omega
Owner

A great start!

Please attempt to get the _has_lessc to be a startup thing. Checking on each request will be a waste.

https://metacpan.org/module/Catalyst::Manual::ExtendingCatalyst might give some ideas on how to extend different parts of the catalyst flow.

@semafor

I am a great deal out of my comfort zone.

@omega omega merged commit 330e073 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 23, 2012
  1. a non-developer attemt on using lessc and offering client side lessc.…

    Jonas G. Drange authored
    …js when lessc is not present.
  2. tests for client side parsing

    Jonas G. Drange authored
  3. forgot to fix some tests

    Jonas G. Drange authored
Commits on Apr 24, 2012
  1. configurable lessc.js path and trying to configure has_lessc so it wo…

    Jonas G. Drange authored
    …nt run on every request
This page is out of date. Refresh to see the latest.
View
49 lib/CatalystX/Less.pm
@@ -8,6 +8,9 @@ package CatalystX::Less;
# in root/wrapper.tt
<link rel="stylesheet" href="[% c.uri_for_combined_less('base.less', 'fonts.less') %]"></link>
+
+ # or
+ [% c.less_for('base.less', 'fonts.less') %]
=head1 JUSTIFICATION
@@ -33,6 +36,7 @@ In YourApp.pm file (default config):
__PACKAGE__->config(
'CatalystX::Less' => {
version_path => 1,
+ less_js_path => "https://github.com/cloudhead/less.js/blob/master/dist/less-1.3.0.min.js"
}
);
@@ -40,6 +44,7 @@ In yourapp.yml (or yourapp_local.yml)
CatalystX::Less:
version_path: 1
+ less_js_path: https://github.com/cloudhead/less.js/blob/master/dist/less-1.3.0.min.js
=head2 max_age seconds
@@ -65,6 +70,7 @@ set to false
use Moose::Role;
use namespace::autoclean;
use CatalystX::InjectComponent;
+use Spawn::Safe;
after 'setup_components' => sub {
my $class = shift;
@@ -93,14 +99,53 @@ sub uri_for_combined_less {
my $encoded = join(";", (map { s/\.less$//; $_;} @lesses));
push(@args, $encoded . ".css");
- my $action = $c->controller('Less')->action_for('less');
+
+ my $action = $c->controller('Less')->action_for('less_to_css');
if ($c->VERSION and (not defined($cfg->{version_path}) or $cfg->{version_path})) {
- $action = $c->controller('Less')->action_for('less_versioned');
+ $action = $c->controller('Less')->action_for('less_to_css_versioned');
unshift(@args, $c->VERSION);
}
my $uri = $c->uri_for($action,@args);
return $uri;
+
+}
+
+sub less_for {
+ my $c = shift;
+ my $ret;
+ my $cfg = $c->config->{'CatalystX::Less'};
+
+ if(not defined($cfg->{node_js_path})) {
+ $c->config->{'CatalystX::Less'}{node_js_path} = "https://github.com/cloudhead/less.js/blob/master/dist/less-1.3.0.min.js";
+ }
+
+ if(_has_lessc($c)) {
+ $ret = "<link href=\"". $c->uri_for_combined_less(@_) ."\" type=\"text/css\">";
+ } else {
+ # TODO: configurable?
+ $ret = "<script src=\"".$cfg->{node_js_path}."\" type=\"text/javascript\"></script>";
+ foreach my $less (@_) {
+ # TODO: hardcoded
+ $ret = $ret . "<link href=\"". $c->uri_for("/static/less/".$less) ."\" type=\"text/less\">";
+ }
+ }
+ return $ret;
+}
+
+sub _has_lessc {
+ my $c = shift;
+ my $cfg = $c->config->{'CatalystX::Less'}{has_lessc};
+ if(not defined($cfg)) {
+ my $results = spawn_safe({ argv => [qw{ lessc }], timeout => 2 });
+ if($results->{error}) {
+ $c->log->warn('Cannot use lessc: '.$results->{error});
+ $c->config->{'CatalystX::Less'}{has_lessc} = 0;
+ } else {
+ $c->config->{'CatalystX::Less'}{has_lessc} = 1;
+ }
+ }
+ return $cfg;
}
1;
View
16 lib/CatalystX/Less/Controller/LessCompiler.pm
@@ -4,7 +4,7 @@ use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller' };
-use CSS::LESSp;
+use Spawn::Safe;
use Path::Class::File qw();
=method less file;file2
@@ -14,16 +14,17 @@ compile and concatenate the less into css.
=cut
-sub less_versioned : Path('/static/less') Args(2) {
+sub less_to_css_versioned : Path('/static/less_to_css') Args(2) {
my ($self, $c, $version, $files) = @_;
if ("$version" ne $c->VERSION) {
$c->log->debug("Version missmatch in less compiler, old cached link?");
}
- $c->forward('less', [$files]);
+ $c->forward('less_to_css', [$files]);
}
-sub less : Path('/static/less') Args(1) {
+sub less_to_css : Path('/static/less_to_css') Args(1) {
my ($self, $c, $files) = @_;
+
$files =~ s/\.css$//;
my @files = map { $_ . '.less' } split(";", $files);
# compile and return
@@ -40,20 +41,17 @@ sub less : Path('/static/less') Args(1) {
# If the config is absolute path, leave it alone
$base_folder = $c->path_to($base_folder) unless $base_folder =~ m|^/|;
-
foreach my $file (@files) {
my $full = $self->_find_less_file($c, $file, $base_folder);
unless (-f $full) {
$c->log->warn("LESS: $full not found, skipping");
next;
}
-
- my $in = $full->openr;
- $c->res->print(CSS::LESSp->parse(join("\n", $in->getlines)));
+ my $results = spawn_safe({ argv => ['lessc', $full], timeout => 2 });
+ $c->res->print($results->{stdout});
}
}
-
sub _find_less_file {
my ($self, $c, $file, $base_folder) = @_;
View
4 t/lib-no-version/TestApp/Controller/Root.pm
@@ -11,8 +11,8 @@ sub base : Chained('/') PathPart('') CaptureArgs(0) {}
# your actions replace this one
sub main : Chained('base') PathPart('') Args(0) {
my ($self, $ctx) = @_;
- my $url = $ctx->uri_for_combined_less('base.less', 'fonts.less');
- $ctx->res->body('<link href="' . $url . '" rel="stylesheet"></link><h1>It works</h1>');
+ my $less = $ctx->less_for('base.less', 'fonts.less');
+ $ctx->res->body($less . '<h1>It works</h1>');
}
__PACKAGE__->meta->make_immutable;
View
4 t/lib/TestApp/Controller/Root.pm
@@ -11,8 +11,8 @@ sub base : Chained('/') PathPart('') CaptureArgs(0) {}
# your actions replace this one
sub main : Chained('base') PathPart('') Args(0) {
my ($self, $ctx) = @_;
- my $url = $ctx->uri_for_combined_less('base.less', 'fonts.less');
- $ctx->res->body('<link href="' . $url . '" rel="stylesheet"></link><h1>It works</h1>');
+ my $less = $ctx->less_for('base.less', 'fonts.less');
+ $ctx->res->body($less . '<h1>It works</h1>');
}
__PACKAGE__->meta->make_immutable;
View
9 t/lib2/TestApp/Controller/Root.pm
@@ -4,15 +4,18 @@ use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller' }
-__PACKAGE__->config(namespace => q{});
+__PACKAGE__->config(
+ namespace => q{},
+ less_js_path => "https://github.com/cloudhead/less.js/blob/master/dist/less-1.3.0.min.js"
+);
sub base : Chained('/') PathPart('') CaptureArgs(0) {}
# your actions replace this one
sub main : Chained('base') PathPart('') Args(0) {
my ($self, $ctx) = @_;
- my $url = $ctx->uri_for_combined_less('base.less', 'fonts.less');
- $ctx->res->body('<link href="' . $url . '" rel="stylesheet"></link><h1>It works</h1>');
+ my $less = $ctx->less_for('base.less', 'fonts.less');
+ $ctx->res->body($less . '<h1>It works</h1>');
}
__PACKAGE__->meta->make_immutable;
View
5 t/live-test-maxage.t
@@ -8,12 +8,15 @@ use Test::More;
use FindBin qw($Bin);
use lib "$Bin/lib";
+use Spawn::Safe;
+if(spawn_safe({ argv => [qw{ lessc }], timeout => 2 })->{error}) { plan skip_all => 'Cannot run lessc so tests are meaningless.'; }
+
BEGIN {
$ENV{TESTAPP_CONFIG} = '{ "CatalystX::Less" => { "max_age" => 3600 } }';
};
# make sure testapp works
-use ok 'TestApp';
+use_ok('TestApp');
# a live test against TestApp, the test application
use Test::WWW::Mechanize::Catalyst 'TestApp';
View
31 t/live-test-no-lessc.t
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+
+use Spawn::Safe;
+if(!spawn_safe({ argv => [qw{ lessc }], timeout => 2 })->{error}) { plan skip_all => 'Found lessc so tests are meaningless.'; }
+
+# setup library path
+use FindBin qw($Bin);
+use lib "$Bin/lib2";
+
+BEGIN {
+ $ENV{TESTAPP_CONFIG} = '{ "static" => { "include_path" => ["t/lib2/alt_root", "t/lib2/TestApp/root"] } }';
+};
+
+# make sure testapp works
+use_ok('TestApp');
+
+# a live test against TestApp, the test application
+use Test::WWW::Mechanize::Catalyst 'TestApp';
+
+my $mech = Test::WWW::Mechanize::Catalyst->new;
+
+$mech->get_ok('http://localhost/', 'get main page');
+note($mech->content);
+$mech->content_contains("cloudhead/less.js");
+$mech->follow_link_ok({url_regex => qr/\.less$/}, "Can follow the less link");
+
+done_testing;
View
5 t/live-test-no-version.t
@@ -4,12 +4,15 @@ use strict;
use warnings;
use Test::More;
+use Spawn::Safe;
+if(spawn_safe({ argv => [qw{ lessc }], timeout => 2 })->{error}) { plan skip_all => 'Cannot run lessc so tests are meaningless.'; }
+
# setup library path
use FindBin qw($Bin);
use lib "$Bin/lib-no-version";
# make sure testapp works
-use ok 'TestApp';
+use_ok('TestApp');
# a live test against TestApp, the test application
use Test::WWW::Mechanize::Catalyst 'TestApp';
View
5 t/live-test-static-simple.t
@@ -4,6 +4,9 @@ use strict;
use warnings;
use Test::More;
+use Spawn::Safe;
+if(spawn_safe({ argv => [qw{ lessc }], timeout => 2 })->{error}) { plan skip_all => 'Cannot run lessc so tests are meaningless.'; }
+
# setup library path
use FindBin qw($Bin);
use lib "$Bin/lib2";
@@ -13,7 +16,7 @@ BEGIN {
};
# make sure testapp works
-use ok 'TestApp';
+use_ok('TestApp');
# a live test against TestApp, the test application
use Test::WWW::Mechanize::Catalyst 'TestApp';
View
5 t/live-test-version-path.t
@@ -4,6 +4,9 @@ use strict;
use warnings;
use Test::More;
+use Spawn::Safe;
+if(spawn_safe({ argv => [qw{ lessc }], timeout => 2 })->{error}) { plan skip_all => 'Cannot run lessc so tests are meaningless.'; }
+
# setup library path
use FindBin qw($Bin);
use lib "$Bin/lib";
@@ -13,7 +16,7 @@ BEGIN {
};
# make sure testapp works
-use ok 'TestApp';
+use_ok('TestApp');
# a live test against TestApp, the test application
use Test::WWW::Mechanize::Catalyst 'TestApp';
View
5 t/live-test.t
@@ -4,12 +4,15 @@ use strict;
use warnings;
use Test::More;
+use Spawn::Safe;
+if(spawn_safe({ argv => [qw{ lessc }], timeout => 2 })->{error}) { plan skip_all => 'Cannot run lessc so tests are meaningless.'; }
+
# setup library path
use FindBin qw($Bin);
use lib "$Bin/lib";
# make sure testapp works
-use ok 'TestApp';
+use_ok('TestApp');
# a live test against TestApp, the test application
use Test::WWW::Mechanize::Catalyst 'TestApp';
Something went wrong with that request. Please try again.