Skip to content

Commit

Permalink
Added stylus sourcemaps support, closes #170
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Oct 24, 2015
1 parent d10c1b6 commit d333d83
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -5,6 +5,7 @@
- `autoprefixer-core` -> `autoprefixer`
- Dropped old `autoprefixer` & `csswring` support. Updae those modules, if used.
- Fixed source maps `sourcesContent` field for Less 2.x engine.
- Added stylus sourcemaps support.
- `fs-tools` -> `mkdirp`.


Expand Down
2 changes: 1 addition & 1 deletion examples/assets/stylesheets/_helper.scss
@@ -1,4 +1,4 @@
$h1-color: aliceblue;
$h1-color: blue;

body {
background: url(image_path("stripes.png"));
Expand Down
7 changes: 7 additions & 0 deletions examples/assets/stylesheets/_helper_stylus.styl
@@ -0,0 +1,7 @@
//
//
//
//
//
h2.styl
color #999
8 changes: 8 additions & 0 deletions examples/assets/stylesheets/stylus.css.styl
@@ -0,0 +1,8 @@
//
//
//
//
@import './_helper_stylus.styl'

h2.styl
background-color #eee
5 changes: 4 additions & 1 deletion examples/views/layout.jade
Expand Up @@ -4,10 +4,13 @@ html(lang='en')
title Mincer demo
!=stylesheet('app.css')
!=stylesheet('alt.css')
!=stylesheet('stylus.css')

body
.container
h1 Mincer
h2 Demo
h2 node-sass style
h2.styl stylus style


!=javascript('app.js')
39 changes: 38 additions & 1 deletion lib/mincer/engines/stylus_engine.js
Expand Up @@ -18,6 +18,7 @@

// stdlib
var path = require('path');
var fs = require('fs');


// 3rd-party
Expand Down Expand Up @@ -68,10 +69,15 @@ StylusEngine.configure = function (fn) {

// Render data
StylusEngine.prototype.evaluate = function (context, locals) {
var withSourcemap = context.environment.isEnabled('source_maps');

var style = stylus(this.data, {
paths: [ path.dirname(this.file) ].concat(context.environment.paths),
filename: this.file,
_imports: []
_imports: [],
sourcemap: !withSourcemap ? false : {
comment: false
}
});

var error = null,
Expand Down Expand Up @@ -127,6 +133,37 @@ StylusEngine.prototype.evaluate = function (context, locals) {
}

this.data = result;

//
// Now add sourcemap info if needed & available.
//

if (withSourcemap && style.sourcemap) {
var map = style.sourcemap;
var dir = path.dirname(context.pathname);

map.sources.forEach(function (file, idx) {
var rel = path.relative(dir, file);
if (path.sep === '\\') { rel = rel.replace('\\', '/'); }
map.sources[idx] = rel;
});

// Stylus now returns sourcemap without original sources. We should glue
// those manually. See https://github.com/stylus/stylus/issues/2036
if (!map.sourcesContent) {
map.sourcesContent = map.sources.map(function (source) {
var res = '';

try {
res = fs.readFileSync(path.join(dir, source), 'utf8');
} catch (__) {}

return res;
});
}

this.map = JSON.stringify(map);
}
};


Expand Down

0 comments on commit d333d83

Please sign in to comment.