Search code examples
javascriptsolace

encapsulate solace into a javascript library


i'm writing a communication layer in javascript for my company, that will use solace to send and receive message. To do that i need to encapsulate Solace, so all the user have to do is just add a reference to our new library, and start using it, without even knowing anything about Solace or referencing it.

That being said, the first thing i need to do is add a reference to Solace in my own library, and that's the issue. The Solace sample for javascript are very clear on how to use directly on a website, where they reference the lib in the tag, but obviously i can't do the same. This what i tried:

var solace = require("../../Solace/solclient-debug.js");

then later (which is coming directly from the sample)

var factoryProps = new solace.SolclientFactoryProperties();

But i get an error saying solace is undefined. Anyomne know how to do that? It's probably a silly question, but my background in javascript is really limited, i'm coming from .Net


Solution

  • With clientside JavaScript, you have basically three choices. You can: 1) add a script tag for each js file (library) you want to include, 2) concatenate all the libraries you want into one file, then include that one tag, or 3) load one file that dynamically adds script tags to the page to load the other libraries.

    Technically, you could also pull through Ajax and run eval(), but that's generally frowned upon.

    The most common one to use these days is probably#2, but doing it by running a tool like webpack, grunt, gulp, etc is typical since it allows you to build in a more standardized way.

    Webpack in particular might be interesting to you, since it lets you write your js files in a way that you explicitly define your dependencies through require or import statements, and it follows those up the dependency chain and makes sure everything is concatenated in order.

    But no,there is no native way to use just one script tag and require libraries like you're asking.