Skip to content

Commit 92c58b7

Browse files
committed
Add additional info about preparing addon for distribution
1 parent e170773 commit 92c58b7

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

contributor_docs/creating_libraries.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function loadCSVAddon(p5, fn, lifecycles){
235235

236236
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`
237237

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:
239239

240240
```js
241241
function loadCSVAddon(p5, fn, lifecycles){
@@ -258,8 +258,31 @@ function loadCSVAddon(p5, fn, lifecycles){
258258
// Addon library related cleanup
259259
}
260260
}
261+
262+
p5.registerAddon(loadCSVAddon);
261263
```
262264

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+
export function loadCSVAddon(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+
263286
## Next steps
264287

265288
Below are some extra tips about authoring your addon library.

0 commit comments

Comments
 (0)