Skip to content

Commit

Permalink
feat: add dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomuikuta committed May 4, 2024
1 parent 9fc0987 commit 6b64390
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/chibivite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
"rollup": "^4.17.2"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/connect": "^3.4.38",
"cac": "^6.7.14",
"connect": "^3.7.0",
"rimraf": "^5.0.5",
"tslib": "^2.6.2"
}
Expand Down
7 changes: 7 additions & 0 deletions packages/chibivite/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'rollup'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import nodeResolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'

Expand All @@ -13,6 +15,7 @@ export default defineConfig({
output: {
dir: './dist',
entryFileNames: `node/[name].js`,
chunkFileNames: 'node/chunks/dep-[hash].js',
exports: 'named',
format: 'esm',
externalLiveBindings: false,
Expand All @@ -25,5 +28,9 @@ export default defineConfig({
tsconfig: path.resolve(__dirname, 'src/node/tsconfig.json'),
sourceMap: true,
}),
commonjs({
extensions: ['.js'],
}),
json(),
],
})
10 changes: 10 additions & 0 deletions packages/chibivite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const cli = cac('chibivite')

cli.command('[root]', 'start dev server').action(async (root: string) => {
console.log(`Hello, chibivite at ${root}!`)
const { createServer } = await import('./server')

try {
const server = await createServer()
await server.listen()
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
console.error(`error when starting dev server:\n${(e as any).stack}`)
process.exit(1)
}
})

cli.parse()
38 changes: 38 additions & 0 deletions packages/chibivite/src/node/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import connect from 'connect'

const PORT = 5137

export async function createServer() {
const middlewares = connect()
const { createServer } = await import('node:http')
const httpServer = createServer(middlewares)

const server = {
middlewares,
httpServer,
async listen() {
await new Promise((resolve, reject) => {
const onError = (e: Error & { code?: string }) => {
httpServer.removeListener('error', onError)

if (e.code === 'EADDRINUSE') {
reject(new Error(`Port ${PORT} is already in use`))
} else {
reject(e)
}
}

httpServer.on('error', onError)

server.httpServer.listen(PORT, () => {
httpServer.removeListener('error', onError)
resolve(PORT)
})
})

return server
},
}

return server
}
Loading

0 comments on commit 6b64390

Please sign in to comment.