Created
July 14, 2017 16:45
-
-
Save davidrleonard/b7fba899e0f63c7cdd80cca37a7e1cce to your computer and use it in GitHub Desktop.
Sass->CSS build task that can be run as an npm script (not done)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const path = require('path'); | |
const fs = require('fs'); | |
const glob = require('glob'); | |
const sass = require('node-sass'); | |
const importOnce = require('node-sass-import-once'); | |
const postcss = require('postcss'); | |
const autoprefixer = require('autoprefixer'); | |
const cssmin = require('clean-css'); | |
function getFileNames(matching) { | |
return new Promise((resolve, reject) => { | |
glob(matching, {}, (err, files) => { | |
if (err) reject(err); | |
resolve(files); | |
}); | |
}); | |
}; | |
function fileNamesToPaths(fileNames) { | |
return Promise.resolve( | |
fileNames.map(fileName => path.join(process.cwd(), fileName)) | |
); | |
}; | |
function forAllFiles(files, fn) { | |
return Promise.all( | |
files.map(fn) | |
); | |
}; | |
function read(filePath) { | |
return new Promise((resolve, reject) => { | |
fs.readFile(filePath, 'utf8', (err, text) => { | |
if (err) reject(err); | |
resolve({ | |
filePath: filePath, | |
text: text | |
}); | |
}); | |
}); | |
}; | |
const sassOptions = text => ({ | |
data: text, | |
importer: importOnce, | |
importOnce: { | |
index: true, | |
bower: true | |
} | |
}); | |
function compile(file) { | |
return new Promise((resolve, reject) => { | |
sass.render(sassOptions(file.text), (err, compiled) => { | |
if (err) reject(err); | |
resolve({ | |
filePath: file.filePath, | |
text: compiled.css.toString() | |
}); | |
}); | |
}); | |
}; | |
function clean(file) { | |
return postcss([autoprefixer, cssmin]).then((result)) | |
}; | |
getFileNames('./sass/**.scss') | |
.then(fileNamesToPaths) | |
.then(files => forAllFiles(files, read)) | |
.then(files => forAllFiles(files, compile)) | |
.then(files => forAllFiles(files, clean)) | |
.then(files => console.log(files[0])) | |
// .then(compileAllFiles) | |
// .then(files => console.log(files[0])) | |
// .catch((e) => console.log(e)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment