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

Commit

Permalink
Merge pull request #23 from sveltejs/remove-webpack-dev-middleware
Browse files Browse the repository at this point in the history
remove webpack-dev-middleware
  • Loading branch information
Rich-Harris authored Dec 18, 2017
2 parents 514331b + 8270463 commit d0dd1d6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 130 deletions.
94 changes: 52 additions & 42 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,21 @@ module.exports = function connect(opts) {
dev
);

const dev_middleware = dev ? require('webpack-dev-middleware')(client, {
noInfo: true,
logLevel: 'silent',
publicPath: '/client/'
}) : null;

const hot_middleware = dev ? require('webpack-hot-middleware')(client, {
reload: true,
path: '/__webpack_hmr',
heartbeat: 10 * 1000
}) : null;

async function handle_webpack_generated_files(url, req, res, next) {
if (dev) {
dev_middleware(req, res, () => {
hot_middleware(req, res, next);
async function handle_webpack_generated_files(req, res, next) {
if (req.pathname.startsWith('/client/')) {
await compiler.ready;
res.set({
'Content-Type': 'application/javascript',
'Cache-Control': 'max-age=31536000'
});
res.end(compiler.asset_cache[req.pathname]);
} else {
if (url.startsWith('/client/')) {
await compiler.ready;
res.set({
'Content-Type': 'application/javascript',
'Cache-Control': 'max-age=31536000'
});
res.end(compiler.asset_cache[url]);
} else {
next();
}
next();
}
}

async function handle_index(url, req, res, next) {
if (url === '/index.html') {
async function handle_index(req, res, next) {
if (req.pathname === '/index.html') {
await compiler.ready;
res.set({
'Content-Type': 'text/html',
Expand All @@ -78,8 +60,8 @@ module.exports = function connect(opts) {
}
}

async function handle_service_worker(url, req, res, next) {
if (url === '/service-worker.js') {
async function handle_service_worker(req, res, next) {
if (req.pathname === '/service-worker.js') {
await compiler.ready;
res.set({
'Content-Type': 'application/javascript',
Expand All @@ -91,7 +73,9 @@ module.exports = function connect(opts) {
}
}

async function handle_route(url, req, res, next) {
async function handle_route(req, res, next) {
const url = req.pathname;

// whatever happens, we're going to serve some HTML
res.set({
'Content-Type': 'text/html'
Expand Down Expand Up @@ -148,15 +132,41 @@ module.exports = function connect(opts) {
}
}

return async function(req, res, next) {
const url = req.url.replace(/\?.+/, '');

handle_index(url, req, res, () => {
handle_service_worker(url, req, res, () => {
handle_webpack_generated_files(url, req, res, () => {
handle_route(url, req, res, next);
});
});
});
const handler = compose_handlers([
dev && require('webpack-hot-middleware')(client, {
reload: true,
path: '/__webpack_hmr',
heartbeat: 10 * 1000
}),

handle_index,
handle_service_worker,
handle_webpack_generated_files,
handle_route
].filter(Boolean));

return function(req, res, next) {
req.pathname = req.url.replace(/\?.+/, '');
handler(req, res, next);
};
};
};

function compose_handlers(handlers) {
return (req, res, next) => {
let i = 0;
function go() {
const handler = handlers[i];

if (handler) {
handler(req, res, () => {
i += 1;
go();
});
} else {
next();
}
}

go();
}
}
12 changes: 10 additions & 2 deletions lib/utils/create_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,16 @@ module.exports = function create_compiler(client, server, dest, routes, dev) {

server.plugin('failed', reject);

// client is already being watched by the middleware,
// so we only need to start the server compiler
client.watch({}, (err, stats) => {
if (stats.hasErrors()) {
reject(stats.toJson().errors[0]);
} else {
client_updated(stats);
client_is_ready = true;
if (server_is_ready) fulfil();
}
});

server.watch({}, (err, stats) => {
if (stats.hasErrors()) {
reject(stats.toJson().errors[0]);
Expand Down
86 changes: 1 addition & 85 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"mkdirp": "^0.5.1",
"rimraf": "^2.6.2",
"webpack": "^3.10.0",
"webpack-dev-middleware": "^2.0.1",
"webpack-hot-middleware": "^2.21.0"
},
"devDependencies": {
Expand Down

0 comments on commit d0dd1d6

Please sign in to comment.