Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 00bcb77

Browse files
committed
Merge branch 'master' into gh-21
2 parents 7730962 + d0dd1d6 commit 00bcb77

File tree

4 files changed

+63
-130
lines changed

4 files changed

+63
-130
lines changed

lib/index.js

+52-42
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,21 @@ module.exports = function connect() {
3333
routes
3434
);
3535

36-
const dev_middleware = dev ? require('webpack-dev-middleware')(client, {
37-
noInfo: true,
38-
logLevel: 'silent',
39-
publicPath: '/client/'
40-
}) : null;
41-
42-
const hot_middleware = dev ? require('webpack-hot-middleware')(client, {
43-
reload: true,
44-
path: '/__webpack_hmr',
45-
heartbeat: 10 * 1000
46-
}) : null;
47-
48-
async function handle_webpack_generated_files(url, req, res, next) {
49-
if (dev) {
50-
dev_middleware(req, res, () => {
51-
hot_middleware(req, res, next);
36+
async function handle_webpack_generated_files(req, res, next) {
37+
if (req.pathname.startsWith('/client/')) {
38+
await compiler.ready;
39+
res.set({
40+
'Content-Type': 'application/javascript',
41+
'Cache-Control': 'max-age=31536000'
5242
});
43+
res.end(compiler.asset_cache[req.pathname]);
5344
} else {
54-
if (url.startsWith('/client/')) {
55-
await compiler.ready;
56-
res.set({
57-
'Content-Type': 'application/javascript',
58-
'Cache-Control': 'max-age=31536000'
59-
});
60-
res.end(compiler.asset_cache[url]);
61-
} else {
62-
next();
63-
}
45+
next();
6446
}
6547
}
6648

67-
async function handle_index(url, req, res, next) {
68-
if (url === '/index.html') {
49+
async function handle_index(req, res, next) {
50+
if (req.pathname === '/index.html') {
6951
await compiler.ready;
7052
res.set({
7153
'Content-Type': 'text/html',
@@ -77,8 +59,8 @@ module.exports = function connect() {
7759
}
7860
}
7961

80-
async function handle_service_worker(url, req, res, next) {
81-
if (url === '/service-worker.js') {
62+
async function handle_service_worker(req, res, next) {
63+
if (req.pathname === '/service-worker.js') {
8264
await compiler.ready;
8365
res.set({
8466
'Content-Type': 'application/javascript',
@@ -90,7 +72,9 @@ module.exports = function connect() {
9072
}
9173
}
9274

93-
async function handle_route(url, req, res, next) {
75+
async function handle_route(req, res, next) {
76+
const url = req.pathname;
77+
9478
// whatever happens, we're going to serve some HTML
9579
res.set({
9680
'Content-Type': 'text/html'
@@ -147,15 +131,41 @@ module.exports = function connect() {
147131
}
148132
}
149133

150-
return async function(req, res, next) {
151-
const url = req.url.replace(/\?.+/, '');
152-
153-
handle_index(url, req, res, () => {
154-
handle_service_worker(url, req, res, () => {
155-
handle_webpack_generated_files(url, req, res, () => {
156-
handle_route(url, req, res, next);
157-
});
158-
});
159-
});
134+
const handler = compose_handlers([
135+
dev && require('webpack-hot-middleware')(client, {
136+
reload: true,
137+
path: '/__webpack_hmr',
138+
heartbeat: 10 * 1000
139+
}),
140+
141+
handle_index,
142+
handle_service_worker,
143+
handle_webpack_generated_files,
144+
handle_route
145+
].filter(Boolean));
146+
147+
return function(req, res, next) {
148+
req.pathname = req.url.replace(/\?.+/, '');
149+
handler(req, res, next);
160150
};
161-
};
151+
};
152+
153+
function compose_handlers(handlers) {
154+
return (req, res, next) => {
155+
let i = 0;
156+
function go() {
157+
const handler = handlers[i];
158+
159+
if (handler) {
160+
handler(req, res, () => {
161+
i += 1;
162+
go();
163+
});
164+
} else {
165+
next();
166+
}
167+
}
168+
169+
go();
170+
}
171+
}

lib/utils/create_prod_compiler.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,16 @@ module.exports = function create_compiler(client, server, dest, routes) {
111111

112112
server.plugin('failed', reject);
113113

114-
// client is already being watched by the middleware,
115-
// so we only need to start the server compiler
114+
client.watch({}, (err, stats) => {
115+
if (stats.hasErrors()) {
116+
reject(stats.toJson().errors[0]);
117+
} else {
118+
client_updated(stats);
119+
client_is_ready = true;
120+
if (server_is_ready) fulfil();
121+
}
122+
});
123+
116124
server.watch({}, (err, stats) => {
117125
if (stats.hasErrors()) {
118126
reject(stats.toJson().errors[0]);

package-lock.json

+1-85
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"require-relative": "^0.8.7",
1717
"rimraf": "^2.6.2",
1818
"webpack": "^3.10.0",
19-
"webpack-dev-middleware": "^2.0.1",
2019
"webpack-hot-middleware": "^2.21.0"
2120
},
2221
"devDependencies": {

0 commit comments

Comments
 (0)