-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
157 lines (130 loc) · 4.17 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
var browserify = require('browserify');
var requireify = require('requireify');
var through = require('through');
var innersource = require('innersource');
var detective = require('detective');
var generator = require('inline-source-map');
var combine = require('combine-source-map');
var _ = require('lodash');
var findPackageJson = require('./lib/findPackageJson');
var outputDirWriter = require('./lib/outputDirWriter');
var scriptjsLocation = require.resolve('scriptjs');
var Q = require('q');
var events = require('events');
var postpend = innersource(addModule).replace(/\n/g, '');
var bundleEmitter = new events.EventEmitter();
var pack;
module.exports = function(filename) {
if(!pack){
pack = findPackageJson(filename);
pack.then(function(data){
if(!data.pack.promethify.outputDir){
var server = require('./lib/server').start(bundleEmitter, pack);
}
else{
outputDirWriter.start(bundleEmitter, data);
}
});
}
var buffer = '';
return through(function(chunk) {
buffer += chunk.toString();
},
function() {
if(_.contains(filename, 'scriptjs')){
this.queue(buffer);
this.queue(null);
return;
}
pack.then(function(data){
var port = data.pack.promethify.port;
var hostname = data.pack.promethify.hostname;
var outputDeployPath = getPath(data.pack.promethify.outputDeployPath);
var prepend = innersource(addRequire)
.replace('SCRIPTJS_LOC', scriptjsLocation)
.replace('HOSTNAME', hostname)
.replace('OUTPUT_DEPLOY_PATH', outputDeployPath)
.replace('PORT_NUM', port)
.replace(/\n/g, '');
var asyncRequres = getAsyncRequires(buffer);
var defs = asyncRequres.map(makeBrowserifyBundle);
var totalPrelude = prepend;
var offset = totalPrelude.split('\n').length - 1;
var complete = totalPrelude + combine.removeComments(buffer) + postpend;
var map = combine.create().addFile({ sourceFile: filename, source: buffer}, {line: offset});
this.queue( complete + '\n'+map.comment());
Q.all(defs).then(function(){
this.queue(null);
}.bind(this));
}.bind(this)).done();
});
};
function addModule(){
}
function addRequire(){
/* jshint shadow: true */
var tmpRequire = require;
var scriptjs = require('SCRIPTJS_LOC');
var require = function require(keys, callback){
if(Array.isArray(keys)){
var urls = keys.map(function(key){
if( key[0] !== '/' ) { key = '/'+key;}
return 'http://HOSTNAME:PORT_NUMOUTPUT_DEPLOY_PATH'+key;
});
var scriptParam = [urls, function(){
var deps = keys.map(function(key){ return window.require(key); });
callback.apply(null, deps);
}];
scriptjs.apply(null, scriptParam);
return;
}else{
return tmpRequire.apply(null, arguments);
}
};
}
function getAsyncRequires(source){
var amdRequires = detective.find(source, {nodes: true});
var amdRequireArray = amdRequires.expressions.map(function(arrString){
var exps = require('esprima').parse(arrString).body[0].expression;
var array = require('static-eval')(exps);
return array;
});
return _.flatten(amdRequireArray);
}
function makeBrowserifyBundle(asyncDep){
var def = Q.defer();
bundleEmitter.emit('bundle', asyncDep, def.promise);
var b = browserify({debug: true});
b.transform(requireify);
pack.then(function(data){
var basedir = data.pack.promethify.basedir;
var dir = data.dir;
if(asyncDep[0] === '/'){
b
.add(dir + '/' + basedir + asyncDep)
.bundle({basedir: dir + '/' + basedir}, function(err, src){
def.resolve(src);
});
}
else {
var resumer = require('resumer');
var stream = resumer()
.queue('module.exports = require("'+asyncDep+'");').end();
b
.require(stream)
.bundle({basedir: dir + '/' + basedir}, function(err, src){
def.resolve(src);
});
}
}).done();
return def.promise;
}
function getPath(deployPath){
if(deployPath){
return '/' + deployPath;
}
else {
return '';
}
}