forked from webpack/webpack-dev-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
157 lines (138 loc) · 4.21 KB
/
middleware.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var mime = require("mime");
var getFilenameFromUrl = require("./lib/GetFilenameFromUrl");
var Shared = require("./lib/Shared");
var pathJoin = require("./lib/PathJoin");
var HASH_REGEXP = /[0-9a-f]{10,}/;
// constructor for the middleware
module.exports = function (compiler, options) {
var context = {
state: false,
webpackStats: undefined,
callbacks: [],
options: options,
compiler: compiler,
watching: undefined,
forceRebuild: false
};
var shared = Shared(context);
// The middleware function
function webpackDevMiddleware(req, res, next) {
function goNext() {
if(!context.options.serverSideRender) return next();
shared.ready(function () {
res.locals.webpackStats = context.webpackStats;
next();
}, req);
}
if(req.method !== "GET") {
return goNext();
}
var filename = getFilenameFromUrl(context.options.publicPath, context.compiler.outputPath, req.url);
if(filename === false) return goNext();
// in lazy mode, rebuild on bundle request
if(context.options.lazy && (!context.options.filename || context.options.filename.test(filename)))
shared.rebuild();
if(HASH_REGEXP.test(filename)) {
try {
if(context.fs.statSync(filename).isFile()) {
processRequest();
return;
}
} catch (e) {
}
}
// delay the request until we have a valid bundle
shared.ready(processRequest, req);
function processRequest() {
try {
var stat = context.fs.statSync(filename);
if(!stat.isFile()) {
if(stat.isDirectory()) {
filename = pathJoin(filename, context.options.index || "index.html");
stat = context.fs.statSync(filename);
if(!stat.isFile()) throw "next";
} else {
throw "next";
}
}
} catch (e) {
return goNext();
}
// server content
var content = context.fs.readFileSync(filename);
content = shared.handleRangeHeaders(content, req, res);
res.setHeader("Access-Control-Allow-Origin", "*"); // To support XHR, etc.
res.setHeader("Content-Type", mime.lookup(filename) + "; charset=UTF-8");
res.setHeader("Content-Length", content.length);
if(context.options.headers) {
for(var name in context.options.headers) {
res.setHeader(name, context.options.headers[name]);
}
}
// Express automatically sets the statusCode to 200, but not all servers do (Koa).
res.statusCode = res.statusCode || 200;
if(res.send) res.send(content);
else res.end(content);
}
}
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl.bind(this, context.options.publicPath, context.compiler.outputPath);
webpackDevMiddleware.waitUntilValid = shared.waitUntilValid;
webpackDevMiddleware.invalidate = shared.invalidate;
webpackDevMiddleware.close = shared.close;
webpackDevMiddleware.fileSystem = context.fs;
return webpackDevMiddleware;
};
// constructor for the direct interface
module.exports.direct = function (compiler, options) {
var context = {
state: false,
webpackStats: undefined,
callbacks: [],
options: options,
compiler: compiler,
watching: undefined,
forceRebuild: false
};
var shared = Shared(context);
// The direct interface
function webpackDevDirect(opts, cb) {
var options = context.options;
var fs = context.fs;
var filename = opts.src;
// in lazy mode, rebuild on bundle request
if(options.lazy && (!options.filename || options.filename.test(filename)))
this.rebuild();
if(HASH_REGEXP.test(filename)) {
try {
if(fs.statSync(filename).isFile()) {
processRequest();
return;
}
} catch (e) {
}
}
// delay the request until we have a valid bundle
shared.ready(processRequest);
function processRequest() {
var stat = context.fs.statSync(filename);
if(!stat.isFile()) {
return cb(new Error("invalid file was requested: " + filename));
}
fs.readFile(filename, function (err, content) {
if(err) {
return cb(new Error("error reading file: " + filename));
}
cb(null, content);
});
}
}
webpackDevDirect.waitUntilValid = shared.waitUntilValid;
webpackDevDirect.invalidate = shared.invalidate;
webpackDevDirect.close = shared.close;
webpackDevDirect.fileSystem = context.fs;
return webpackDevDirect;
};