obs-websocket-community-projects / obs-websocket-js

Consumes https://github.com/obsproject/obs-websocket
MIT License
667 stars 96 forks source link

Static import problems #170

Open artdevgame opened 4 years ago

artdevgame commented 4 years ago

Description:

I'm trying to use this library in the browser and I'd like to include the dist files with an ES6 import, i.e.:

import OBSWebSocket from '../../../node_modules/obs-websocket-js/dist/obs-websocket.js'

But I'm experiencing a problem with how OBSWebSocket is exported. Currently the dist/obs-websocket.js has the following:

var OBSWebSocket =

This results in an empty object ({}).

If I change the line to read:

export default OBSWebSocket =

I get the result I desire, which is access to the OBSWebSocket constructor.

Is there any reason the dist file can't be updated?

Versions Used (if applicable):

artemjackson commented 4 years ago

bump

artemjackson commented 4 years ago

@artdevgame as a workaround you can do the following:

Add exports-loader to your's webpack-config:

...
rules: [
  ...,
  {
    test: require.resolve('obs-websocket-js/dist/obs-websocket'),
    use: 'exports-loader?OBSWebSocket'
  },
  ...
]
...

After that you can

import OBSWebSocket from 'obs-websocket-js/dist/obs-websocket';

var obs = new OBSWebSocket();

as usual.

P.S. original answer: https://stackoverflow.com/a/51360811

hodbauer commented 4 years ago

Hi, I hope that this issue is the relevant place to ask. I have a project in Angular and I try to add this library.

I success to import the lib with:

import OBSWebSocket from 'obs-websocket-js/dist/obs-websocket';

but then I cannot use all the plus of typescript because the folder of the types is 2 levels above (same level as dist).

How could I enjoy both worlds?

artemjackson commented 4 years ago

Hey there! The issue is no more actual since maintainers added a native support for UMD modules: https://github.com/haganbmj/obs-websocket-js/commit/1f7f9f45cfc738e040ad0ce5457c126501c8ec08 Anyone who's on the boat should update to obs-websocket-js@4

artemjackson commented 4 years ago

@hodbauer Since support for UMD is here you can now just

import OBSWebSocket from 'obs-websocket-js';

const ws = new OBSWebSocket()

ws.connect <- IDE TS autocompletion