Skip to content

Commit

Permalink
chore(server): build using esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Nov 26, 2023
1 parent b14cd44 commit 2262336
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ build

# dotenv environment variables file
.env

!build.js
63 changes: 63 additions & 0 deletions packages/server/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {rm} from 'fs/promises';
import {createLogger} from '@alwatr/logger';
import {context, analyzeMetafile} from 'esbuild';

const logger = createLogger('@alwatr/storage-server-build', true);

const srcDir = 'src';
const outDir = 'dist';
const srcFilename = 'index';

async function build(cleanMode, watchMode, debugMode, prettyMode) {
logger.logMethodArgs?.('build', {cleanMode, watchMode, debugMode, prettyMode});

const esbuildContext = await context({
entryPoints: [`${srcDir}/${srcFilename}.ts`],

logLevel: 'info',
platform: 'node',
target: 'es2018',
format: 'esm',
conditions: debugMode ? ['development'] : undefined,

minify: !prettyMode,
treeShaking: true,
sourcemap: true,
sourcesContent: debugMode,
bundle: true,
splitting: false,
charset: 'utf8',
legalComments: 'none',
metafile: true,
outbase: srcDir,
outdir: outDir,
entryNames: watchMode ? '[name]' : '[dir]/[name]'
});

if (cleanMode) {
logger.logOther?.('🧹 Cleaning...');
await rm(outDir, {recursive: true, force: true});
}

if (watchMode) {
logger.logOther?.('👀 Watching...');
esbuildContext.watch();
} else {
logger.logOther?.('🚀 Building...');
const buildInfo = await esbuildContext.rebuild();
await esbuildContext.dispose();

if (debugMode) {
logger.logOther?.(await analyzeMetafile(buildInfo.metafile));
}

logger.logOther?.('✅ Done.');
}
}

const cleanMode = process.argv.includes('--clean');
const watchMode = process.argv.includes('--watch');
const debugMode = process.argv.includes('--debug');
const prettyMode = process.argv.includes('--pretty');

build(cleanMode, watchMode, debugMode, prettyMode);
22 changes: 14 additions & 8 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@
"cb": "run-s clean build",
"s": "yarn run start",
"w": "yarn run watch",
"l": "run-s 'lint:*'",
"lint:ts": "tsc --build",
"start": "NODE_OPTIONS=--enable-source-maps run-s clean build serve",
"build": "yarn run build:ts",
"build:ts": "tsc --build",
"build:es": "esbuild src/index.ts --platform=node --target=node19 --bundle --format=esm --minify --sourcemap --outdir=dist --out-extension:.js=.mjs",
"build": "yarn run build:es",
"build:es": "yarn node ./build.js",
"clean": "rm -rf dist build .tsbuildinfo",
"serve": "yarn node --enable-source-maps dist/index.mjs",
"serve:debug": "yarn node --inspect --enable-source-maps dist/index.mjs",
"serve": "yarn node --enable-source-maps dist/index.js",
"serve:debug": "yarn node --inspect --enable-source-maps dist/index.js",
"watch": "run-s clean build && run-p watch:es watch:node",
"watch:node": "nodemon -w dist/ --enable-source-maps dist/index.mjs",
"watch:debug-node": "nodemon -w dist/ --inspect --enable-source-maps dist/index.mjs",
"watch:node": "nodemon -w dist/ --enable-source-maps dist/index.js",
"watch:debug-node": "nodemon -w dist/ --inspect --enable-source-maps dist/index.js",
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
"watch:es": "yarn run build:es --watch"
},
Expand All @@ -59,6 +60,11 @@
"@alwatr/storage-engine": "workspace:^",
"@alwatr/type": "^1.2.1",
"@alwatr/util": "^1.3.1",
"@types/node": "^20.10.0"
"@types/node": "^20.10.0",
"esbuild": "^0.19.7",
"nodemon": "^3.0.1",
"npm-run-all": "^4.1.5",
"tslib": "^2.6.2",
"typescript": "^5.3.2"
}
}
3 changes: 1 addition & 2 deletions packages/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"composite": true,
"tsBuildInfoFile": ".tsbuildinfo",
"rootDir": "src",
"outDir": "build"
// "noEmit": true
"noEmit": true
},

"include": ["src/**/*.ts"],
Expand Down
Loading

0 comments on commit 2262336

Please sign in to comment.