This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
index.js
317 lines (254 loc) · 6.26 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
var fs = require('fs'),
path = require('path');
/**
* Get binding
*
* @api private
*/
function getBinding() {
var name = process.platform + '-' + process.arch;
var candidates = [
path.join(__dirname, '..', 'build', 'Release', 'binding.node'),
path.join(__dirname, '..', 'build', 'Debug', 'binding.node'),
path.join(__dirname, '..', 'vendor', name, 'binding.node')
];
var candidate = candidates.filter(fs.existsSync)[0];
if (!candidate) {
throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
}
return candidate;
}
/**
* Get outfile
*
* @param {Object} options
* @api private
*/
function getOutFile(options) {
var file = options.file;
var outFile = options.outFile;
if (!file || !outFile || typeof outFile !== 'string' || typeof file !== 'string') {
return false;
}
if (path.resolve(outFile) !== path.normalize(outFile).replace(new RegExp(path.sep + '$'), '')) {
return false;
}
return path.resolve(path.dirname(file), outFile);
}
/**
* Get stats
*
* @param {Object} options
* @api private
*/
function getStats(options) {
var stats = options.stats;
stats.entry = options.file || 'data';
stats.start = Date.now();
return stats;
}
/**
* End stats
*
* @param {Object} options
* @param {Object} sourceMap
* @api private
*/
function endStats(options, sourceMap) {
var stats = options.stats || {};
stats.end = Date.now();
stats.duration = stats.end - stats.start;
stats.sourceMap = sourceMap;
return stats;
}
/**
* Get style
*
* @param {Object} options
* @api private
*/
function getStyle(options) {
var style = options.output_style || options.outputStyle;
var styles = {
nested: 0,
expanded: 1,
compact: 2,
compressed: 3
};
return styles[style];
}
/**
* Get source map
*
* @param {Object} options
* @api private
*/
function getSourceMap(options) {
var file = options.file;
var outFile = options.outFile;
var sourceMap = options.sourceMap;
if (sourceMap) {
if (typeof sourceMap !== 'string') {
sourceMap = outFile ? outFile + '.map' : '';
} else if (outFile) {
sourceMap = path.resolve(path.dirname(file), sourceMap);
}
}
return sourceMap;
}
/**
* Get options
*
* @param {Object} options
* @api private
*/
function getOptions(options) {
options = options || {};
options.comments = options.source_comments || options.sourceComments || false;
options.data = options.data || null;
options.file = options.file || null;
options.imagePath = options.image_path || options.imagePath || '';
options.outFile = getOutFile(options) || null;
options.paths = (options.include_paths || options.includePaths || []).join(path.delimiter);
options.precision = parseInt(options.precision) || 5;
options.sourceMap = getSourceMap(options);
options.stats = options.stats || {};
options.style = getStyle(options) || 0;
if (options.imagePath && typeof options.imagePath !== 'string') {
throw new Error('`imagePath` needs to be a string');
}
getStats(options);
var error = options.error;
var success = options.success;
options.error = function(err, code) {
if (error) {
error(err, code);
}
};
options.success = function(css, sourceMap) {
endStats(options, sourceMap);
if (success) {
success(css, sourceMap);
}
};
delete options.image_path;
delete options.include_paths;
delete options.includePaths;
delete options.source_comments;
delete options.sourceComments;
return options;
}
/**
* Require binding
*/
var binding = require(getBinding());
/**
* Render (deprecated)
*
* @param {String} css
* @param {Function} cb
* @param {Object} options
* @api private
*/
function deprecatedRender(css, cb, options) {
options = getOptions(options);
options.data = css;
options.error = cb;
options.success = function(css) {
cb(null, css);
};
binding.render(options);
}
/**
* Render sync (deprecated)
*
* @param {String} css
* @param {Object} options
* @api private
*/
function deprecatedRenderSync(css, options) {
options = getOptions(options);
options.data = css;
return binding.renderSync(options);
}
/**
* Render
*
* @param {Object} options
* @api public
*/
module.exports.render = function(options) {
if (typeof arguments[0] === 'string') {
return deprecatedRender.apply(this, arguments);
}
options = getOptions(options);
options.file ? binding.renderFile(options) : binding.render(options);
};
/**
* Render sync
*
* @param {Object} options
* @api public
*/
module.exports.renderSync = function(options) {
if (typeof arguments[0] === 'string') {
return deprecatedRenderSync.apply(this, arguments);
}
var output;
options = getOptions(options);
output = options.file ? binding.renderFileSync(options) : binding.renderSync(options);
endStats(options, options.stats.sourceMap);
return output;
};
/**
* Render file
*
* `options.sourceMap` can be used to specify that the source map should be saved:
*
* - If falsy the source map will not be saved
* - If `options.sourceMap === true` the source map will be saved to the
* standard location of `options.file + '.map'`
* - Else `options.sourceMap` specifies the path (relative to the `outFile`)
* where the source map should be saved
*
* @param {Object} options
* @api public
*/
module.exports.renderFile = function(options) {
options = options || {};
var outFile = options.outFile;
var success = options.success;
if (options.sourceMap === true) {
options.sourceMap = outFile + '.map';
}
options.success = function(css, sourceMap) {
fs.writeFile(outFile, css, function(err) {
if (err) {
return options.error(err);
}
if (!options.sourceMap) {
return success(outFile);
}
var dir = path.dirname(outFile);
var sourceMapFile = path.resolve(dir, options.sourceMap);
fs.writeFile(sourceMapFile, sourceMap, function(err) {
if (err) {
return options.error(err);
}
success(outFile, sourceMapFile);
});
});
};
module.exports.render(options);
};
/**
* Middleware
*
* @api public
*/
module.exports.middleware = function() {
return new Error([
'The middleware has been moved to',
'https://github.com/sass/node-sass-middleware'
].join(' '));
};