Created
May 22, 2021 13:38
-
-
Save istarkov/b2c26918c8a4765f48b8bce99eec0992 to your computer and use it in GitHub Desktop.
esbuild node app setup for latest stable node > 14.16.0
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
// same as `yarn esbuild index.ts src/**/*.ts --platform=node --format=esm --tsconfig=./tsconfig.json --outdir=dist --watch` | |
// but with respawn server on change | |
import esbuild from 'esbuild'; | |
import fg from 'fast-glob'; | |
import { spawn } from 'child_process'; | |
const entryPoints = await fg(['index.ts', 'src/**/*.ts']); | |
const dev = process.env.NODE_ENV !== 'production'; | |
let childProcess = null; | |
const startServer = () => { | |
if (childProcess) { | |
childProcess.kill(); | |
} | |
console.info('Starting/Restarting dev server'); | |
childProcess = spawn( | |
'node', | |
['--experimental-specifier-resolution=node', 'dist/index.js'], | |
{ | |
stdio: 'inherit', | |
}, | |
); | |
}; | |
const buildRes = await esbuild.build({ | |
entryPoints, | |
platform: 'node', | |
format: 'esm', | |
tsconfig: './tsconfig.json', | |
outdir: 'dist', | |
watch: dev | |
? { | |
onRebuild: (error, result) => { | |
if (error) { | |
console.error(error); | |
return; | |
} | |
result.warnings.forEach(warning => console.warn(warning)); | |
startServer(); | |
}, | |
} | |
: false, | |
}); | |
buildRes.warnings.forEach(warning => console.warn(warning)); | |
if (dev) { | |
startServer(); | |
} |
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
... | |
type="module" | |
... |
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
Show hidden characters
{ | |
"compilerOptions": { | |
"rootDir": ".", | |
"strict": true, | |
"module": "ESNext", | |
"target": "ESNext", | |
"esModuleInterop": true, | |
"moduleResolution": "node", | |
"outDir": "./dist", | |
"sourceMap": true, | |
"isolatedModules": true, | |
/* Additional Checks */ | |
"noUnusedLocals": true /* Report errors on unused locals. */, | |
"noUnusedParameters": true /* Report errors on unused parameters. */, | |
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */, | |
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */, | |
"noUncheckedIndexedAccess": true | |
}, | |
"exclude": ["./dist", "./scripts"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment