forked from cferdinandi/build-tool-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sass.js
78 lines (66 loc) · 2.34 KB
/
sass.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
var sass = require('sass');
var fs = require('fs');
var pkg = require('./package.json');
// Configs
var configs = {
name: 'BuildToolsCookbook',
files: ['main.scss'],
pathIn: 'src/scss',
pathOut: 'dist/css',
indentType: 'tab',
indentWidth: 1,
minify: true,
sourceMap: false
};
// Banner
var banner = `/*! ${configs.name ? configs.name : pkg.name} v${pkg.version} | (c) ${new Date().getFullYear()} ${pkg.author.name} | ${pkg.license} License | ${pkg.repository.url} */`;
var getOptions = function (file, filename, minify) {
return {
file: `${configs.pathIn}/${file}`,
outFile: `${configs.pathOut}/${filename}`,
sourceMap: configs.sourceMap,
sourceMapContents: configs.sourceMap,
indentType: configs.indentType,
indentWidth: configs.indentWidth,
outputStyle: minify ? 'compressed' : 'expanded'
};
};
var writeFile = function (pathOut, fileName, fileData, printBanner = true) {
// Create the directory path
fs.mkdir(pathOut, { recursive: true }, function (err) {
// If there's an error, throw it
if (err) throw err;
// Write the file to the path
fs.writeFile(`${pathOut}/${fileName}`, fileData, function (err) {
if (err) throw err;
var data = fs.readFileSync(`${pathOut}/${fileName}`);
var fd = fs.openSync(`${pathOut}/${fileName}`, 'w+');
var insert = printBanner ? new Buffer.from(banner + '\n') : '';
fs.writeSync(fd, insert, 0, insert.length, 0);
fs.writeSync(fd, data, 0, data.length, insert.length);
fs.close(fd, function (err) {
if (err) throw err;
console.log(`Compiled ${pathOut}/${fileName}`);
})
})
})
}
var parseSass = function (file, minify) {
var filename = `${file.slice(0, file.length - 5)}${minify ? '.min' : ''}.css`;
sass.render(getOptions(file, filename, minify), function (err, result) {
// If there's an error, throw it
if (err) throw err;
// Write the file
writeFile(configs.pathOut, filename, result.css);
if (configs.sourceMap && !configs.sourceMapEmbed) {
// Write external sourcemap
writeFile(configs.pathOut, filename + '.map', result.map, false);
}
});
};
configs.files.forEach(function (file) {
parseSass(file);
if (configs.minify) {
parseSass(file, true);
}
});