-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlasso-sass-plugin.js
98 lines (78 loc) · 2.87 KB
/
lasso-sass-plugin.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
var sass = null;
var sassPath = null;
try {
sassPath = require.resolve('sass');
} catch(e) {}
if (sassPath) {
sass = require(sassPath);
}
var nodePath = require('path');
var extend = require('raptor-util/extend');
module.exports = function(lasso, config) {
var sassHandler = {
properties: {
path: 'string',
paths: 'string',
virtualPath: 'string',
code: 'string',
external: 'boolean'
},
init: function(lassoContext, callback) {
if (!sass) {
var missingSassError = new Error('Unable to handle Sass dependency for path "' + this.path + '". The "node-sass" module was not found. This module should be installed as a top-level application dependency using "npm install node-sasss --save".');
if (callback) return callback(missingSassError);
throw missingSassError;
}
var path = this.path;
if (path || this.code) {
if (path) {
this.path = this.resolvePath(path);
}
} else {
var pathError = new Error('"path" or "code" is required');
if (callback) return callback(pathError);
throw pathError;
}
if (callback) callback();
},
read: function(lassoContext, callback) {
return new Promise((resolve, reject) => {
callback = callback || function (err, res) {
return err ? reject(err) : resolve(res);
};
var path = this.path;
var renderOptions = extend({}, config);
if (this.code) {
renderOptions.data = this.code;
} else if (path) {
renderOptions.file = path;
} else {
return callback(new Error('Invalid sass dependency. No path or code'));
}
sass.render(renderOptions, function(error, result) {
if (error) {
callback(error);
} else {
callback(null, result.css);
}
});
});
},
getDir: function() {
if (this.dir) {
return this.dir;
}
var path = this.path || this.virtualPath;
return path ? nodePath.dirname(path) : undefined;
},
getLastModified: function(lassoContext, callback) {
return callback ? callback(null, -1) : -1;
},
calculateKey: function() {
return 'less:' + (this.code || this.virtualPath || this.path);
}
};
lasso.dependencies.registerStyleSheetType('scss', sassHandler);
lasso.dependencies.registerStyleSheetType('sass', sassHandler);
};