How to ship javascript library with plugin?


(OG) #1

Hi,
I’d like to ask what is recommended way to ship javascript library with some plugin? I can load it asynchronously from external url using e.g.

Em.$.getScript('https://cdnjs.cloudflare.com/ajax/libs/......min.js').done(function() {
...
}

But I’d prefer storing library locally. Normally I’d use vendor.js to include it and have it loaded before the plugin code, but this approach needs hacking with the Discourse core… So what is recommended way and where to add external js library to plugin? Is there some convention? Thx.


(Rafael dos Santos Silva) #2

Just add the js lib at the assets folder in your plugin, or if available, as a gem dependency.


(OG) #3

Thanks… In the assets folder will it be loaded automatically or do I need to take care of asynchronous load as quoted in previous post? I need to load library before running plugin code.


(Rafael dos Santos Silva) #4

It’s loaded in the plugin-third-party file, inside the HEAD. You can use ES6 modules to require stuff.


(Tadej Novak) #5

I asked a similar question a few days ago (Existing JS module import into ES6 plugin). The problem I have is, that I want it included server side and I have no idea on which path I should require it in ES6 modules.


(Rafael dos Santos Silva) #6

Got back to you there, so we don’t pollute here.


(OG) #7

Thanks… placing js lib in asset folder and register_asset in plugin.rb did the trick.