Skip to content

Commit 7427aa9

Browse files
committed
Add lib-compiler
1 parent ed05177 commit 7427aa9

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

lib/lib-compiler/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Command } from '@commander-js/extra-typings';
2+
import chalk from 'chalk';
3+
import { build, context, type BuildOptions } from 'esbuild';
4+
5+
/**
6+
* Returns a {@link Command} that executes with the provided ESBuild options.
7+
*/
8+
export default function getBuildCommand(esbuildOptions: Omit<BuildOptions, 'minify'>) {
9+
return new Command()
10+
.option('--dev', 'If specified, the built output is not minified for easier debugging')
11+
.option('--watch', 'Run esbuild in watch mode')
12+
.action(async ({ dev, watch }) => {
13+
if (watch) {
14+
const buildContext = await context({
15+
...esbuildOptions,
16+
minify: !dev,
17+
plugins: [{
18+
name: 'Watch Plugin',
19+
setup({ onEnd }) {
20+
onEnd(() => {
21+
console.log(chalk.greenBright('Build completed.'));
22+
});
23+
}
24+
}]
25+
});
26+
console.log(chalk.yellowBright('Running ESBuild in watch mode.'));
27+
await buildContext.watch();
28+
} else {
29+
await build(esbuildOptions);
30+
}
31+
});
32+
}

lib/lib-compiler/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@sourceacademy/lib-compiler",
3+
"private": true,
4+
"version": "1.0.0",
5+
"type": "module",
6+
"description": "Wrapper around esbuild and commander to make it simpler to bundle library packages",
7+
"devDependencies": {
8+
"@commander-js/extra-typings": "^13.0.0",
9+
"typescript": "^5.8.2"
10+
},
11+
"dependencies": {
12+
"chalk": "^5.0.1",
13+
"commander": "^13.0.0",
14+
"esbuild": "^0.25.5"
15+
},
16+
"exports": {
17+
".": {
18+
"types": "./index.ts",
19+
"default": "./dist/index.js"
20+
}
21+
},
22+
"scripts": {
23+
"build": "yarn tsc"
24+
}
25+
}

lib/lib-compiler/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"include": ["./index.ts"],
3+
"compilerOptions": {
4+
"module": "nodenext",
5+
"moduleResolution": "nodenext",
6+
"outDir": "./dist"
7+
}
8+
}

0 commit comments

Comments
 (0)