How to load ‘plugin’ for external module
Hi there, I have loaded an external library in my client script in the dependencies, and referenced it in the call back parameters. This library is leaflet and I am using L to interact with it. This all works great.
My issue comes in when I try to load a plugin for this library. If I just add it in the dependencies list after the main library I get various errors (depending on module method ie: cjs, amd, umd) that lead me to believe it is an async issue. The plugin depends on “L” being defined to work.
Would anyone have any suggestions on how I might go about getting this to work? I thought about using the “Packages” parameter in the require.config but the documentation is very limited.
Thanks for you help!
SuiteScript 2.0 compatibility is rated by if it supports amd, uses browser globals like window, and has dependencies.
No support of amd modules (or umd modules) requires you to use a require exports config like the Add a non-AMD library example. I have never been desperate enough to do this. I would rather use a bundler like browserify to package it as a umd module. You may find https://wzrd.in/ easier to use.
If the library uses browser globals, you may have problems with the SuiteScript evaluator throwing errors when saving/uploading files because it does not have browser globals. In those cases I like using an if statement and the local require for conditional loading in my client script
if (typeof window !== "undefined") { require(["./path/to/myLib.js"], function (myLib) {}); }
Last thing is that your external library may use dependencies. You will probably have to look at the source code for this, unless your library is very well documented. For example, Geodesic’s define statement looks like
define(['exports', 'leaflet'], factory)
which means that it expects a module named leaflet to exist. You will want to use a require configuration to Name a Custom Module like in the Import a third-party library example
Thank you for this. I really liked the example of adding exports to the paths in the config file. I’ll try out the local require and browserify looks amazing, I’m sure to use it in the future. As a temporary awful fix I just grabbed all the modules code and added it directly in my script file (removed it’s define and exports).
jeffthomson
I should clarify, with the original package you can call things like L.tilelayer and so on (works fine). But this plugin lets me do things like L.geodesic so it adds the ‘geodesic’ function to the L object.