Skip to content

Commit

Permalink
substitute redirects with code transforms
Browse files Browse the repository at this point in the history
Thanks to Snowpack's WTR plugin for most of this implementation: https://github.com/snowpackjs/snowpack/blob/main/plugins/web-test-runner-plugin/plugin.js

There should be a slight speed increase due to bypassing network requests, but I haven't tested it.  This implementation seems to load fewer files also.
  • Loading branch information
a-laughlin authored Oct 4, 2021
1 parent 67a32f1 commit 54fec41
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
const vite = require("vite");

module.exports = function () {
let server;
/** @type {import('vite').ViteDevServer } */
let server

return {
name: "vite-plugin",

async serverStart({ app }) {
server = await vite.createServer({
clearScreen: false,
});
await server.listen();
const port = server.config.server.port;
const protocol = server.config.server.https ? "https" : "http";
app.use((ctx, next) => {
ctx.redirect(`${protocol}://localhost:${port}${ctx.originalUrl}`);
return;
});
name: 'vite-plugin',
async serverStart({app}) {
server = await vite.createServer({clearScreen: false})
await server.listen()
},

async serverStop() {
return server.close();
await server.close()
},
async serve({request, response, app, originalUrl, req, res, socket}) {
if (isTestRunnerFile(request.url)) return
return {body: (await server.transformRequest(request.url)).code}
},
transformImport({source}) {
if (!isTestFilePath(source) || isTestRunnerFile(source)) return
const {port, https, host} = server.config.server
return `${https ? 'https' : 'http'}://${host ?? 'localhost'}:${port ?? 80}${source}`
},
};
}
};

0 comments on commit 54fec41

Please sign in to comment.