Skip to content

Commit 9331101

Browse files
committed
feat: configure esbuild for Node.js environment with dual module support
1 parent da80312 commit 9331101

File tree

10 files changed

+465
-53
lines changed

10 files changed

+465
-53
lines changed

packages/mcp-express/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 dependencies that shouldn't be bundled
14+
external: ['express', 'cors', '@brionmario-experimental/mcp-node'],
15+
sourcemap: true,
16+
minify: true,
17+
target: 'node18', // Target Node.js version
18+
};
19+
20+
// Build ESM version
21+
async function buildESM() {
22+
await build({
23+
...commonOptions,
24+
outfile: 'dist/index.js',
25+
format: 'esm',
26+
});
27+
console.log('✅ ESM build complete');
28+
}
29+
30+
// Build CommonJS version
31+
async function buildCJS() {
32+
await build({
33+
...commonOptions,
34+
outfile: 'dist/cjs/index.js',
35+
format: 'cjs',
36+
});
37+
38+
// Create a package.json for the CJS directory to specify type
39+
fs.mkdirSync('dist/cjs', {recursive: true});
40+
fs.writeFileSync('dist/cjs/package.json', JSON.stringify({type: 'commonjs'}, null, 2));
41+
console.log('✅ CJS build complete');
42+
}
43+
44+
// Generate TypeScript declaration files
45+
async function generateTypes() {
46+
try {
47+
await execAsync('tsc --emitDeclarationOnly --declaration --outDir dist');
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-express/package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"module": "dist/index.js",
2222
"exports": {
2323
"import": "./dist/index.js",
24-
"require": "./dist/cjs/index.js"
24+
"require": "./dist/cjs/index.js",
25+
"types": "./dist/index.d.ts"
2526
},
2627
"files": [
2728
"dist",
@@ -35,7 +36,11 @@
3536
"directory": "packages/mcp-express"
3637
},
3738
"scripts": {
38-
"build": "tsc -p tsconfig.lib.json",
39+
"clean": "rm -rf dist",
40+
"build": "node build.js",
41+
"build:types": "tsc -p tsconfig.lib.json --emitDeclarationOnly",
42+
"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",
43+
"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",
3944
"fix:lint": "eslint --fix --ext .ts,.js src",
4045
"lint": "eslint --ext .ts,.js src"
4146
},
@@ -45,6 +50,7 @@
4550
"@types/node": "^22.15.3",
4651
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?4ee6f6be232d7631999d709a86b91612f1d34ce7",
4752
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?4ee6f6be232d7631999d709a86b91612f1d34ce7",
53+
"esbuild": "^0.25.4",
4854
"eslint": "8.57.0",
4955
"prettier": "^2.6.2",
5056
"typescript": "~5.7.2"
@@ -60,5 +66,8 @@
6066
"cors": "^2.8.5",
6167
"express": "^4.21.2",
6268
"tslib": "^2.8.1"
69+
},
70+
"engines": {
71+
"node": ">=18.0.0"
6372
}
64-
}
73+
}
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"module": "dist/index.js",
2323
"exports": {
2424
"import": "./dist/index.js",
25-
"require": "./dist/cjs/index.js"
25+
"require": "./dist/cjs/index.js",
26+
"types": "./dist/index.d.ts"
2627
},
2728
"files": [
2829
"dist",
@@ -36,14 +37,19 @@
3637
"directory": "packages/mcp-node"
3738
},
3839
"scripts": {
39-
"build": "tsc -p tsconfig.lib.json",
40+
"clean": "rm -rf dist",
41+
"build": "node build.js",
42+
"build:types": "tsc --emitDeclarationOnly --declaration",
43+
"build:esm": "esbuild src/index.ts --bundle --platform=node --outfile=dist/index.js --format=esm --sourcemap --minify --external:jose --external:node-fetch",
44+
"build:cjs": "esbuild src/index.ts --bundle --platform=node --outfile=dist/cjs/index.js --format=cjs --sourcemap --minify --external:jose --external:node-fetch",
4045
"fix:lint": "eslint --fix --ext .ts,.js src",
4146
"lint": "eslint --ext .ts,.js src"
4247
},
4348
"devDependencies": {
4449
"@types/node": "^22.15.3",
4550
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?4ee6f6be232d7631999d709a86b91612f1d34ce7",
4651
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?4ee6f6be232d7631999d709a86b91612f1d34ce7",
52+
"esbuild": "^0.25.4",
4753
"eslint": "8.57.0",
4854
"prettier": "^2.6.2",
4955
"typescript": "~5.7.2"
@@ -55,5 +61,8 @@
5561
"jose": "^6.0.10",
5662
"node-fetch": "^3.3.2",
5763
"tslib": "^2.8.1"
64+
},
65+
"engines": {
66+
"node": ">=18.0.0"
5867
}
59-
}
68+
}

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)