-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Become resilient to lack of global variable #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
Sure it aint really hard, but now we have a situation when shipped code is not usable on its own, which I consider a bad thing. |
The code as written works without modification in Node.js. It is 100% required to use some sort of bundler to use in the browser (if you don't want to use the pre-built versions available in repo and on several CDNs). What use case are you working on? This library needs references to |
Well, I've just assumed that the library targets to be isomorphic and is doing so by assuming |
Any update on this? It's really a pain in the ass to handle these errors. |
Imho the best option would be to just create a global module which would export appropriate global variable based on a simple check and import this in every module which needs a reference to it instead of reaching to a global namespace. Bryce seemed unconvienced when ive raised this issue though. Maybe could be reconsidered? Or closed as not wanted. |
Heya, I'm also looking into this case in the context of Angular CLI (angular/angular-cli#5804). We use That's ok insofar as getting the dev server to run. But for browser apps it is incorrect to have There is a concrete case where this is problematic right now: mapbox/mapbox-gl-js#4359 (which is at the root of angular/angular-cli#5804). The comment there explains the problem pretty well, but the TLDR is that uglifying code that relies on webpack wrapping breaks with It is true that there are a bunch of ways the symptom can be fixed in packages that use sockjs-client/lib/utils/browser-crypto.js Lines 3 to 17 in 0d8627f
Since |
This is also a problem with Angular 6. Are we getting this fixed? |
I have the same problem with angular 6, any fix/ workaround? |
Same problema with Angular6. Any advice? Thanks |
I have this problem as well and needs this very urgent. Any ideas? Thanks in advance. |
I have websockets working with Angular 6 and Spring Boot over sockjs and stomp. One way to clear up this sockjs issue is to remove sockjs from your Angular app and use a cdn instead. Uninstall sockjs then add this to your index: Then add this after the imports in an angular component you want to use sockjs in: This should let you follow the tutorials out their without any issues. Here are two tutorials I used to get Spring and Angular websockets working (using sockjs and stomp): |
@michaelmarshall ,
It seems to me that my spring boot is not really there for the connection although i didn't receive a 404 so at least the url is correct and someone is listening on this port. Does the spring boot websocketconfig needs any additional infos? |
Update.
Thank you so much for pointing me in the right direction. |
For anyone still running into Webpack errors, this worked for me in Webpack v4.8.3: module: {
rules: [
{
test: require.resolve('sockjs-client'),
use: [
{
/**
* sockjs expects variable named 'global' to behave like window
* @see {@link https://webpack.js.org/loaders/imports-loader}
*/
loader: 'imports-loader',
options: 'global=>window',
},
{
/**
* Expose sockjs module as `SockJs` on the window
* @see {@link https://webpack.js.org/loaders/expose-loader/}
*/
loader: 'expose-loader',
options: 'SockJS'
}
]
}, |
To summarize, this is only a problem if you are bundling your own version of this library. There are pre-bundled versions available on CDNs and in the I'd like to hear more about why folks are doing the bundling themselves, instead of using the pre-bundled versions. There are optimizations done for the production build, that you would miss out on, unless you duplicated that configuration. |
From my perspective - it's also a slight problem because of the reason mentioned in the starting post of this discussion:
I'm consuming sockjs-client directly from npm with webpack, which is super common nowadays. Less and less people are using prebundled from CDNs. |
So use the |
Yes, that's an option - but nowadays you have to educate people to do this. The community expectation is that they can just require/import a package without accessing any deep paths. This flow is just easier because you can use all libraries like this without checking what path you need to access for each, so it just scales better. As you can see quite some people were confused by this, looking at comments and issues/PRs referencing this issue. |
This issue has been inactive for 30 days. It will be in closed in 5 days without any new activity. |
Huge amount of users are compiling the lib from the source code when using bundlers like webpack, rollup and probably others.
Having a
global
variable is not guaranteed! I.e. with webpack3 we can specifynode: false
option to stop any node specific polyfills being added to the bundle - this makesglobal
undefined and thus this lib stops working.Imho we shouldnt rely on having it because it requires bundlers / users to provide it.
I'm committed to making a PR fixing this, but first I would like to establish if its something you would like to follow and how this should be accomplished - i.e. by wrapping detecting what
global
is in a module and importing it whenever we need it, or maybe some simple checks against undefined would suffice - but that needs more investigation later.The text was updated successfully, but these errors were encountered: