Skip to content

Commit

Permalink
Added css and stylus support
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed Jan 26, 2012
1 parent 7b2b271 commit fb5fba3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
Binary file added .DS_Store
Binary file not shown.
10 changes: 8 additions & 2 deletions README.md
@@ -1,5 +1,5 @@
# CRSH your javascript into tiny blocks.
With crsh, you can combine a whole bunch of javascript (or coffeescript!) files into one tiny block. Or several if you like. All files are compiled and merged automagically after each change, but not when in production mode. Inversely; the resulting code is uglified, but only when IN production. Line numbers are handy in development.
# CRSH your javascript and css into tiny blocks.
With crsh, you can combine a whole bunch of javascript and coffeescript or css and stylus files into one tiny block. Or several if you like. All files are compiled and merged automagically after each change, but the watcher is disabled in production. In production, resulting javascript will also be uglified.

## Requirements
* Node.js 0.4+
Expand Down Expand Up @@ -82,6 +82,12 @@ Gets a handy url fragment containing the name of the block file and a modificati
### Crsh.middleware([base_path], blockDefs)
For each item in blockDefs, constructs a new Crsh instance and exposes it in req.crsh[name]. Also creates a "crsh_{name}" properties in view locals.

### Crsh.getFormat(filename_or_path)
Will return "js" if the extension is .js or .coffee, will return .css if the extension is .css or .styl and will otherwise return false.

### Crsh.isJs|isCss(filename_or_path)
Convenient aliases to Crsh.getFormat to determine if the format is what you expect.

---

### Copyright (c) 2012 Stephen Belanger
Expand Down
90 changes: 80 additions & 10 deletions lib/index.js
Expand Up @@ -6,6 +6,7 @@ var helper = require('./helper')
, jsp = uglify.parser
, pro = uglify.uglify
, coffee = null
, stylus = null

/**
* Construct a Crsh block
Expand All @@ -16,6 +17,8 @@ var helper = require('./helper')
function Crsh (path, list) {
var self = this

this.format = null

// Path is optional
if (typeof path !== 'string') {
list = path
Expand Down Expand Up @@ -59,22 +62,73 @@ Crsh.path = process.cwd()
*/
Crsh.prototype.add = function (f) {
f = helper.realPath(f, this.path)
var format = Crsh.getFormat(f)

// If we add any coffee files, require coffee-script
if (path.extname(f) === '.coffee') {
coffee = require('coffee-script')
// Mixing formats is bad
if (this.format && this.format !== format) {
throw new Error('Can not mix formats!')
}

// Add to list and rebuild name
// Make sure format is set
this.format || (this.format = format)

// Ensure necessary parsers are available
this.prepParsers(f)

// Add to list
this.list.push(f)
this.name = 'crsh-' + helper.md5(this.list.join(',')) + '.js'

// Add to watcher
this.watcher && this.watcher.add(f)

return this
}

/**
* Determines if a parser library is need and loads it
*
* @param string
* File name or path
*/
Crsh.prototype.prepParsers = function (file) {
var ext = path.extname(file)

// Needs coffeescript
if ('.coffee' === ext) {
try {
coffee = require('coffee-script')
} catch (e) {
throw new Error('coffee-script not installed')
}
}

// Needs stylus
if ('.styl' === ext) {
try {
stylus = require('stylus')
} catch (e) {
throw new Error('stylus not installed')
}
}
}

/**
* Attempt to identify file type
*
* @param string
* File name or path
*/
Crsh.getFormat = function (file) {
var ext = path.extname(file)
return ['.js','.coffee'].indexOf(ext) >= 0 ? 'js'
: ['.css','.styl'].indexOf(ext) >= 0 ? 'css'
: false
}

// Aliases for getFormat
Crsh.isJs = function (f) { return 'js' === Crsh.getFormat(f) }
Crsh.isCss = function (f) { return 'css' === Crsh.getFormat(f) }


/**
* Remove a file from the watcher
Expand All @@ -85,12 +139,11 @@ Crsh.prototype.add = function (f) {
Crsh.prototype.remove = function (f) {
f = helper.realPath(f, this.path)

// Remove from list and rebuild name
// Remove from list
var p = this.list.indexOf(f)
if (p > -1) {
this.list.splice(p, 1)
}
this.name = 'crsh-' + helper.md5(this.list.join(',')) + '.js'

// Remove from watcher
this.watcher && this.watcher.remove(f)
Expand All @@ -103,10 +156,14 @@ Crsh.prototype.remove = function (f) {
* Merge and minify all the files
*/
Crsh.prototype.compile = function (cb) {
var self = this
var isProd = process.env.NODE_ENV === 'production'
, list = this.list
, self = this

cb || (cb = function () {})

// Generate name only when a compile occurs
this.name = 'crsh-' + helper.md5(this.list.join(',')) + '.' + this.format

// Load all the files into a hash
Files.load(list, function (err, files) {
Expand All @@ -120,12 +177,25 @@ Crsh.prototype.compile = function (cb) {
return coffee.compile(files[file].toString())
}

// Return regular javascript as-is
// Compile stylus files
if (path.extname(file) === '.styl') {
var css = ''
stylus(files[file].toString())
.set('compress', isProd)
.set('filename', file)
.render(function (err, v) {
css = v
})

return css
}

// Return normal files as-is
return files[file]
}).join('')

// Only uglify in production
if (process.env.NODE_ENV === 'production') {
if (isProd && this.format === 'js') {
var ast = jsp.parse(data)
ast = pro.ast_mangle(ast)
ast = pro.ast_squeeze(ast)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Stephen Belanger <admin@stephenbelanger.com> (http://stephenbelanger.com)",
"name": "crsh",
"description": "crsh your javascript into tiny blocks.",
"version": "0.0.6",
"version": "0.1.0",
"repository": {
"url": "git://github.com/Qard/crsh.git"
},
Expand Down

0 comments on commit fb5fba3

Please sign in to comment.