-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pavel Zagoskin
committed
May 24, 2013
1 parent
978a3b2
commit 86efdea
Showing
4 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
bundleify | ||
========= | ||
|
||
Creates bundled .js source from one entry file. Optionally will write the file to the filesystem. For use with browserify 2.x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
require('coffee-script') | ||
|
||
var bundleify = require("./src/bundleify"); | ||
var bundlr = require("./src/bundlr"); | ||
|
||
|
||
module.exports = bundleify; | ||
module.exports = bundlr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
|
||
|
||
path = require('path') | ||
coffeeify = require('coffeeify') | ||
browserify = require('browserify') | ||
brfs = require('brfs') | ||
uglify = require('uglify-js') | ||
mime = require('mime') | ||
through = require('through') | ||
fs = require('fs') | ||
|
||
|
||
|
||
|
||
module.exports = bundlr = (filename) -> | ||
data = '' | ||
write = (buf) -> | ||
data += buf | ||
end = () -> | ||
safeText = data.replace(/\"/g, '\u005C\u0022') | ||
safeText = safeText.replace(/^(.*)/gm, '"$1') | ||
safeText = safeText.replace(/(.+)$/gm, '$1" +') | ||
safeText = safeText.replace(/\+$/, '') | ||
|
||
finalHtml = 'module.exports = ' + safeText + ';\n'; | ||
|
||
@queue finalHtml | ||
@queue null | ||
|
||
ext = path.extname(filename) | ||
if ext is ".html" | ||
return through(write, end) | ||
else | ||
return through() | ||
|
||
|
||
module.exports = buildjs = (opts) -> | ||
|
||
console.log htmlify | ||
src = opts.src | ||
route = opts.route | ||
dest = opts.dest | ||
debug = opts.debug | ||
write = opts.write or true | ||
caching = opts.cache or false | ||
watchCallback = opts.watchCallback or (err, data) -> | ||
if err | ||
console.log err | ||
else | ||
console.log "bundle success." | ||
|
||
console.log "[Source]:" , src | ||
console.log "[Route]:" , route | ||
console.log "[Dest]:" , dest | ||
|
||
|
||
b = browserify() | ||
|
||
b.transform(coffeeify) | ||
b.transform(brfs) | ||
b.transform(htmlify) | ||
|
||
b.transform (filename) -> | ||
b.allFiles.push(filename) | ||
return through() | ||
|
||
|
||
|
||
|
||
b.add __dirname+"/../"+src | ||
|
||
|
||
|
||
|
||
|
||
|
||
cache = {} | ||
|
||
|
||
|
||
|
||
|
||
|
||
return (req, res, next) -> | ||
|
||
|
||
|
||
watchFiles = (b, path) -> | ||
watchers = b.allFiles.map (filename) -> | ||
fs.watch filename , {persist:false} , () -> | ||
delete cache[path] | ||
generate b , watchCallback | ||
|
||
watchers.forEach (watcher) -> | ||
watcher.close() | ||
|
||
|
||
|
||
sendResponse = (err, src) -> | ||
if err | ||
next(err) | ||
else | ||
|
||
|
||
res.contentType('application/javascript') | ||
res.send(src) | ||
generate = (b , callback) -> | ||
|
||
try | ||
b.allFiles = [] | ||
b.bundle {debug:debug} , (err, src) -> | ||
compress = !debug | ||
watch = debug | ||
if watch | ||
watchFiles(b, req_path) | ||
|
||
|
||
|
||
if err | ||
return callback(err) | ||
|
||
|
||
if compress | ||
result = uglify src , | ||
fromString:true | ||
|
||
src = result | ||
|
||
cache[req_path] = src | ||
|
||
|
||
|
||
|
||
|
||
if write | ||
fs.writeFile dest , src, 'utf8', -> | ||
console.log "File Written." | ||
|
||
callback(null,src) | ||
|
||
catch err | ||
|
||
return callback(err) | ||
|
||
|
||
|
||
|
||
|
||
|
||
req_path = req.path | ||
|
||
|
||
|
||
if !path.extname(req_path) | ||
return next() | ||
else if mime.lookup(req_path) isnt "application/javascript" | ||
return next() | ||
|
||
|
||
if req_path is route | ||
if caching and cache[req_path] isnt undefined | ||
sendResponse(null, cache[req_path]) | ||
else | ||
generate(b , sendResponse) | ||
else | ||
next() |