You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contributor_docs/creating_libraries.md
+24-1Lines changed: 24 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -235,7 +235,7 @@ function loadCSVAddon(p5, fn, lifecycles){
235
235
236
236
Notice that in the lifecycle functions you have access to `this` which refers to the current `p5` instance same as you would have in the functions attached to `fn`
237
237
238
-
Here is what your p5.loadcsv.js file should look like at the end of this tutorial:
238
+
Here is what your p5.loadcsv.js file should look like at this point in the tutorial:
239
239
240
240
```js
241
241
functionloadCSVAddon(p5, fn, lifecycles){
@@ -258,8 +258,31 @@ function loadCSVAddon(p5, fn, lifecycles){
258
258
// Addon library related cleanup
259
259
}
260
260
}
261
+
262
+
p5.registerAddon(loadCSVAddon);
261
263
```
262
264
265
+
## Step 7
266
+
As a final step, we will add a few more changes to our addon to prepare it for distribution. There are a few options you may wish to distribute your addon:
267
+
268
+
* As a single JavaScript file which your users will include in their HTML with a `<script>` tag.
269
+
* As an ESM module that your users can use with `<script type="module">`, install from NPM, or any other ESM module usage.
270
+
271
+
Either of the above may also be passed through a build tool to be bundled into a different format. As we can see, there are many different options and below will be a recommendation which is the pattern that p5.js itself uses, you may choose another option that fits your addon.
272
+
273
+
```js
274
+
exportfunctionloadCSVAddon(p5, fn, lifecycles){
275
+
// Addon code ...
276
+
}
277
+
278
+
if (typeof p5 !==undefined) {
279
+
p5.registerAddon(loadCSVAddon);
280
+
}
281
+
```
282
+
283
+
In the above snippet, an additional `if` condition is added around the call to `p5.registerAddon()`. This is done to support both direct usage in ESM modules (where users can directly import your addon function then call `p5.registerAddon()` themselves) and after bundling support regular `<script>` tag usage without your users needing to call `p5.registerAddon()` directly as long as they have included the addon `<script>` tag after the `<script>` tag including p5.js itself.
284
+
285
+
263
286
## Next steps
264
287
265
288
Below are some extra tips about authoring your addon library.
0 commit comments