-
Notifications
You must be signed in to change notification settings - Fork 7
/
sass-builder.js
73 lines (64 loc) · 2.2 KB
/
sass-builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// TODO: or use more efficient node-sass (issues with installing it via jspm)
var Sass = require('sass.js/dist/sass.sync');
// TODO: allow building as external file
// Turn on source maps
// FIXME: embedded vs as external file?
Sass.options({
// Embed included contents in maps
sourceMapContents: true,
// Embed sourceMappingUrl as data uri
sourceMapEmbed: true,
// Disable sourceMappingUrl in css output
sourceMapOmitUrl: false
});
// Resolve @imports
var fs = require('fs');
Sass.importer(function(request, done) {
var url = request.current;
fs.readFile(url, {encoding: 'UTF-8'}, function(err, text) {
if (err) {
done({path: url, error: err.message});
} else {
done({path: url, content: text});
}
});
});
// FIXME: avoid copying this boilerplate from the plugin-css...
function escape(source) {
return source
.replace(/(["\\])/g, '\\$1')
.replace(/[\f]/g, "\\f")
.replace(/[\b]/g, "\\b")
.replace(/[\n]/g, "\\n")
.replace(/[\t]/g, "\\t")
.replace(/[\r]/g, "\\r")
.replace(/[\u2028]/g, "\\u2028")
.replace(/[\u2029]/g, "\\u2029");
}
// FIXME: use link rel=stylesheet to make sourcemaps work?
var cssInject = "(function(c){var d=document,a='appendChild',i='styleSheet',s=d.createElement('style');s.type='text/css';d.getElementsByTagName('head')[0][a](s);s[i]?s[i].cssText=c:s[a](d.createTextNode(c));})";
function compileSass(source, url) {
return new Promise(function(resolve, reject) {
Sass.compile(source, {
inputPath: url.replace('file://', '')
}, function(result) {
var successful = result.status === 0;
if (successful) {
resolve(result.text);
} else {
reject(result.formatted);
}
});
});
}
module.exports = function(loads, opts) {
var stubDefines = loads.map(function(load) {
return (opts.systemGlobal || 'System') + ".register('" + load.name + "', [], false, function() {});";
}).join('\n');
return Promise.all(loads.map(function(load) {
return compileSass(load.source, load.address);
})).then(function(compiledCss) {
var cssOutput = compiledCss.join('\n');
return [stubDefines, cssInject, '("' + escape(cssOutput) + '");'].join('\n');
});
};