Skip to content

Commit d284295

Browse files
committed
opdeps docs
1 parent 7bc8bf3 commit d284295

File tree

4 files changed

+84
-13
lines changed

4 files changed

+84
-13
lines changed

md/5_writing_ops/dev_libraries/dev_libraries.md

+84-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,92 @@
11
# Libraries
22

3-
#### How can I use external libraries in cables?
3+
## How can I use external libraries in cables?
44

5-
There is a variety of external libraries available in cables. Once you created your [own OP](https://dev.cables.gl/docs/5_writing_ops/dev_hello_op/dev_hello_op) you find
6-
the selection of libraries from the "code" menu on the right side of the editor, when the op is selected. Select any available
7-
libraries, save and reload the patch and you can use them in your ops.
5+
You can add external libraries to your [own ops](https://dev.cables.gl/docs/5_writing_ops/dev_hello_op/dev_hello_op).<br/>
6+
These can be either uploaded to cables.gl, or be loaded from an external source (be aware of [CORS-Issues](../../4_export_embed/cors)).
87

9-
![Button](img/libs.png)
8+
![](img/add_dependencies.png)
109

11-
### What if the library I need is not in that list?
10+
Add them to the Op via "Manage Op" and then "Add Dependencies". From there you have several options to load a library for your op:
1211

13-
Upload the `.js`-file to any patch, just like you would with images or other assets. For all the ops you have
14-
editing rights for you can now select "use library from patch assets..." from the above dropdown. The filemanager
15-
will open and you can pick the library to use in your own op(s).
12+
![](img/dependency_tabs.png)
13+
14+
### Upload File
15+
16+
This option let's you upload any javascript file to your Op and use it as a library. Any code that is in the file
17+
will be loaded by cables and be included in any export. Libraries are loaded *before* the Op is executed.
18+
19+
Pick a type for your library. If unsure, read on [below](#librarytypes). You can now use all the new functionality in your Op.
20+
21+
### From URL
22+
23+
Pick any library from any source, your own server or a CDN (e.g. [jsDelivr](https://www.jsdelivr.com/) or [cdnjs](https://cdnjs.com/))
24+
and start using the functionality in your Op.
25+
26+
Pick a type for your library. If unsure, read on [below](#librarytypes).
27+
28+
This is a great way to quickly test libraries in cables, there is a few caveats though:
29+
30+
* If the library is no longer available at the given URL, your Op will no longer work.
31+
* Sometimes you will run into [CORS-Issues](../../4_export_embed/cors) that might not be fixable by you, if you do not control the "other side".
32+
* The loading of your patch will now need access to the internet, even in exports.
33+
34+
### From Npm (Standalone only)
35+
36+
In the [standalone version](https://cables.gl/standalone) of cables you can add [npm packages](https://www.npmjs.com/) via this tab.
37+
Simply enter the name of the package (as you would do when running `npm install`) and cables will try to install and load the npm.
38+
39+
Be aware that the [NPM ecosystem](https://www.npmjs.com/) is shared between browsers and "backend systems" ([nodejs](https://nodejs.org/))
40+
and some packages cannot be used by cables, at all. Other packages might be architecture dependent and will not work on all operating systems.
41+
You will have to consult the documentation of the module to find this kind of information.
42+
43+
### Op
44+
45+
Enter a name of any Op to load it's libraries when using your Op. This is useful when you are creating a collection of
46+
Ops that all need to load the same library but you don't want to add it to every individual op.
47+
48+
All libraries are loaded globally and all Ops have access to the global variables provided. This function just
49+
makes sure that you do not need a "LoadMyLibraryOp" before using other ops in your collection. These references to
50+
ops will fail if/once the op is deleted and they will also not automatically update to a newer version, if available.
51+
52+
### Core-Lib
53+
54+
Core-Libs are specific functions of the cables core that went into single modules, small libraries. Since we do not
55+
want to load a lot of code that is not needed for every patch, some ops use these libraries to enhance their functionality
56+
with features from cables core (lights, shadows and physics are some examples).
57+
58+
Pick any core-lib from the list to give your Op the additional functionality.
59+
60+
## Managing dependencies
61+
62+
![](img/manage_dependencies.png)
63+
64+
Once you added a library to an op you can start using it right away. The list of dependencies will show options
65+
to delete or download any custom library added.
66+
67+
<a id="librarytypes"></a>
68+
## Library types
69+
70+
By picking the type of the library ("Common JS" or "JS Module") you tell cables how your library needs to be
71+
loaded (sadly you need to know this before, cables cannot guess this from the sourcecode).
72+
73+
By picking "Common JS" your library will be loaded via adding `<script src="mysuperlib.js"/>` to the DOM and
74+
letting the browser decide what to do. This will add your library to the global scope then. You will the be able
75+
to use that variable of the library in your Op (e.g. [handlebars](https://handlebarsjs.com/) adds `Handlebars` to the global scope for you to use).
76+
77+
If you pick "JS Module" you will be able to define the name of the imported variable, your library will be loaded
78+
by calling `import * as YourName from "mysuperlib.js"` and you will then be able to use `YourName` in your Op.
79+
80+
#### How to best-guess the library type?
81+
82+
* Check the documentation of the library, especially the examples on how to import the lib
83+
* * if it says something like `<script src="mylib.js">` it is most likely "Common JS"
84+
* * if it says something like `import MyLib from "mylib.js"` you have a "JS Module"
85+
* Check the sourcecode
86+
* * search for `module.exports` or `require`, if this is present you will most likely have a "Common JS" library
87+
* * if you find something like `class` or `export const` or `export default`, you have a "JS Module"
88+
* Simply try both, you will see errors in the console that might indicate the type of your library
89+
* * `export declarations may only appear at top level of a module` means you loaded a "JS Module" but have your type as "Common Js"
90+
* * loading a "Common JS" as a "JS Module" might work, but will not have the right contents in your `YourLib` variable and fail when using it
1691

17-
Be aware that this makes the patch the source for that library, once you delete the patch, the ops will no
18-
longer work. Fear not though: cables will warn you before deleting such patches.
1992

20-
If you plan to make your ops public somehow preferrably pick libraries that have been put under MIT-License for the
21-
least conflict with cables' own licencing.
Loading
Loading
Loading

0 commit comments

Comments
 (0)