Advanced Topics

Publish your sketch

There are several platforms where you can post and share your sketch, such as p5.js Web Editor or OpenProcessing.

If you use Git and store your code on GitHub, you can also publish your sketch with GitHub Pages.
Create a docs directory, and save there index.html together with other files to be loaded (which some templates already do in the dist directory), and then set the docs directory to be published.

Edit HTML file and improve performance

The Template PETR+ does both of the two below:

Node.js module resolution

If you separate your source code into multiple files (modules), sometimes you may want use index.js for treating any directory as a module.
Like this:

// src/my-module/index.js

export { something };
// src/main.js

import { something } from "./my-module";

This is allowed in a Node.js style. So you have to install and use a Rollup plugin @rollup/plugin-node-resolve for bundling that code with Rollup.

And if you use TypeScript, you also have to add the following config in tsconfig.json:

{
  "compilerOptions": {
    "moduleResolution": "node",
  }
}

Improve the build process for development

Creative coding often involves editing the code over and over again to fine-tune the results. So, if you use TypeScript or Rollup or anything else, it may be a little annoying to build the sketch manually and wait for seconds each time.

Here are some tips for doing this process automatically and/or efficiently. These might also be useful for live coding.

TypeScript

If you use TypeScript without any module bundler like Rollup, try adding an npm scripts command in the scripts field of package.json.
Like the below (you can freely rename dev):

{
  "dev": "tsc --watch"
}

then run it with npm run dev.

Rollup

If you use Rollup without TypeScript, try adding a command like the below:

{
  "dev": "rollup --config --watch"
}

then run it with npm run dev.

This will watch your source code files and automatically bundle them (i.e. generate a single *.js file) each time you save one of them.

TypeScript & Rollup

If you use both TypeScript and Rollup, it’s a bit more complicated. There are several ways as follows: