Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
airportyh committed Jul 23, 2013
0 parents commit 70bfc16
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/node_modules
3 changes: 3 additions & 0 deletions examples/one/index.html
@@ -0,0 +1,3 @@
<body>
<script src="play.js"></script>
</body>
3 changes: 3 additions & 0 deletions examples/one/lib/another.js
@@ -0,0 +1,3 @@
console.log('hello from another')

module.exports = 'another'
7 changes: 7 additions & 0 deletions examples/one/play.js
@@ -0,0 +1,7 @@

var hyperglue = require('hyperglue')
var another = require('./lib/another')
var three = require('three')
console.log('hello world')

module.exports = 'play'
22 changes: 22 additions & 0 deletions package.json
@@ -0,0 +1,22 @@
{
"name": "bff",
"version": "0.0.0",
"description": "Browserify Friend - it's like browserify was built into the browser.",
"dependencies": {
"Set": "~0.4.1",
"browserify": "~2.25.0",
"ecstatic": "~0.4.5"
},
"bin": "server.js",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [
"browserify",
"browser"
],
"author": "Toby Ho",
"license": "MIT"
}
93 changes: 93 additions & 0 deletions server.js
@@ -0,0 +1,93 @@
#! /usr/bin/env node

var http = require('http')
var ecstatic = require('ecstatic')
var browserify = require('browserify')
var path = require('path')
var Set = require('Set')

var rootDir = process.cwd()
var server = http.createServer(function(req, resp){
if (isJavascript(req)){
serveScript(req, resp)
}else{
serveFile(req, resp)
}
})

var serveFile = ecstatic({ root: rootDir })

function isJavascript(req){
var uri = req.url.split('?')[0]
return !!uri.match(/\.js$/)
}

function serveScript(req, resp){
resp.setHeader('Content-Type', 'text/javascript')
var uri = req.url.split('?')[0]
var path = '.' + uri
getDeps(path, function(err, deps){
if (err){
respondWithError(resp, err)
return
}
var b = browserify()
b.add(path)
deps.forEach(function(dep){
b.require(dep)
})
b.bundle({
debug: true,
detectGlobals: false
}, function(err, src){
if (err){
respondWithError(resp, err)
return
}
resp.end(src)
})
b.on('error', function(e){
respondWithError(resp, e)
})
})
}

function getDeps(pth, callback){
var b = browserify()
b.add(pth)
b.bundle({
detectGlobals: false
}, function(err){
if (err){
callback(err)
}else{
callback(null, deps.toArray())
}
})
var deps = new Set
b.on('dep', function(dep){
if (!isModule(dep)){
var relpath = path.relative(rootDir, dep.id)
deps.add('./' + relpath.match(/^(.+)\.js$/)[1])
for (var d in dep.deps){
deps.add(d)
}
}
})
b.on('error', function(e){
callback(e)
})
}

function isModule(dep){
var relpath = path.relative(rootDir, dep.id)
return relpath.substring(0, 12) === 'node_modules'
}

function respondWithError(resp, err){
resp.end('console.error("' + String(err).replace(/\"/g, '\\"') + '")')
}

var port = 3000
server.listen(port)
console.log('Serving the contents of ' + rootDir + ' on port ' + port + '.')

0 comments on commit 70bfc16

Please sign in to comment.