-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrenderSass.js
More file actions
58 lines (50 loc) · 1.81 KB
/
Copy pathrenderSass.js
File metadata and controls
58 lines (50 loc) · 1.81 KB
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
const path = require('path');
const sass = require('node-sass');
const mkFullPathSync = require('./mkFullPathSync');
const writeFile = require('./writeFile');
const { log } = require('./log');
module.exports = function renderSass(dest, file, config, cb, write = false) {
// extract sass only configs
const { outputStyle, includePaths, failOnError } = config.sass;
// proper extension
const destFile = dest.replace('.scss', '.css');
// url of the saved file
const outFile = path.join(config.general.destinationPath, destFile);
// extract from config
sass.render({
file,
outputStyle,
includePaths,
outFile,
sourceMap: !config.general.isProduction
}, (error, result) => {
// log if there are any errors
if (error) {
log(__filename, `${destFile} ${error.message}!`, '', 'error', true);
// if set to exit on error, you might not want to exit on all cases
if (failOnError) {
process.exit(1);
} else {
log(__filename, `Sass file not renderd - ${path.basename(destFile)}`, ``, 'error', true);
// skip the rest
return;
}
}
// create folder if it does not exist
mkFullPathSync(path.dirname(outFile));
// write the css file, overriding it if write is enabled
// its better to skip this so we only write the css once reducing I/O
if (write) {
writeFile(outFile, result.css, true);
}
// if is dev add source maps
if (!config.general.isProduction && write) {
writeFile(`${outFile}.map`, result.map, true);
}
// pass the destination relative for map
result.destFile = destFile;
// log and call back
log(__filename, `Sass rendered - ${path.basename(destFile)}`, ` (Duration ${result.stats.duration}ms)`, 'success', true);
return cb(result, outFile);
});
};