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
0 parents
commit 70bfc16
Showing
6 changed files
with
129 additions
and
0 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 @@ | ||
/node_modules |
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,3 @@ | ||
<body> | ||
<script src="play.js"></script> | ||
</body> |
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,3 @@ | ||
console.log('hello from another') | ||
|
||
module.exports = 'another' |
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,7 @@ | ||
|
||
var hyperglue = require('hyperglue') | ||
var another = require('./lib/another') | ||
var three = require('three') | ||
console.log('hello world') | ||
|
||
module.exports = 'play' |
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,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" | ||
} |
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,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 + '.') |