|
| 1 | +const { spawn } = require('child_process'); |
| 2 | +const http = require('http'); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const ROOT = path.resolve(__dirname, '..'); |
| 7 | +const BUILD = path.join(ROOT, 'build'); |
| 8 | +const PORT = Number(process.env.PORT) || 3000; |
| 9 | + |
| 10 | +const MIME = { |
| 11 | + '.html': 'text/html; charset=utf-8', |
| 12 | + '.js': 'application/javascript; charset=utf-8', |
| 13 | + '.css': 'text/css; charset=utf-8', |
| 14 | + '.json': 'application/json; charset=utf-8', |
| 15 | + '.png': 'image/png', |
| 16 | + '.jpg': 'image/jpeg', |
| 17 | + '.jpeg': 'image/jpeg', |
| 18 | + '.gif': 'image/gif', |
| 19 | + '.webp': 'image/webp', |
| 20 | + '.svg': 'image/svg+xml', |
| 21 | + '.ico': 'image/x-icon', |
| 22 | + '.woff': 'font/woff', |
| 23 | + '.woff2': 'font/woff2', |
| 24 | + '.wasm': 'application/wasm', |
| 25 | + '.map': 'application/json', |
| 26 | +}; |
| 27 | + |
| 28 | +function tryResolveFile(filePath) { |
| 29 | + if (!filePath.startsWith(BUILD)) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) { |
| 33 | + return filePath; |
| 34 | + } |
| 35 | + return null; |
| 36 | +} |
| 37 | + |
| 38 | +function resolveRequest(urlPath) { |
| 39 | + const decoded = decodeURIComponent(urlPath.split('?')[0]); |
| 40 | + const normalized = |
| 41 | + decoded === '/' || decoded === '' |
| 42 | + ? '/index.html' |
| 43 | + : decoded.endsWith('/') |
| 44 | + ? decoded + 'index.html' |
| 45 | + : decoded; |
| 46 | + |
| 47 | + let filePath = path.normalize(path.join(BUILD, normalized)); |
| 48 | + |
| 49 | + let found = tryResolveFile(filePath); |
| 50 | + if (found) { |
| 51 | + return found; |
| 52 | + } |
| 53 | + |
| 54 | + if (path.extname(filePath) === '') { |
| 55 | + found = tryResolveFile(filePath + '.html'); |
| 56 | + if (found) { |
| 57 | + return found; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + found = tryResolveFile(path.join(BUILD, path.basename(filePath))); |
| 62 | + if (found) { |
| 63 | + return found; |
| 64 | + } |
| 65 | + |
| 66 | + if (normalized.startsWith('/dist/')) { |
| 67 | + found = tryResolveFile(path.join(BUILD, 'dist', path.basename(filePath))); |
| 68 | + if (found) { |
| 69 | + return found; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | +} |
| 75 | + |
| 76 | +let serverStarted = false; |
| 77 | + |
| 78 | +function startServer() { |
| 79 | + if (serverStarted) { |
| 80 | + return; |
| 81 | + } |
| 82 | + serverStarted = true; |
| 83 | + |
| 84 | + const server = http.createServer((req, res) => { |
| 85 | + const filePath = resolveRequest(req.url || '/'); |
| 86 | + |
| 87 | + if (!filePath) { |
| 88 | + res.writeHead(404); |
| 89 | + res.end(); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const ext = path.extname(filePath); |
| 94 | + res.writeHead(200, { |
| 95 | + 'Content-Type': MIME[ext] || 'application/octet-stream', |
| 96 | + 'Access-Control-Allow-Origin': '*', |
| 97 | + }); |
| 98 | + fs.createReadStream(filePath).pipe(res); |
| 99 | + }); |
| 100 | + |
| 101 | + server.on('error', (err) => { |
| 102 | + if (err.code === 'EADDRINUSE') { |
| 103 | + console.error( |
| 104 | + `Port ${PORT} is already in use. Stop the other process or set PORT to another value.` |
| 105 | + ); |
| 106 | + } else { |
| 107 | + console.error(err); |
| 108 | + } |
| 109 | + parcel.kill('SIGINT'); |
| 110 | + process.exit(1); |
| 111 | + }); |
| 112 | + |
| 113 | + server.listen(PORT, () => { |
| 114 | + const url = `http://localhost:${PORT}/`; |
| 115 | + console.log(`Dev server: ${url}`); |
| 116 | + openBrowser(url); |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +function openBrowser(url) { |
| 121 | + const cmd = |
| 122 | + process.platform === 'win32' |
| 123 | + ? `start "" "${url}"` |
| 124 | + : process.platform === 'darwin' |
| 125 | + ? `open "${url}"` |
| 126 | + : `xdg-open "${url}"`; |
| 127 | + spawn(cmd, { shell: true, stdio: 'ignore' }); |
| 128 | +} |
| 129 | + |
| 130 | +const parcelBin = path.join( |
| 131 | + ROOT, |
| 132 | + 'node_modules', |
| 133 | + '.bin', |
| 134 | + process.platform === 'win32' ? 'parcel.cmd' : 'parcel' |
| 135 | +); |
| 136 | + |
| 137 | +const parcelArgs = [ |
| 138 | + 'watch', |
| 139 | + './src/index.pug', |
| 140 | + './src/achievements/*.pug', |
| 141 | + './src/projects/*.pug', |
| 142 | + '--out-dir', |
| 143 | + 'build', |
| 144 | + '--public-url', |
| 145 | + '/', |
| 146 | + '--no-cache', |
| 147 | +]; |
| 148 | + |
| 149 | +const parcel = spawn(parcelBin, parcelArgs, { |
| 150 | + cwd: ROOT, |
| 151 | + stdio: ['inherit', 'pipe', 'pipe'], |
| 152 | + shell: process.platform === 'win32', |
| 153 | +}); |
| 154 | + |
| 155 | +parcel.stdout.on('data', (chunk) => { |
| 156 | + process.stdout.write(chunk); |
| 157 | + if (!serverStarted && /Built in/.test(chunk.toString())) { |
| 158 | + startServer(); |
| 159 | + } |
| 160 | +}); |
| 161 | + |
| 162 | +parcel.stderr.on('data', (chunk) => { |
| 163 | + process.stderr.write(chunk); |
| 164 | +}); |
| 165 | + |
| 166 | +parcel.on('exit', (code) => { |
| 167 | + process.exit(code ?? 1); |
| 168 | +}); |
| 169 | + |
| 170 | +process.on('SIGINT', () => { |
| 171 | + parcel.kill('SIGINT'); |
| 172 | + process.exit(0); |
| 173 | +}); |
0 commit comments