|
18 | 18 |
|
19 | 19 | // stdlib
|
20 | 20 | var path = require('path');
|
| 21 | +var fs = require('fs'); |
21 | 22 |
|
22 | 23 |
|
23 | 24 | // 3rd-party
|
@@ -68,10 +69,15 @@ StylusEngine.configure = function (fn) {
|
68 | 69 |
|
69 | 70 | // Render data
|
70 | 71 | StylusEngine.prototype.evaluate = function (context, locals) {
|
| 72 | + var withSourcemap = context.environment.isEnabled('source_maps'); |
| 73 | + |
71 | 74 | var style = stylus(this.data, {
|
72 | 75 | paths: [ path.dirname(this.file) ].concat(context.environment.paths),
|
73 | 76 | filename: this.file,
|
74 |
| - _imports: [] |
| 77 | + _imports: [], |
| 78 | + sourcemap: !withSourcemap ? false : { |
| 79 | + comment: false |
| 80 | + } |
75 | 81 | });
|
76 | 82 |
|
77 | 83 | var error = null,
|
@@ -127,6 +133,37 @@ StylusEngine.prototype.evaluate = function (context, locals) {
|
127 | 133 | }
|
128 | 134 |
|
129 | 135 | 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 | + } |
130 | 167 | };
|
131 | 168 |
|
132 | 169 |
|
|
0 commit comments