Skip to content
This repository

Allow for more complex source and destination paths through middleware. #679

Closed
wants to merge 1 commit into from

4 participants

David Bainbridge Jan Buschtöns TJ Holowaychuk Roman Komarov
David Bainbridge

Support for a 'base' directory which is used to construct the path accessed via a request through through the middleware.

app.use(stylus.middleware({
    src: __dirname + '/views'
  , dest: __dirname + '/public/stylesheets'
  , base: __dirname + '/public'
  , compile: compile
}));
.
.
app.use(express.static(__dirname + '/public'));

given a main.styl file located in /views/stylesheets; can reference it via:

link(rel='stylesheet', href='/stylesheets/main.css')

These changes should not break any existing code.

David Bainbridge Allow for more complex source and destination paths through middleware.
Support for a 'base' directory which is used to construct the path accessed via a request through through the middleware.

app.use(stylus.middleware({
    src: __dirname + '/views'
  , dest: __dirname + '/public/stylesheets'
  , base: __dirname + '/public'
  , compile: compile
}));
.
.
app.use(express.static(__dirname + '/public'));

given a main.styl file located in /views; can reference it via:
link(rel='stylesheet', href='/stylesheets/main.css')
5300869
Jan Buschtöns

+1 This should really be implemented. I just spent 30 minutes finding out, why I can't get my precious /css/reset.css.

@visionmedia When will this be merged?

TJ Holowaychuk

i still haven't tried the patch. at a glance it looks like it simply replaces the var but wouldn't work correctly

Jan Buschtöns

It works good for the dest parameter. Yet it does not alter the src parameter.

Something like this would be awesome:

app.use(stylus.middleware({
    static: __dirname + "/public"
  , routes: {
        __dirname + "/styles": "css"
      , __dirname + "/some-framework/styles": "framework/css"
    }
}));

Stylus would only handle requests that go to the folders /css (/public/css) and /framework/css (/public/framework/css). This could be a possible solution. The main thing that annoys me is that I'm currently unable to keep my .css an .styl seperated like so:

/app
  /views
    index.jade
  /styles
    index.styl
  /public
    /css
      index.css
David Bainbridge

The change was to only affect the dest since that is the case that is the most annoying and could directly affect what the end-user sees. The patch was also done to be the least intrusive and not break any currently working code hence adding only a 'base'.. which should have been 'destBase'. It is by no means perfect but I think addresses the largest use case.

Roman Komarov
Collaborator
kizu commented March 01, 2014

Thank you for your PR! However, a lot of time passed since this PR was opened, so now it needs to be rebased to the master.

Could you look at this issue once again? There is a chance it was fixed somewhere on the road, but if it is not, feel free to rebase your patch to the master. Otherwise, in a month or so I could close it, to keep our PR queue clean.

Roman Komarov
Collaborator
kizu commented April 06, 2014

As I wrote in the previous comment, I'm closing this PR to keep PR queue clean. If you'd find time and rebase your PR to the master, we would welcome a new PR.

Anyway, thank you for the work you've done there!

Roman Komarov kizu closed this April 06, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 17, 2012
David Bainbridge Allow for more complex source and destination paths through middleware.
Support for a 'base' directory which is used to construct the path accessed via a request through through the middleware.

app.use(stylus.middleware({
    src: __dirname + '/views'
  , dest: __dirname + '/public/stylesheets'
  , base: __dirname + '/public'
  , compile: compile
}));
.
.
app.use(express.static(__dirname + '/public'));

given a main.styl file located in /views; can reference it via:
link(rel='stylesheet', href='/stylesheets/main.css')
5300869
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 8 additions and 1 deletion. Show diff stats Hide diff stats

  1. 9  lib/middleware.js
9  lib/middleware.js
@@ -32,6 +32,8 @@ var imports = {};
32 32
  *    `src`       Source directory used to find .styl files
33 33
  *    `dest`      Destination directory used to output .css files
34 34
  *                when undefined defaults to `src`.
  35
+ *    `base`      Base directory used when requesting .css files
  36
+ *                when undefined defaults to `dest`.   
35 37
  *    `compile`   Custom compile function, accepting the arguments
36 38
  *                `(str, path)`.
37 39
  *    `compress`  Whether the output .css files should be compressed
@@ -95,6 +97,11 @@ module.exports = function(options){
95 97
     ? options.dest
96 98
     : src;
97 99
 
  100
+// Default base dir to dest
  101
+var base = options.base
  102
+    ? options.base
  103
+    : dest;
  104
+
98 105
   // Default compile callback
99 106
   options.compile = options.compile || function(str, path){
100 107
     return stylus(str)
@@ -109,7 +116,7 @@ module.exports = function(options){
109 116
     if ('GET' != req.method && 'HEAD' != req.method) return next();
110 117
     var path = url.parse(req.url).pathname;
111 118
     if (/\.css$/.test(path)) {
112  
-      var cssPath = join(dest, path)
  119
+      var cssPath = join(base, path)
113 120
         , stylusPath = join(src, path.replace('.css', '.styl'));
114 121
 
115 122
       // Ignore ENOENT to fall through as 404
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.