-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathindex.js
99 lines (76 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var setupWatchify = require('./lib/bundlers/watchify.js')
, createHandler = require('./lib/create-handler.js')
, makeIO = require('./lib/make-io.js')
, path = require('path')
var parseArgs = require('./lib/args-to-options.js')
module.exports = beefy
function beefy(opts, ready) {
var io = makeIO(process, process.stdout, process.stderr)
opts =
typeof opts === 'string' ? {entries: [opts]} :
Array.isArray(opts) ? {entries: opts} :
opts
opts.cwd = opts.cwd || path.dirname(module.parent)
opts.entries = opts.entries || []
opts.bundlerFlags = opts.bundlerFlags || []
opts.bundler = opts.bundler || null
opts.quiet = opts.quiet === undefined ? true : opts.quiet
opts.live = !!opts.live
opts.watchify = opts.watchify === undefined ? true : opts.watchify
var args = ['9999']
, innerHandler
if(opts.cwd) {
args.push('--cwd', opts.cwd)
}
if(opts.live) {
args.push('--live')
}
if(typeof opts.bundler === 'string') {
args.push('--bundler', opts.bundler)
}
if(opts.index) {
args.push('--index', opts.index)
}
if('watchify' in opts && !opts.watchify) {
args.push('--no-watchify')
}
args.push('--')
args = args.concat(opts.bundlerFlags)
parseArgs(args, opts.cwd, function(err, genOpts) {
if(err) {
if(ready) {
return ready(err)
}
throw err
}
switch(opts.bundler && typeof opts.bundler) {
case 'function':
genOpts.handler.bundler.command = opts.bundler
break
case 'object':
genOpts.handler.bundler = opts.bundler
break
}
genOpts.handler.entries = fixupEntries(opts.entries)
genOpts.handler.log = opts.quiet ? false : genOpts.handler.log
innerHandler = createHandler(genOpts.handler, io, opts.unhandled)
})
return handler
function handler(req, resp) {
if(!innerHandler) {
return setTimeout(handler, 0, req, resp)
}
innerHandler(req, resp)
}
function fixupEntries(entries) {
return Array.isArray(entries) ? entries.reduce(toObject, {}) : entries
function toObject(obj, entry) {
entry = path.resolve(opts.cwd, entry)
obj[entry
.replace(opts.cwd, '/')
.replace('\\', '/')
.replace(/\/+/g, '/')] = entry
return obj
}
}
}