Skip to content

Commit

Permalink
refactor out a separate method and fix todo
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Sep 16, 2021
1 parent 7486720 commit 9899659
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,7 @@ class Watcher extends EventEmitter {
print_config_conflicts(conflicts, 'kit.vite.');

this.vite = await vite.createServer(merged_config);

/**
* @type {import('connect').Server}
*/
const connect_server = this.vite.middlewares;
// TODO: replace index with name after https://github.com/vitejs/vite/pull/4908 is merged and released
const spa_fallback_index = 7;
const html_middlewares = ['viteIndexHtmlMiddleware', 'vite404Middleware'];
for (let i = connect_server.stack.length - 1; i > 0; i--) {
// @ts-expect-error using internals until https://github.com/vitejs/vite/pull/4640 is merged
if (html_middlewares.includes(connect_server.stack[i].name) || i === spa_fallback_index) {
connect_server.stack.splice(i, 1);
}
}

remove_html_middlewares(this.vite.middlewares);
await this.vite.listen(this.port);
}

Expand Down Expand Up @@ -517,3 +503,20 @@ function not_found(res) {
res.statusCode = 404;
res.end('Not found');
}

/**
* @param {import('connect').Server} server
*/
function remove_html_middlewares(server) {
const html_middlewares = [
'viteIndexHtmlMiddleware',
'vite404Middleware',
'viteSpaFallbackMiddleware'
];
for (let i = server.stack.length - 1; i > 0; i--) {
// @ts-expect-error using internals until https://github.com/vitejs/vite/pull/4640 is merged
if (html_middlewares.includes(server.stack[i].handle.name)) {
server.stack.splice(i, 1);
}
}
}

0 comments on commit 9899659

Please sign in to comment.