Skip to content

Commit ed05177

Browse files
committed
Add new lib-compiler package to replace repeated code for building libraries
1 parent 87e7784 commit ed05177

File tree

10 files changed

+63
-96
lines changed

10 files changed

+63
-96
lines changed

eslint.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ export default tseslint.config(
331331
}
332332
},
333333
{
334-
name: 'Rules specifically for buildtools',
335-
files: ['lib/buildtools/**/*.ts'],
334+
name: 'Rules specifically for files that interact with Node only',
335+
files: ['lib/buildtools/**/*.ts', '**/vitest.config.js'],
336336
rules: {
337337
'import/extensions': ['error', 'ignorePackages'],
338338
}

lib/buildtools/build.js

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,20 @@
33
*/
44

55
// @ts-check
6-
import { Command } from '@commander-js/extra-typings';
7-
import chalk from 'chalk';
8-
import { build, context } from 'esbuild';
6+
import getBuildCommand from '@sourceacademy/lib-compiler';
97

10-
const command = new Command()
11-
.option('--dev', 'If specified, the built output is not minified for easier debugging')
12-
.option('--watch', 'Run esbuild in watch mode')
13-
.action(async ({ dev, watch }) => {
14-
/**
15-
* @type {import('esbuild').BuildOptions}
16-
*/
17-
const esbuildOptions = {
18-
entryPoints: ['./src/commands/index.ts'],
19-
banner: {
20-
js: '#!/usr/bin/node'
21-
},
22-
bundle: true,
23-
format: 'esm',
24-
minify: !dev,
25-
outfile: './bin/index.js',
26-
packages: 'external',
27-
platform: 'node',
28-
target: 'node20',
29-
tsconfig: './tsconfig.json',
30-
};
31-
if (watch) {
32-
const buildContext = await context({
33-
...esbuildOptions,
34-
plugins: [{
35-
name: 'Watch Plugin',
36-
setup({ onEnd }) {
37-
onEnd(() => {
38-
console.log(chalk.greenBright('Build completed.'));
39-
});
40-
}
41-
}]
42-
});
43-
console.log(chalk.yellowBright('Running ESBuild in watch mode.'));
44-
await buildContext.watch();
45-
} else {
46-
await build(esbuildOptions);
47-
}
48-
});
8+
const command = getBuildCommand({
9+
entryPoints: ['./src/commands/index.ts'],
10+
banner: {
11+
js: '#!/usr/bin/node'
12+
},
13+
bundle: true,
14+
format: 'esm',
15+
outfile: './bin/index.js',
16+
packages: 'external',
17+
platform: 'node',
18+
target: 'node20',
19+
tsconfig: './tsconfig.json',
20+
});
4921

5022
await command.parseAsync();

lib/buildtools/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"version": "1.0.0",
66
"devDependencies": {
77
"@commander-js/extra-typings": "^13.0.0",
8+
"@sourceacademy/lib-compiler": "workspace:^",
89
"@types/estree": "^1.0.0",
910
"@types/lodash": "^4.14.198",
1011
"@types/node": "^22.15.30",

lib/lintplugin/build.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@
33
*/
44

55
// @ts-check
6-
import { Command } from '@commander-js/extra-typings';
7-
import { build } from 'esbuild';
6+
import getBuildCommand from '@sourceacademy/lib-compiler';
87

9-
const command = new Command()
10-
.option('--dev', 'If specified, the built output is not minified for easier debugging')
11-
.action(async ({ dev }) => {
12-
await build({
13-
entryPoints: ['./src/index.ts'],
14-
bundle: true,
15-
format: 'esm',
16-
minify: !dev,
17-
outfile: './dist.js',
18-
packages: 'external',
19-
platform: 'node',
20-
target: 'node20',
21-
tsconfig: './tsconfig.prod.json'
22-
});
23-
});
8+
const command = getBuildCommand({
9+
entryPoints: ['./src/index.ts'],
10+
bundle: true,
11+
format: 'esm',
12+
outfile: './dist.js',
13+
packages: 'external',
14+
platform: 'node',
15+
target: 'node20',
16+
tsconfig: './tsconfig.prod.json'
17+
});
2418

2519
await command.parseAsync();

lib/lintplugin/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
"typescript-eslint": "^8.33.1"
2525
},
2626
"devDependencies": {
27-
"@commander-js/extra-typings": "^13.0.0",
27+
"@sourceacademy/lib-compiler": "workspace:^",
2828
"@typescript-eslint/rule-tester": "^8.33.1",
29-
"commander": "^13.0.0",
30-
"esbuild": "^0.25.5",
3129
"typescript": "^5.8.2",
3230
"vitest": "^3.2.3"
3331
},

lib/lintplugin/vitest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22
// Lint Plugin vitest config
33
import { defineProject, mergeConfig } from 'vitest/config';
4-
import rootConfig from '../../vitest.config';
4+
import rootConfig from '../../vitest.config.js';
55

66
export default mergeConfig(
77
rootConfig,

lib/markdown-tree/build.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
*/
44

55
// @ts-check
6-
import { Command } from '@commander-js/extra-typings';
7-
import { build } from 'esbuild';
6+
import getBuildCommand from '@sourceacademy/lib-compiler';
87

9-
const command = new Command()
10-
.option('--dev', 'If specified, the built output is not minified for easier debugging')
11-
.action(async ({ dev }) => {
12-
await build({
13-
entryPoints: ['./src/index.ts'],
14-
bundle: true,
15-
format: 'cjs',
16-
minify: !dev,
17-
// Something just doesn't work right (something requires the 'node:process' module which can't be found)
18-
// when its compiled to ESM, so unfortunately we need to compile this to CJS instead.
19-
outfile: './dist.cjs',
8+
const command = getBuildCommand({
9+
entryPoints: ['./src/index.ts'],
10+
bundle: true,
11+
format: 'cjs',
12+
// Something just doesn't work right (something requires the 'node:process' module which can't be found)
13+
// when its compiled to ESM, so unfortunately we need to compile this to CJS instead.
14+
outfile: './dist.cjs',
2015

21-
// Node builtin modules are present at runtime but our lodash dependency is not
22-
// That's why we don't externalize lodash when bundling with esbuild
23-
platform: 'node',
24-
target: 'node20',
25-
tsconfig: './tsconfig.json'
26-
});
27-
});
16+
// Node builtin modules are present at runtime but our lodash dependency is not
17+
// That's why we don't externalize lodash when bundling with esbuild
18+
platform: 'node',
19+
target: 'node20',
20+
tsconfig: './tsconfig.json'
21+
});
2822

2923
await command.parseAsync();

lib/markdown-tree/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
"private": true,
55
"type": "module",
66
"devDependencies": {
7-
"@commander-js/extra-typings": "^13.0.0",
7+
"@sourceacademy/lib-compiler": "workspace:^",
88
"@types/lodash": "^4.14.198",
99
"@types/markdown-it": "^14.1.2",
10-
"commander": "^13.0.0",
11-
"esbuild": "^0.25.5",
1210
"typescript": "^5.8.2",
1311
"vitest": "^3.2.3"
1412
},

vitest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ export default defineConfig({
2828
]
2929
},
3030
projects: [
31+
'./devserver',
3132
'./lib/*/vitest.config.js',
32-
'./devserver/vite.config.ts',
33-
'./src/*/vitest.config.js'
33+
'./src/bundles',
34+
'./src/tabs'
3435
],
3536
root: import.meta.dirname,
3637
silent: 'passed-only',

yarn.lock

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,14 +3320,24 @@ __metadata:
33203320
languageName: unknown
33213321
linkType: soft
33223322

3323-
"@sourceacademy/lint-plugin@workspace:^, @sourceacademy/lint-plugin@workspace:lib/lintplugin":
3323+
"@sourceacademy/lib-compiler@workspace:^, @sourceacademy/lib-compiler@workspace:lib/lib-compiler":
33243324
version: 0.0.0-use.local
3325-
resolution: "@sourceacademy/lint-plugin@workspace:lib/lintplugin"
3325+
resolution: "@sourceacademy/lib-compiler@workspace:lib/lib-compiler"
33263326
dependencies:
33273327
"@commander-js/extra-typings": "npm:^13.0.0"
3328-
"@typescript-eslint/rule-tester": "npm:^8.33.1"
3328+
chalk: "npm:^5.0.1"
33293329
commander: "npm:^13.0.0"
33303330
esbuild: "npm:^0.25.5"
3331+
typescript: "npm:^5.8.2"
3332+
languageName: unknown
3333+
linkType: soft
3334+
3335+
"@sourceacademy/lint-plugin@workspace:^, @sourceacademy/lint-plugin@workspace:lib/lintplugin":
3336+
version: 0.0.0-use.local
3337+
resolution: "@sourceacademy/lint-plugin@workspace:lib/lintplugin"
3338+
dependencies:
3339+
"@sourceacademy/lib-compiler": "workspace:^"
3340+
"@typescript-eslint/rule-tester": "npm:^8.33.1"
33313341
eslint: "npm:^9.31.0"
33323342
typescript: "npm:^5.8.2"
33333343
vitest: "npm:^3.2.3"
@@ -3348,11 +3358,9 @@ __metadata:
33483358
version: 0.0.0-use.local
33493359
resolution: "@sourceacademy/markdown-plugin-directory-tree@workspace:lib/markdown-tree"
33503360
dependencies:
3351-
"@commander-js/extra-typings": "npm:^13.0.0"
3361+
"@sourceacademy/lib-compiler": "workspace:^"
33523362
"@types/lodash": "npm:^4.14.198"
33533363
"@types/markdown-it": "npm:^14.1.2"
3354-
commander: "npm:^13.0.0"
3355-
esbuild: "npm:^0.25.5"
33563364
lodash: "npm:^4.17.21"
33573365
typescript: "npm:^5.8.2"
33583366
vitest: "npm:^3.2.3"
@@ -3367,6 +3375,7 @@ __metadata:
33673375
resolution: "@sourceacademy/modules-buildtools@workspace:lib/buildtools"
33683376
dependencies:
33693377
"@commander-js/extra-typings": "npm:^13.0.0"
3378+
"@sourceacademy/lib-compiler": "workspace:^"
33703379
"@sourceacademy/modules-lib": "workspace:^"
33713380
"@types/estree": "npm:^1.0.0"
33723381
"@types/lodash": "npm:^4.14.198"

0 commit comments

Comments
 (0)