Skip to content
This repository

Now less files with @import 'whatever.less' will work #174

Closed
wants to merge 9 commits into from

9 participants

Hunter Loftis Pau Ramon Revilla Aseem Kishore Christian Joudrey TJ Holowaychuk Isaac Adrian Lang E. Timothy Uy Eneko Alonso
Hunter Loftis

(less.compile takes a stat path that connect wasn't giving to it)

I committed a billion times just to get the quick fix in and pull it into the project that already has @imports... tips on how to compress commits into one easier to read one are welcome ;)

Cheers,
Hunter

Pau Ramon Revilla

+1

I had a similar pull request pending on last connect but never got into the master. I thing is good to have the option to force compilation in development environments.

Aseem Kishore
aseemk commented May 11, 2011

+1

I coincidentally filed this issue with LESS a week ago: less/less.js#255

Here's the super simple test case I wrote: https://gist.github.com/952109

Christian Joudrey

+1

I think this could be generalized by adding compilerOptions to the compiler middleware.

Essentially what we need is a way to pass options to LESS.render.

That being said, this is what I am using in the meantime:

var less = require('less');
var origRender = less.render;
less.render = function(str, options, fn) {
  if (typeof(options) === 'function') {
    fn = options;
    options = { paths: [__dirname + '/public/css'] };
  }
  return origRender.call(this, str, options, fn);
};
TJ Holowaychuk
Collaborator

it's getting a bit ridiculous though :p the more we have to tweak things for specific "compilers" the more I think they should each just supply their own if they wish, stylus was to specific for this middleware as well

Christian Joudrey

If you could abstract the compiler middleware a bit so that we could re-use parts of it, I wouldn't mind writing a LESS compiler middleware.

TJ Holowaychuk
Collaborator

I think we should remove it in 2.0

Isaac

tips on how to compress commits into one easier to read one are welcome

If the commits you want to compress are at the top of the history (i.e. all the latest commits after a certain point) then its easy to squash commits with an interactive rebase.

This destroys history and can go wrong so do it on a branch git checkout -b squash-branch

Start an interactive rebase on the ref of the last commit immediately before your commits git rebase -i ref

If you did your commits on a new branch off master you could supply e.g. master as the ref to the HEAD commit of master, or if you just did your commits on master you could supply a commit ref like 398662d.

Your editor should launch listing the commits. Leave the top commit as pick while changing pick on all other lines to s. Save and quit. The rebase will start, I guess its possible you will need to resolve conflicts but it usually doesn't happen in cases where squashing commits makes sense.

Your editor should launch again once finished to allow you to edit the final squashed commits message.

Golden rule never ever change history that has been shared with others. only do this with private history before you push to github.

Eneko Alonso

Has this code been published on any connect version yet? If so, which one and how do I find that out next time? Thanks!

Adrian Lang

I currently use the following hack to support relative imports:

// Code goes in your app.configure function

// Hack connect.js to allow relative @import statements in less.js
var less = require('less'),
    staticDir = '/static',
    lessDir = staticDir + '/style';
express.compiler.compilers.less.compile = function(str, fn){
    try {
        less.render(str, {paths: [__dirname + lessDir]}, fn);
    } catch (err) {
        fn(err);
    }
};

app.use(lessDir, express.compiler({ src: __dirname + lessDir, enable: ['less'] }));

app.use(staticDir, express['static'](__dirname + staticDir, {maxAge: 1000 * 60 * 60 * 24 * 365}));

I use this way to keep my project compatible with vanilla connect.js.

E. Timothy Uy

@adrianlang I found that less.compile is not being called in your hack. Is there something else that is missing?
@cjoudrey Your fix works on my end. Any changes since 10 mos ago?

TJ Holowaychuk
Collaborator

this middleware is deprecated, there's one specific to less now (from the stylus code) called less-middleware I believe

TJ Holowaychuk visionmedia closed this June 21, 2012
E. Timothy Uy

@visionmedia Thanks! For anyone lese running into this thread, 'less-middleware' does the trick.

sudo npm install less-middleware -g

app.use(require('less-middleware')({ src: __dirname + staticDir}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
14  lib/connect/middleware/compiler.js
@@ -45,6 +45,7 @@ exports = module.exports = function compiler(options){
45 45
 
46 46
     var srcDir = process.connectEnv.compilerSrc || options.src || process.cwd(),
47 47
         destDir = process.connectEnv.compilerDest || options.dest || srcDir,
  48
+        forceCompile = process.connectEnv.forceCompile || options.forceCompile,
48 49
         enable = options.enable;
49 50
 
50 51
     if (!enable || enable.length === 0) {
@@ -79,8 +80,8 @@ exports = module.exports = function compiler(options){
79 80
                                     next(err);
80 81
                                 }
81 82
                             } else {
82  
-                                // Source has changed, compile it
83  
-                                if (srcStats.mtime > destStats.mtime) {
  83
+                                // Source has changed, compile it. can optionally be forced to compile
  84
+                                if (forceCompile || srcStats.mtime > destStats.mtime) {
84 85
                                     compile();
85 86
                                 } else {
86 87
                                     // Defer file serving
@@ -97,6 +98,7 @@ exports = module.exports = function compiler(options){
97 98
                         if (err) {
98 99
                             next(err);
99 100
                         } else {
  101
+                            var options = {paths: [path.dirname(src)]}
100 102
                             compiler.compile(str, function(err, str){
101 103
                                 if (err) {
102 104
                                     next(err);
@@ -105,7 +107,7 @@ exports = module.exports = function compiler(options){
105 107
                                         next(err);
106 108
                                     });
107 109
                                 }
108  
-                            });
  110
+                            }, options);
109 111
                         }
110 112
                     });
111 113
                 }
@@ -140,10 +142,12 @@ var compilers = exports.compilers = {
140 142
     less: {
141 143
         match: /\.css$/,
142 144
         ext: '.less',
143  
-        compile: function(str, fn){
  145
+        compile: function(str, fn, options){
  146
+            console.log("Sending paths to less.render() as:")
  147
+            console.dir(options)
144 148
             var less = cache.less || (cache.less = require('less'));
145 149
             try {
146  
-                less.render(str, fn);
  150
+                less.render(str, options, fn);
147 151
             } catch (err) {
148 152
                 fn(err);
149 153
             }
4  lib/connect/middleware/gzip.js
... ...
@@ -1,8 +1,10 @@
1 1
 try {
2 2
     module.exports = require('./gzip-compress');
3 3
 } catch (e) {
4  
-    if (/^Cannot find module /.test(e.message))
  4
+    if (/^Cannot find module /.test(e.message)) {
  5
+        console.warn("Can't find fast C-based gzip-compress module, using slow as hell JS version instead. Install the node-compress library!")
5 6
         module.exports = require('./gzip-proc');
  7
+    }
6 8
     else
7 9
         throw e;
8 10
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.