Skip to content

Instantly share code, notes, and snippets.

@marcusandre
Last active January 26, 2017 10:26
Show Gist options
  • Save marcusandre/f1c5d07141859bb89594773c4b24b4c3 to your computer and use it in GitHub Desktop.
Save marcusandre/f1c5d07141859bb89594773c4b24b4c3 to your computer and use it in GitHub Desktop.
(WIP) budo middleware which returns transpiled css via postcss (postcss-import and cssnext)
/**
* Module dependencies.
*/
var budo = require('budo')
var fs = require('fs')
var path = require('path')
var postcss = require('postcss')
var cssimport = require('postcss-import')
var cssnext = require('postcss-cssnext')
var cssPath = path.join(__dirname, 'public/css')
var bundlePath = cssPath + '/bundle.css'
// budo options
var opts = {
dir: 'public',
live: true,
stream: process.stdout,
middleware: proxy
}
// start budo
budo(opts)
/**
* Handle static route
*/
function proxy (req, res, next) {
var match = req.url.match(/.css/) !== null
// check if `.css` file
if (match === true) {
// read bundle file
fs.readFile(bundlePath, function (err, data) {
if (err) throw err
// transpile css from read `data`
postcss([cssimport, cssnext])
.process(data, {
from: bundlePath,
map: { inline: true }
})
.then(function (parsed) {
// prepare HTTP <header>
res.writeHead(200, { 'Content-Type': 'text/css' })
// send css as HTTP <body>
res.write(parsed.css)
// end connection
res.end()
})
})
} else {
next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment