Skip to content

Commit 533a505

Browse files
committed
Fix sdk compatibility across browser nodejs and modules
1 parent 8a19a96 commit 533a505

File tree

2 files changed

+96
-16
lines changed

2 files changed

+96
-16
lines changed

package.json

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
"agentic-ai"
1313
],
1414
"type": "module",
15-
"main": "./dist/index.js",
16-
"module": "./dist/index.esm.js",
15+
"main": "./dist/index.cjs",
16+
"module": "./dist/index.mjs",
1717
"types": "./dist/index.d.ts",
1818
"exports": {
1919
".": {
20-
"import": "./dist/index.esm.js",
21-
"require": "./dist/index.js",
22-
"types": "./dist/index.d.ts"
20+
"import": {
21+
"types": "./dist/index.d.mts",
22+
"default": "./dist/index.mjs"
23+
},
24+
"require": {
25+
"types": "./dist/index.d.cts",
26+
"default": "./dist/index.cjs"
27+
}
2328
}
2429
},
2530
"files": [
@@ -28,6 +33,7 @@
2833
],
2934
"scripts": {
3035
"build": "rollup -c",
36+
"build:watch": "rollup -c -w",
3137
"clean": "rimraf dist && rimraf node_modules && rimraf package-lock.json",
3238
"docs:api": "typedoc"
3339
},
@@ -37,17 +43,18 @@
3743
"eventemitter3": "^5.0.1",
3844
"file-type": "^21.0.0",
3945
"mime-types": "^3.0.1",
40-
"uuid": "^9.0.1",
41-
"ws": "^8.16.0",
42-
"zod": "^3.22.4",
46+
"rimraf": "^6.0.1",
4347
"typedoc": "^0.28.13",
4448
"typedoc-plugin-markdown": "^4.8.1",
45-
"rimraf": "^6.0.1"
49+
"uuid": "^9.0.1",
50+
"ws": "^8.16.0",
51+
"zod": "^3.22.4"
4652
},
4753
"devDependencies": {
4854
"@rollup/plugin-commonjs": "^25.0.0",
4955
"@rollup/plugin-json": "^6.0.0",
5056
"@rollup/plugin-node-resolve": "^15.2.0",
57+
"@rollup/plugin-replace": "^5.0.7",
5158
"@rollup/plugin-typescript": "^11.1.0",
5259
"@types/jest": "^29.5.14",
5360
"@types/mime-types": "^3.0.1",

rollup.config.js

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import commonjs from '@rollup/plugin-commonjs';
33
import typescript from '@rollup/plugin-typescript';
44
import dts from 'rollup-plugin-dts';
55
import json from '@rollup/plugin-json';
6+
import replace from '@rollup/plugin-replace';
67
import builtins from 'builtin-modules';
78
import { readFileSync } from 'fs';
89

@@ -15,43 +16,115 @@ const allDependencies = [
1516
...builtins // Node.js built-in modules
1617
];
1718

19+
// Node.js-specific dependencies to exclude from browser bundle
20+
const nodeDependencies = [
21+
'file-type',
22+
'ws',
23+
'@opentelemetry/sdk-logs'
24+
];
25+
1826
// Base plugins configuration
1927
const createPlugins = (isBrowser) => [
2028
resolve({
2129
browser: isBrowser,
2230
preferBuiltins: !isBrowser
2331
}),
24-
commonjs(),
32+
commonjs({
33+
transformMixedEsModules: true
34+
}),
35+
json(),
36+
typescript({
37+
tsconfig: './tsconfig.json',
38+
declaration: false,
39+
sourceMap: false
40+
})
41+
];
42+
43+
// Browser-specific plugins with Node.js replacements
44+
const createBrowserPlugins = () => [
45+
replace({
46+
preventAssignment: true,
47+
values: {
48+
// Replace Node.js dynamic imports with empty promises for browser bundle
49+
"import('file-type')": "Promise.resolve({})",
50+
"import('ws')": "Promise.resolve({})",
51+
// Replace Node.js crypto require with Web Crypto API
52+
"const crypto = require('crypto')": "const crypto = globalThis.crypto || window.crypto",
53+
"require('crypto')": "globalThis.crypto || window.crypto"
54+
}
55+
}),
56+
resolve({
57+
browser: true,
58+
preferBuiltins: false
59+
}),
60+
commonjs({
61+
transformMixedEsModules: true
62+
}),
2563
json(),
2664
typescript({
2765
tsconfig: './tsconfig.json',
28-
declaration: false
66+
declaration: false,
67+
sourceMap: false
2968
})
3069
];
3170

3271
const configs = [
33-
// ESM bundle
72+
// ESM bundle (for Node.js and modern bundlers)
3473
{
3574
input: 'src/index.ts',
3675
output: {
37-
file: 'dist/index.esm.js',
76+
file: 'dist/index.mjs',
3877
format: 'es'
3978
},
4079
plugins: createPlugins(false),
4180
external: allDependencies
4281
},
43-
// CommonJS bundle
82+
// CommonJS bundle (for Node.js)
4483
{
4584
input: 'src/index.ts',
4685
output: {
47-
file: 'dist/index.js',
86+
file: 'dist/index.cjs',
4887
format: 'cjs',
4988
exports: 'named'
5089
},
5190
plugins: createPlugins(false),
5291
external: allDependencies
5392
},
54-
// Type definitions
93+
// UMD bundle (for browsers)
94+
{
95+
input: 'src/index.ts',
96+
output: {
97+
file: 'dist/index.umd.js',
98+
format: 'umd',
99+
name: 'UiPath',
100+
globals: {
101+
'file-type': 'FileType',
102+
'ws': 'WebSocket',
103+
'@opentelemetry/sdk-logs': 'OpenTelemetry'
104+
}
105+
},
106+
plugins: createBrowserPlugins(),
107+
external: nodeDependencies // Exclude Node.js-specific deps
108+
},
109+
// Type definitions for ESM
110+
{
111+
input: 'src/index.ts',
112+
output: {
113+
file: 'dist/index.d.mts',
114+
format: 'es'
115+
},
116+
plugins: [dts()]
117+
},
118+
// Type definitions for CommonJS
119+
{
120+
input: 'src/index.ts',
121+
output: {
122+
file: 'dist/index.d.cts',
123+
format: 'es'
124+
},
125+
plugins: [dts()]
126+
},
127+
// Type definitions (main)
55128
{
56129
input: 'src/index.ts',
57130
output: {

0 commit comments

Comments
 (0)