-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
161 lines (130 loc) · 3.88 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
158
159
160
161
var extend = require("extend"),
fs = require("fs"),
gaze = require("gaze"),
mkdirp = require("mkdirp"),
path = require("path"),
url = require('url'),
requirejs = require("requirejs");
function compile(opts, callback) {
log("compiling now");
requirejs.optimize(opts,
function success(build) {
var deps = build.split("\n").filter(function(line, index) {
return !!line && index > 2;
}).map(function(file) {
return path.normalize(file);
});
log("compilation succeeded for %s", opts.out);
callback(null, deps);
}, function error(err) {
log("compilation failed for %s:\n%s", opts.out, err);
callback(err);
});
}
// Shhhhh don't say a word
var debugMessages = false;
function log() {
if (debugMessages) {
console.log.apply(this, arguments);
}
}
function setWatchers(module, deps) {
module._watched = true;
gaze(deps, function(err, watcher) {
log("watching %j", watcher.relative());
watcher.on("all", function(event, filepath) {
log("%s was %s", filepath, event);
module._compiled = false;
});
});
}
module.exports = function(opts) {
opts = opts || {};
if (!opts.src) {
throw "requirejs-middleware: You must specify a `src` directory";
}
if (!opts.dest) {
throw "requirejs-middleware: You must specify a `dest` directory";
}
if (!opts.modules) {
throw "requirejs-middleware: You must specify atleast one module in `modules`";
}
// TODO figure out a less crappy way of setting this
debugMessages = !!opts.debug;
opts.defaults = opts.defaults || {};
Object.keys(opts.modules).forEach(function(key) {
var module = opts.modules[key];
module = extend(true, module, opts.defaults);
module.name = module.name || path.relative(module.baseUrl, __dirname + "/almond");
module.out = path.join(opts.dest, key);
if (opts.once) {
log("`once` set");
compile(module, function(err) {
if (err) {
throw err;
}
});
}
});
return function(req, res, next) {
if (opts.once) {
return next();
}
// Only deal with GET or HEAD requests
if (req.method.toUpperCase() != "GET" && req.method.toUpperCase() != "HEAD") {
return next();
}
var pathname = url.parse(req.url).pathname;
var module = opts.modules[pathname];
// Is this a require module we're aware of, and has it been compiled?
if (!module || module._compiled) {
return next();
}
var srcPath = path.join(opts.src, pathname);
fs.stat(srcPath, function(err, srcStats) {
// Ignore ENOENT to fall through as 404
if (err) {
return next(err.code == "ENOENT" ? null : err);
}
// If we're not building with almond, just copy the file to `dest`
if (!opts.build) {
var destPath = path.join(opts.dest, pathname);
mkdirp(path.dirname(destPath), function(err) {
if (err) {
log("error creating directory structure %j", err);
return next(err);
}
var reader = fs.createReadStream(srcPath),
writer = fs.createWriteStream(destPath);
reader.on("error", function(err) {
log("error reading file: %j", err);
next(err);
});
writer.on("close", function() {
log("copied %s into `dest`", pathname);
if (!module._watched) {
setWatchers(module, srcPath);
}
next();
});
writer.on("error", function(err) {
log("error writing file: %j", err);
next(err);
});
reader.pipe(writer);
});
return;
}
compile(module, function(err, deps) {
if (err) {
return next(err);
}
module._compiled = true;
if (!module._watched) {
setWatchers(module, deps);
}
next();
});
});
};
}