Permalink
Browse files

Added css and stylus support

  • Loading branch information...
Qard committed Jan 26, 2012
1 parent 7b2b271 commit fb5fba3a81f2cec39f876f9bc5910ba3dd8fada3
Showing with 89 additions and 13 deletions.
  1. BIN .DS_Store
  2. +8 −2 README.md
  3. +80 −10 lib/index.js
  4. +1 −1 package.json
View
BIN .DS_Store
Binary file not shown.
View
@@ -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+
@@ -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
View
@@ -6,6 +6,7 @@ var helper = require('./helper')
, jsp = uglify.parser
, pro = uglify.uglify
, coffee = null
+ , stylus = null
/**
* Construct a Crsh block
@@ -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
@@ -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
@@ -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)
@@ -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) {
@@ -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)
View
@@ -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"
},

0 comments on commit fb5fba3

Please sign in to comment.