Skip to content

Commit 1e4197e

Browse files
committed
fix: 🐛 fix esm/cjs bundling
1 parent fd15726 commit 1e4197e

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

esbuild.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ for (const f of formats)
1616
external: Object.keys(package.dependencies),
1717
format: f.format,
1818
outfile: 'build/index' + f.extension,
19+
target: 'node16',
20+
define: {
21+
'process.env.ES_TARGET': '"' + f.format + '"',
22+
},
23+
})
24+
.catch(e => {
25+
console.log(e)
1926
})
20-
.catch(() => process.exit(1))
2127
.then(() => console.log('Successfully bundled the package in the ' + f.format + ' format'))

src/core/XAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { getObjectChanges } from '../utils/getObjectChanges'
2121
export const DefaultHostname = 'ws.xapi.pro'
2222
export const DefaultRateLimit = 850
2323

24-
type AccountType = 'real' | 'demo'
24+
type AccountType = string
2525
export interface XAPIConfig {
2626
accountId: string
2727
password: string

src/modules/WebSocketWrapper.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { Listener } from './Listener'
22
import { Timer } from './Timer'
3-
3+
import type { WebSocket as WS } from 'ws'
44
export const isNodeJS = () => typeof window === 'undefined' && typeof module !== 'undefined' && module.exports
55

6+
function getWS(): Promise<typeof WS> {
7+
if (process.env.ES_TARGET == 'esm') {
8+
return import('ws')
9+
} else {
10+
// eslint-disable-next-line
11+
return new Promise(resolve => resolve(require('ws')))
12+
}
13+
}
14+
615
export class WebSocketWrapper extends Listener {
716
private ws: any = null
817
private _tryReconnect = false
@@ -38,7 +47,7 @@ export class WebSocketWrapper extends Listener {
3847
this._connectionTimeout.clear()
3948
if (isNodeJS()) {
4049
// NodeJS module
41-
import('ws').then(WebSocketClient => {
50+
getWS().then(WebSocketClient => {
4251
this.ws = new WebSocketClient(this.url)
4352
this.ws.on('open', () => {
4453
this._status = true

0 commit comments

Comments
 (0)