Skip to content

Instantly share code, notes, and snippets.

@istarkov
Created May 22, 2021 13:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save istarkov/b2c26918c8a4765f48b8bce99eec0992 to your computer and use it in GitHub Desktop.
Save istarkov/b2c26918c8a4765f48b8bce99eec0992 to your computer and use it in GitHub Desktop.
esbuild node app setup for latest stable node > 14.16.0
// 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();
}
...
type="module"
...
{
"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