And webpack does not provide global require. We had some discussions about it in github before, but I still can’t find the more universal way to initialize Socket.
This is a good point to ask the community, how to improve this?
Now Drab injects its javascript directly into the page. This is why:
Drab’s JS are dynamic (EEx), they depends on the configuration: for example they injects only the parts of used Drab Modules, so if you don’t want to use Drab.Live, it does not inject that part of javascript. Also, there are configuration options which changes the generation of the JS part.
The JS generation function (Drab.Client.run/2) passes some information from conn to the client, for example - the controller name, in the token, which is checked on each execution of the event handler.
Personally, I find npm and friends a nightmare.
I could extract all Drab JS and put it as an npm package, changing Drab.Client.run/2 to pass all the configuration options and modules to use. I could even split it into individual packages, so you could decide if you want Drab.Live or not. But, would this solve the issue with require and webpack?
Also, I could make require("phoenix").Socket as a configurable option, so you could do something like:
Sorry maybe strange question but why that didnt work ?
I added that to app.js window.socket = require("phoenix").Socket
and in drap.js i changed this.Socket = window.socket
I got that errorthis.Socket is not a constructor.
var params = Object.assign({ __drab_return: this.drab_return_token }, additional_token);
params = Object.assign(params, {__client_lib_version: Drab.client_lib_version});
this.socket = new this.Socket("/socket", { //error at this line
params: params
});
I see. Now it is clearer.
Looks like your window.socket is not a correct object. You may try to debug what is there before you do this.Socket = window.socket in drab.js.
If you have latest drab you can use that way. I copied from drab source code
Custom socket constructor (Webpack “require is not defined” fix)
If you are using JS bundler other than default brunch, the require method may not be availabe
as global. In this case, you might see the error:
require is not defined
in the Drab’s javascript, in line:
this.Socket = require(“phoenix”).Socket;
In this case, you must provide it. In the app.js add a global variable, which will be passed
to Drab later:
window.__socket = require("phoenix").Socket;
Then, tell Drab to use this instead of default require("phoenix").Socket. Add to config.exs:
Uncaught ReferenceError: require is not defined
at app.js:5
(anonymous) @ app.js:5
(index):111 Uncaught ReferenceError: require is not defined
at Object.create ((index):111)
at (index):1085
at (index):1089
Is there a live project with drab as main UI engine? How do you guys use it? I find the whole idea very promising, but since you mention webpack, you still use JS on the front. What part the drab is for then? Just as a wrapper for socket connections?