/
ristretto.js
179 lines (179 loc) · 6.21 KB
/
ristretto.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(function() {
var Ristretto, coffee, constants, fs, path;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
fs = require('fs');
coffee = require('coffee-script');
path = require('path');
constants = require('constants');
Ristretto = (function() {
Ristretto.UnknownModule = (function() {
__extends(UnknownModule, Error);
function UnknownModule(req, fileName) {
this.req = req;
this.fileName = fileName;
this.message = "Cannot find module `" + this.req + "` required in file `" + (path.basename(this.fileName)) + "`";
}
return UnknownModule;
})();
Ristretto.extensions = {
'.js': function(fileName) {
return fs.readFileSync(fileName, 'utf8');
},
'.coffee': function(fileName) {
return coffee.compile(fs.readFileSync(fileName, 'utf8'));
}
};
function Ristretto(options) {
var _ref, _ref2, _ref3;
if (options == null) {
options = {};
}
this.main = (_ref = options.main) != null ? _ref : ((function() {
throw new Error('No entry point specified');
})());
this.libraries = (_ref2 = options.libraries) != null ? _ref2 : {};
this.raw = (_ref3 = options.raw) != null ? _ref3 : [];
this.modules = {};
this.add(path.join(process.cwd(), this.main), "./main");
}
Ristretto.prototype.load = function(fileName) {
return Ristretto.extensions[path.extname(fileName)](fileName);
};
Ristretto.prototype.resolve = function(req, dir) {
var failure, reqPath;
failure = function() {
throw new Error('foo');
};
reqPath = (function() {
var _ref;
switch (req.charAt(0)) {
case '/':
return req;
case '.':
return path.normalize(path.join(dir, req));
default:
return (_ref = this.libraries[req]) != null ? _ref : failure();
}
}).call(this);
if ((path.existsSync(reqPath)) && (fs.statSync(reqPath)).isDirectory()) {
try {
return this.resolve(path.join(reqPath, 'index'), reqPath);
} catch (e) {
return failure();
}
} else {
if (path.existsSync(reqPath)) {
return reqPath;
}
if (path.existsSync(reqPath + '.js')) {
return reqPath + '.js';
}
if (path.existsSync(reqPath + '.coffee')) {
return reqPath + '.coffee';
}
return failure();
}
};
Ristretto.prototype.add = function(fileName, moduleName) {
var module, parent;
if (fileName in this.modules) {
return this.modules[fileName];
}
module = this.modules[fileName] = {
hash: moduleName + ' [' + (Object.keys(this.modules)).length + ']',
children: [],
parents: [],
rank: 0
};
module.body = this.load(fileName).replace(/require\(('|")(.*?)\1\)/g, __bind(function(_, _, req, pos, contents) {
var dep, depName;
depName = (function() {
try {
return this.resolve(req, path.dirname(fileName));
} catch (e) {
throw new Ristretto.UnknownModule(req, fileName);
}
}).call(this);
dep = this.add(this.resolve(req, path.dirname(fileName)), req);
dep.children.push(module);
module.parents.push(dep);
return "require('" + dep.hash + "')";
}, this));
if (module.parents.length) {
module.rank = 1 + Math.max.apply({}, (function() {
var _i, _len, _ref, _results;
_ref = module.parents;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
parent = _ref[_i];
_results.push(parent.rank);
}
return _results;
})());
}
return module;
};
Ristretto.prototype.bundle = function() {
var fileName, module;
return [
((function() {
var _i, _len, _ref, _results;
_ref = this.raw;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
fileName = _ref[_i];
_results.push(this.load(fileName));
}
return _results;
}).call(this)).join(';\n'), ";var _ristretto={\n modules:{},\n require:function(id){return _ristretto.modules[id].exports}\n};", ((function() {
var _i, _len, _ref, _results;
_ref = ((function() {
var _ref, _results;
_ref = this.modules;
_results = [];
for (fileName in _ref) {
module = _ref[fileName];
_results.push(module);
}
return _results;
}).call(this)).sort(function(a, b) {
return a.rank - b.rank;
});
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
module = _ref[_i];
_results.push("//" + module.hash + "\n;(function(require, module, exports){\n" + module.body + "\n})(_ristretto.require, _ristretto.modules[\"" + module.hash + "\"] = {exports:{}}, _ristretto.modules[\"" + module.hash + "\"].exports);");
}
return _results;
}).call(this)).join('\n')
].join('\n');
};
return Ristretto;
})();
exports.bundle = function(options) {
var ast, output, parser, uglify, _ref;
output = (new Ristretto(options)).bundle();
if (options.minify != null) {
_ref = require('uglify-js'), uglify = _ref.uglify, parser = _ref.parser;
ast = parser.parse(output);
ast = uglify.ast_mangle(ast);
ast = uglify.ast_squeeze(ast);
output = uglify.gen_code(ast);
}
if (options.copyright != null) {
output = "/*\n" + options.copyright + "\n*/\n" + output;
}
if (options.target != null) {
return fs.writeFileSync(options.target, output);
} else {
return output;
}
};
}).call(this);