Skip to content

Instantly share code, notes, and snippets.

@serby
Forked from bengourley/piler.js
Created November 10, 2012 09:07
Show Gist options
  • Save serby/4050508 to your computer and use it in GitHub Desktop.
Save serby/4050508 to your computer and use it in GitHub Desktop.
var stylus = require('stylus')
, nib = require('nib')
, uglify = require('uglifyjs')
function processStylus(input, cb) {
fs.readFile(input.src, function (err, data) {
if (err) return cb(err)
stylus
.use(nib())
.set('compress', true)
.set('more options', { foo: 1 })
.define('my function', function () { /* ... */ })
.compile(data, function (err, css) {
if (err) return cb(err)
fs.writeFile(input.dest, function (err) {
if (err) return cb(err)
cb(null, 'yay')
})
})
}
function processJS(input, cb) {
// The string of concated js
var out = ''
function write() {
fs.writeFile(input.dest, out, function (err) {
if (err) return cb(err)
cb(null, 'yay')
})
}
// Determine whether to bundle or provide loader
if (someFlag) {
// This makes the assumption that the paths in srcs are the
// the same as the public available web path. (but they are not)
out += buildLoader(input.srcs)
} else {
var done = 0
input.srcs.forEach(function (src) {
fs.readFile(src, function (err, data) {
out += uglify(data)
if (++done === input.srcs.length) write()
})
})
}
}
// admin bundle - piler.js
module.exports = function(piler) {
// Define custom processors. min and watch could be built in.
var processStylus = require('processStylus')
, processJs = require('processJs')
var buildCss = piler.task.buildCss = function(done) {
processStylus(
{ src: 'public/css/screen.styl'
, dest: 'public/css/screen.css'
}, done)
}
piler.task.buildJavascript = function(done) {
processJs(
[ { dest: 'public/js/app.js'
, srcs: [ 'public/js/a.js', 'public/js/b.js' ]
}
], done)
}
piler.task.watchCss = piler.watch.bind(null, 'public/css/**/*.styl', buildCss)
}
// global piler.js
module.exports = function (piler) {
piler.loadTasks('./bundles/**/piler.js')
// Define single tasks for build etc
piler.task('build', function(done) {
piler.tasks.version(function(error) {
if (error) {
return done(error)
}
piler.tasks.buildCss(function(error) {
if (error) {
return done(error)
}
piler.tasks.buildJavascript(done)
})
})
})
// or possibly more simply
piler.task.build = function() {
piler.seq([
piler.task.version,
piler.task.buildCss,
piler.task.buildJavascript
])
}
}
// cli
// Watch for changes and call tasks
$ piler watch
// Run all stylus tasks
$ piler stylus
// Build project
$ piler build
@serby
Copy link
Author

serby commented Nov 10, 2012

@bengourley there will be a config per bundle so it is quite hard to reference the other build tasks. This is a problem for both string based invocation and anonymous function wrapped functions. I don't really want to assign to a variable before passing them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment