I noticed that the js files added using register_asset
via plugins are passed as a string to eval
function.
The problem with eval is, it doesn’t add the object to the global scope as discussed here. https://stackoverflow.com/questions/4670805/javascript-eval-on-global-scope
which is essential for using a js library.
To be specific, I was trying to use https://github.com/muaz-khan/RecordRTC/blob/master/RecordRTC.js
in a plugin but the functions aren’t accessible in the ember files.
The workaround for this is to process the file with webpack, using libraryTarget
as umd
.
Many of the libraries are processed this way which seems to work out of the box.
Now I’m not an expert on these things, but just wondering whether it makes sense for discourse to add the registered assets to the global scope.
Note: Apologies if some of this sounds silly. I didn’t expect to fiddle with webpack to get js libraries working with discourse which motivated me to post this writeup.