chore(examples) prepare examples; cleanup

This commit is contained in:
Benjamin Wegener
2024-01-11 17:12:24 +00:00
parent 77b0138a40
commit dc21e6a488
3 changed files with 42 additions and 7 deletions

View File

@@ -1,15 +1,36 @@
import { unlinkSync } from 'node:fs';
const transpiler = Bun.Transpiler({
loader: 'cjs',
});
export default {
port: Bun.env.BUN_PORT || Bun.env.APP_PORT || 3000,
async fetch(req) {
const magicKey = Math.random().toString(36).substring(2, 10);
const url = new URL(req.url);
if (url.pathname == '/main.js') {
await Bun.build({
const path = `./.cache/`;
const filename = `${magicKey}.js`;
const file = Bun.file(`${path}/${filename}`);
const result = await Bun.build({
entrypoints: ['./src/main.js'],
outdir: '.',
outdir: path,
target: 'browser',
splitting: true,
naming: `[dir]/${filename}`,
});
return new Response(Bun.file('./main.js'), {
console.log(result);
const resp = new Response(file, {
headers: { 'Cache-Control': 'no-cache' },
});
setTimeout(() => {
unlinkSync(`${path}/${filename}`);
}, 400);
return resp;
}
if (url.pathname == '/') return new Response(Bun.file('./index.html'));
return new Response('ERR 404: Not Found', { status: 404 });