Skip to content

Commit d333d83

Browse files
author
Vitaly Puzrin
committed
Added stylus sourcemaps support, closes #170
1 parent d10c1b6 commit d333d83

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `autoprefixer-core` -> `autoprefixer`
66
- Dropped old `autoprefixer` & `csswring` support. Updae those modules, if used.
77
- Fixed source maps `sourcesContent` field for Less 2.x engine.
8+
- Added stylus sourcemaps support.
89
- `fs-tools` -> `mkdirp`.
910

1011

examples/assets/stylesheets/_helper.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$h1-color: aliceblue;
1+
$h1-color: blue;
22

33
body {
44
background: url(image_path("stripes.png"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//
2+
//
3+
//
4+
//
5+
//
6+
h2.styl
7+
color #999
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
//
3+
//
4+
//
5+
@import './_helper_stylus.styl'
6+
7+
h2.styl
8+
background-color #eee

examples/views/layout.jade

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ html(lang='en')
44
title Mincer demo
55
!=stylesheet('app.css')
66
!=stylesheet('alt.css')
7+
!=stylesheet('stylus.css')
78

89
body
910
.container
1011
h1 Mincer
11-
h2 Demo
12+
h2 node-sass style
13+
h2.styl stylus style
14+
1215

1316
!=javascript('app.js')

lib/mincer/engines/stylus_engine.js

+38-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
// stdlib
2020
var path = require('path');
21+
var fs = require('fs');
2122

2223

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

6970
// Render data
7071
StylusEngine.prototype.evaluate = function (context, locals) {
72+
var withSourcemap = context.environment.isEnabled('source_maps');
73+
7174
var style = stylus(this.data, {
7275
paths: [ path.dirname(this.file) ].concat(context.environment.paths),
7376
filename: this.file,
74-
_imports: []
77+
_imports: [],
78+
sourcemap: !withSourcemap ? false : {
79+
comment: false
80+
}
7581
});
7682

7783
var error = null,
@@ -127,6 +133,37 @@ StylusEngine.prototype.evaluate = function (context, locals) {
127133
}
128134

129135
this.data = result;
136+
137+
//
138+
// Now add sourcemap info if needed & available.
139+
//
140+
141+
if (withSourcemap && style.sourcemap) {
142+
var map = style.sourcemap;
143+
var dir = path.dirname(context.pathname);
144+
145+
map.sources.forEach(function (file, idx) {
146+
var rel = path.relative(dir, file);
147+
if (path.sep === '\\') { rel = rel.replace('\\', '/'); }
148+
map.sources[idx] = rel;
149+
});
150+
151+
// Stylus now returns sourcemap without original sources. We should glue
152+
// those manually. See https://github.com/stylus/stylus/issues/2036
153+
if (!map.sourcesContent) {
154+
map.sourcesContent = map.sources.map(function (source) {
155+
var res = '';
156+
157+
try {
158+
res = fs.readFileSync(path.join(dir, source), 'utf8');
159+
} catch (__) {}
160+
161+
return res;
162+
});
163+
}
164+
165+
this.map = JSON.stringify(map);
166+
}
130167
};
131168

132169

0 commit comments

Comments
 (0)