-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Router from '@koa/router'; | ||
import Koa from 'koa'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { PassThrough } from 'node:stream'; | ||
|
||
/** @returns {import("rollup").Plugin} */ | ||
const devServer = ({ port = 3000 } = {}) => { | ||
const koa = new Koa(); | ||
const router = new Router(); | ||
|
||
let sendCommand = null; | ||
router.get('/__dev_server', (ctx) => { | ||
ctx.set('Content-Type', 'text/event-stream'); | ||
ctx.set('Cache-Control', 'no-cache'); | ||
ctx.set('Connection', 'keep-alive'); | ||
const stream = new PassThrough(); | ||
ctx.status = 200; | ||
ctx.body = stream; | ||
sendCommand = (cmd) => { | ||
stream.write(`data: ${cmd}\n\n`); | ||
}; | ||
ctx.req.on('close', () => { | ||
sendCommand = null; | ||
}); | ||
}); | ||
|
||
let html = ''; | ||
router.get('/', (ctx) => { | ||
ctx.body = html; | ||
}); | ||
|
||
koa.use(router.routes()).use(router.allowedMethods()); | ||
koa.listen(port, '0.0.0.0'); | ||
|
||
return { | ||
name: 'dev-server', | ||
watchChange() { | ||
sendCommand?.('update'); | ||
}, | ||
async generateBundle(_, bundle) { | ||
let body = ''; | ||
Object.values(bundle).forEach((file) => { | ||
if ( | ||
path.extname(file.fileName) === '.html' && | ||
!file.fileName.endsWith('back.html') | ||
) { | ||
body += file.source; | ||
} | ||
}); | ||
body += `<script> ${await fs.readFile(path.resolve(import.meta.dirname, 'runtime.js'), { encoding: 'utf-8' })} </script>`; | ||
html = `<!DOCTYPE html> | ||
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head> | ||
<body>${body}</body> | ||
`; | ||
sendCommand?.('refresh'); | ||
}, | ||
}; | ||
}; | ||
|
||
export default devServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const source = new EventSource('/__dev_server'); | ||
|
||
source.onmessage = (event) => { | ||
switch (event.data) { | ||
case 'refresh': | ||
location.reload(); | ||
break; | ||
case 'update': | ||
console.log('code updated. Will refresh after build.'); | ||
break; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { extname } from 'node:path'; | ||
|
||
export default () => ({ | ||
name: 'generate-template', | ||
generateBundle(_, bundle) { | ||
Object.keys(bundle) | ||
.filter((fileName) => extname(fileName) !== '.html') | ||
.forEach((fileName) => { | ||
delete bundle[fileName]; | ||
}); | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: `style.css`, | ||
source: ``, | ||
}); | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: `back.html`, | ||
source: `<div id="at-back-indicator"></div>{{FrontSide}}`, | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.