Skip to content

Commit 21dd191

Browse files
authored
Merge pull request #8 from 3nethz/main
configure esbuild for Node.js environment with dual module support
2 parents da80312 + 905bb43 commit 21dd191

File tree

11 files changed

+469
-51
lines changed

11 files changed

+469
-51
lines changed

.changeset/puny-icons-play.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@brionmario-experimental/mcp-express': patch
3+
'@brionmario-experimental/mcp-node': patch
4+
---
5+
6+
configure esbuild for Node.js environment with dual module support

packages/mcp-express/build.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// build.js
2+
import {build} from 'esbuild';
3+
import {exec} from 'child_process';
4+
import {promisify} from 'util';
5+
import fs from 'fs';
6+
7+
const execAsync = promisify(exec);
8+
9+
// Common build options
10+
const commonOptions = {
11+
entryPoints: ['src/index.ts'],
12+
bundle: true,
13+
platform: 'node',
14+
// External dependencies that shouldn't be bundled
15+
external: ['express', 'cors', '@brionmario-experimental/mcp-node'],
16+
sourcemap: true,
17+
minify: true,
18+
target: 'node18', // Target Node.js version
19+
};
20+
21+
// Build ESM version
22+
async function buildESM() {
23+
await build({
24+
...commonOptions,
25+
outfile: 'dist/index.js',
26+
format: 'esm',
27+
});
28+
console.log('✅ ESM build complete');
29+
}
30+
31+
// Build CommonJS version
32+
async function buildCJS() {
33+
await build({
34+
...commonOptions,
35+
outfile: 'dist/cjs/index.js',
36+
format: 'cjs',
37+
});
38+
39+
// Create a package.json for the CJS directory to specify type
40+
fs.mkdirSync('dist/cjs', {recursive: true});
41+
fs.writeFileSync('dist/cjs/package.json', JSON.stringify({type: 'commonjs'}, null, 2));
42+
console.log('✅ CJS build complete');
43+
}
44+
45+
// Generate TypeScript declaration files
46+
async function generateTypes() {
47+
try {
48+
// Using the lib config to generate declarations
49+
await execAsync('tsc -p tsconfig.lib.json --emitDeclarationOnly');
50+
console.log('✅ TypeScript declarations generated');
51+
} catch (error) {
52+
console.error('❌ Error generating TypeScript declarations:', error);
53+
process.exit(1);
54+
}
55+
}
56+
57+
// Clean previous build
58+
async function clean() {
59+
try {
60+
await execAsync('rm -rf dist');
61+
console.log('✅ Previous build cleaned');
62+
} catch (error) {
63+
console.error('❌ Error cleaning previous build:', error);
64+
}
65+
}
66+
67+
// Main build function
68+
async function runBuild() {
69+
try {
70+
console.log('🚀 Starting build process...');
71+
72+
await clean();
73+
await Promise.all([buildESM(), buildCJS()]);
74+
await generateTypes();
75+
76+
console.log('✅ Build completed successfully!');
77+
} catch (error) {
78+
console.error('❌ Build failed:', error);
79+
process.exit(1);
80+
}
81+
}
82+
83+
runBuild();

packages/mcp-express/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
"directory": "packages/mcp-express"
3636
},
3737
"scripts": {
38-
"build": "tsc -p tsconfig.lib.json",
38+
"clean": "rm -rf dist",
39+
"build": "node build.js",
40+
"build:types": "tsc --emitDeclarationOnly --declaration",
41+
"build:esm": "esbuild src/index.ts --bundle --platform=node --outfile=dist/index.js --format=esm --sourcemap --minify --external:express --external:cors --external:@brionmario-experimental/mcp-node",
42+
"build:cjs": "esbuild src/index.ts --bundle --platform=node --outfile=dist/cjs/index.js --format=cjs --sourcemap --minify --external:express --external:cors --external:@brionmario-experimental/mcp-node",
3943
"fix:lint": "eslint --fix --ext .ts,.js src",
4044
"lint": "eslint --ext .ts,.js src"
4145
},
@@ -45,6 +49,7 @@
4549
"@types/node": "^22.15.3",
4650
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?4ee6f6be232d7631999d709a86b91612f1d34ce7",
4751
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?4ee6f6be232d7631999d709a86b91612f1d34ce7",
52+
"esbuild": "^0.25.4",
4853
"eslint": "8.57.0",
4954
"prettier": "^2.6.2",
5055
"typescript": "~5.7.2"
@@ -60,5 +65,8 @@
6065
"cors": "^2.8.5",
6166
"express": "^4.21.2",
6267
"tslib": "^2.8.1"
68+
},
69+
"engines": {
70+
"node": ">=18.0.0"
6371
}
64-
}
72+
}
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4+
"outDir": "./dist",
45
"declaration": true,
5-
"outDir": "dist",
6-
"declarationDir": "dist",
6+
"declarationMap": true,
7+
"inlineSources": true,
78
"types": ["node"]
89
},
9-
"exclude": [
10-
"**/*.spec.ts",
11-
"**/*.test.ts",
12-
"**/*.spec.tsx",
13-
"**/*.test.tsx",
14-
"**/*.spec.js",
15-
"**/*.test.js",
16-
"**/*.spec.jsx",
17-
"**/*.test.jsx"
18-
],
19-
"include": ["src/**/*.js", "src/**/*.ts", "types/**/*.d.ts"]
10+
"include": ["src/**/*.ts"],
11+
"exclude": ["**/*.spec.ts", "**/*.test.ts", "jest.config.ts"]
2012
}
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "dist",
4+
"outDir": "./dist/spec",
55
"module": "commonjs",
66
"types": ["jest", "node"]
77
},
8-
"include": [
9-
"test-configs",
10-
"jest.config.js",
11-
"**/*.test.ts",
12-
"**/*.spec.ts",
13-
"**/*.test.js",
14-
"**/*.spec.js",
15-
"**/*.d.ts"
16-
]
8+
"include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts", "jest.config.ts"]
179
}

packages/mcp-node/build.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {build} from 'esbuild';
2+
import {exec} from 'child_process';
3+
import {promisify} from 'util';
4+
import fs from 'fs';
5+
6+
const execAsync = promisify(exec);
7+
8+
// Common build options
9+
const commonOptions = {
10+
entryPoints: ['src/index.ts'],
11+
bundle: true,
12+
platform: 'node',
13+
external: ['jose', 'node-fetch'], // External dependencies that shouldn't be bundled
14+
sourcemap: true,
15+
minify: true,
16+
target: 'node18', // Target Node.js version
17+
};
18+
19+
// Build ESM version
20+
async function buildESM() {
21+
await build({
22+
...commonOptions,
23+
outfile: 'dist/index.js',
24+
format: 'esm',
25+
});
26+
console.log('✅ ESM build complete');
27+
}
28+
29+
// Build CommonJS version
30+
async function buildCJS() {
31+
await build({
32+
...commonOptions,
33+
outfile: 'dist/cjs/index.js',
34+
format: 'cjs',
35+
});
36+
37+
// Create a package.json for the CJS directory to specify type
38+
fs.mkdirSync('dist/cjs', {recursive: true});
39+
fs.writeFileSync('dist/cjs/package.json', JSON.stringify({type: 'commonjs'}, null, 2));
40+
console.log('✅ CJS build complete');
41+
}
42+
43+
// Generate TypeScript declaration files
44+
async function generateTypes() {
45+
try {
46+
// Using the lib config to generate declarations
47+
await execAsync('tsc -p tsconfig.lib.json --emitDeclarationOnly');
48+
console.log('✅ TypeScript declarations generated');
49+
} catch (error) {
50+
console.error('❌ Error generating TypeScript declarations:', error);
51+
process.exit(1);
52+
}
53+
}
54+
55+
// Clean previous build
56+
async function clean() {
57+
try {
58+
await execAsync('rm -rf dist');
59+
console.log('✅ Previous build cleaned');
60+
} catch (error) {
61+
console.error('❌ Error cleaning previous build:', error);
62+
}
63+
}
64+
65+
// Main build function
66+
async function runBuild() {
67+
try {
68+
console.log('🚀 Starting build process...');
69+
70+
await clean();
71+
await Promise.all([buildESM(), buildCJS()]);
72+
await generateTypes();
73+
74+
console.log('✅ Build completed successfully!');
75+
} catch (error) {
76+
console.error('❌ Build failed:', error);
77+
process.exit(1);
78+
}
79+
}
80+
81+
runBuild();

packages/mcp-node/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@
3636
"directory": "packages/mcp-node"
3737
},
3838
"scripts": {
39-
"build": "tsc -p tsconfig.lib.json",
39+
"clean": "rm -rf dist",
40+
"build": "node build.js",
41+
"build:types": "tsc --emitDeclarationOnly --declaration",
42+
"build:esm": "esbuild src/index.ts --bundle --platform=node --outfile=dist/index.js --format=esm --sourcemap --minify --external:jose --external:node-fetch",
43+
"build:cjs": "esbuild src/index.ts --bundle --platform=node --outfile=dist/cjs/index.js --format=cjs --sourcemap --minify --external:jose --external:node-fetch",
4044
"fix:lint": "eslint --fix --ext .ts,.js src",
4145
"lint": "eslint --ext .ts,.js src"
4246
},
4347
"devDependencies": {
4448
"@types/node": "^22.15.3",
4549
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?4ee6f6be232d7631999d709a86b91612f1d34ce7",
4650
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?4ee6f6be232d7631999d709a86b91612f1d34ce7",
51+
"esbuild": "^0.25.4",
4752
"eslint": "8.57.0",
4853
"prettier": "^2.6.2",
4954
"typescript": "~5.7.2"
@@ -55,5 +60,8 @@
5560
"jose": "^6.0.10",
5661
"node-fetch": "^3.3.2",
5762
"tslib": "^2.8.1"
63+
},
64+
"engines": {
65+
"node": ">=18.0.0"
5866
}
59-
}
67+
}

packages/mcp-node/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"esModuleInterop": true,
77
"experimentalDecorators": true,
88
"importHelpers": true,
9-
"lib": ["ESNext"],
9+
"lib": ["ESNext", "DOM", "WebWorker"],
1010
"module": "ESNext",
1111
"moduleResolution": "node",
1212
"skipLibCheck": true,
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4+
"outDir": "./dist",
45
"declaration": true,
5-
"outDir": "dist",
6-
"declarationDir": "dist",
6+
"declarationMap": true,
7+
"inlineSources": true,
78
"types": ["node"]
89
},
9-
"exclude": [
10-
"**/*.spec.ts",
11-
"**/*.test.ts",
12-
"**/*.spec.tsx",
13-
"**/*.test.tsx",
14-
"**/*.spec.js",
15-
"**/*.test.js",
16-
"**/*.spec.jsx",
17-
"**/*.test.jsx"
18-
],
19-
"include": ["src/**/*.js", "src/**/*.ts", "types/**/*.d.ts"]
10+
"include": ["src/**/*.ts"],
11+
"exclude": ["**/*.spec.ts", "**/*.test.ts", "jest.config.ts"]
2012
}
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "dist",
4+
"outDir": "./dist/spec",
55
"module": "commonjs",
66
"types": ["jest", "node"]
77
},
8-
"include": [
9-
"test-configs",
10-
"jest.config.js",
11-
"**/*.test.ts",
12-
"**/*.spec.ts",
13-
"**/*.test.js",
14-
"**/*.spec.js",
15-
"**/*.d.ts"
16-
]
8+
"include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts", "jest.config.ts"]
179
}

0 commit comments

Comments
 (0)