Skip to content

Commit

Permalink
Enforce folder separation to keep .js result files elsewhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed Aug 29, 2011
1 parent ea252e5 commit 4bdf8d2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 60 deletions.
4 changes: 3 additions & 1 deletion README.md
@@ -1,6 +1,8 @@
# express-coffee
Express-coffee is an express middleware that automatically compiles and serves coffeescript files.

## WARNING: 0.0.2 has breaking changes. If you use this, update your code.

## Requirements
* Node.js 0.4+
* Coffeescript
Expand All @@ -19,7 +21,7 @@ Express-coffee is an express middleware that automatically compiles and serves c
}));

#### definition.path = (string)
The path variable is the path to your static files. Basically this is just used to map req.url onto for file paths.
The path variable is the path to your static files. Basically this is just used to map req.url onto for file paths. Requests to /javascripts/file.js will result in compilation of /coffeescripts/file.coffee

#### definition.live = (boolean)
If live is enabled, the middleware will check every request if the coffeescript file has been modified recently and recompile the javascript file if it's older. Otherwise, the middleware will only check to make sure the compiled javascript file exists and serve that, regardless of age. Live is disabled by default. I recommend using something like !process.env.PRODUCTION to set it, so it only recompiles per-request while in development.
Expand Down
113 changes: 54 additions & 59 deletions middleware.coffee
Expand Up @@ -8,69 +8,64 @@ pro = uglify.uglify

# Export closure to build middleware.
module.exports = (opts, coffee) ->
# Ensure some defaults.
if typeof opts.watch is 'undefined' then opts.watch = ['.coffee']
else if typeof opts.watch is 'string' then opts.watch = opts.watch.split ','
if typeof opts.uglify is 'undefined' then opts.uglify = true
if typeof opts.live is 'undefined' then opts.live = !process.env.PRODUCTION

# Return the middleware
(req, res, next) ->
# Make sure the current URL ends in either .coffee or .js
if !!~opts.watch.indexOf('.coffee') and !!~req.url.search(/.coffee$/)
cfile = opts.path + req.url
jfile = cfile.replace /.coffee$/, '.js'

else if !!~opts.watch.indexOf('.js') and !!~req.url.search(/.js$/)
if !~req.url.search(/^\/javascripts/) and !~req.url.search(/.js$/) then do next
else
jfile = opts.path + req.url
cfile = jfile.replace /.js$/, '.coffee'

# These aren't the droids you're looking for.
else return do next

# Handle the final serve.
end = (txt) ->
res.contentType 'javascript'
res.header 'Content-Length', txt.length
res.send txt

# Yup, we have to (re)compile.
compile = ->
file.readFile cfile, (err, cdata) ->
if err then do next
else
# Don't crash the server just because a compile failed.
try
ctxt = coffee.compile do cdata.toString
cfile = jfile.replace(/^\/javascripts/, '/coffeescripts')
cfile = cfile.replace(/.js$/, '.coffee')

# Handle the final serve.
end = (txt) ->
res.contentType 'javascript'
res.header 'Content-Length', txt.length
res.send txt

# Yup, we have to (re)compile.
compile = ->
file.readFile cfile, (err, cdata) ->
if err then do next
else
# Don't crash the server just because a compile failed.
try
# Attempt to compile to Javascript.
ctxt = coffee.compile do cdata.toString

# Ugligfy!
if opts.uglify
ast = jsp.parse ctxt
ast = pro.ast_mangle ast
ast = pro.ast_squeeze ast
ctxt = pro.gen_code ast
# Ugligfy, if enabled.
if opts.uglify
ast = jsp.parse ctxt
ast = pro.ast_mangle ast
ast = pro.ast_squeeze ast
ctxt = pro.gen_code ast

# Return
end ctxt
file.writeFile jfile, ctxt

# Continue on errors.
catch err
do next

# Check if the .js file exists.
file.readFile jfile, (err, jdata) ->
if err then do compile
else if not opts.live then end jdata
else
# Get mod date of .coffee file.
file.stat cfile, (err, cstat) ->
if err then end jdata
else
# Get mod date of .js file.
ctime = do (new Date cstat.mtime).getTime
file.stat jfile, (err, jstat) ->
if err then end jdata
else
# Compare mod dates.
jtime = do (new Date jstat.mtime).getTime
if ctime <= jtime then end jdata
else do compile
# Return result
end ctxt
file.writeFile jfile, ctxt
# Continue on errors.
catch err
do next
# Check if the .js file exists.
file.readFile jfile, (err, jdata) ->
if err then do compile
else if not opts.live then end jdata
else
# Get mod date of .coffee file.
file.stat cfile, (err, cstat) ->
if err then end jdata
else
# Get mod date of .js file.
ctime = do (new Date cstat.mtime).getTime
file.stat jfile, (err, jstat) ->
if err then end jdata
else
# Compare mod dates.
jtime = do (new Date jstat.mtime).getTime
if ctime <= jtime then end jdata
else do compile

0 comments on commit 4bdf8d2

Please sign in to comment.