Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct use of p5.js with ES6? #1734

Closed
vilvadot opened this issue Dec 31, 2016 · 4 comments
Closed

Correct use of p5.js with ES6? #1734

vilvadot opened this issue Dec 31, 2016 · 4 comments

Comments

@vilvadot
Copy link

So I set up a little starting project with Webpack interpreting ES6 with babel.

Im loving the way to work but I have a question. For example in this sketch:

screen shot 2016-12-31 at 02 11 04

As you can see I must prepend "p5" to all functions, since its not in the global namespace, is there a way to avoid this for the shake of cleaner and easier to write code?

@lmccart
Copy link
Member

lmccart commented Jan 7, 2017

I'm sorry, I don't think there is at this time, unless you use p5 in "global mode"

@lmccart lmccart closed this as completed Jan 7, 2017
@designbyadrian
Copy link

@albertovilva how is your sketch function triggered? How is setup() and draw() called?

@owlsketch
Copy link

owlsketch commented Oct 10, 2017

@designbyadrian his code resides in this repository: https://github.com/albertovilva/p5-tests.
His sketch is found here, and it is called in app.js

@Streemo
Copy link

Streemo commented May 19, 2018

The p5 npm package is apparently not production-ready:

import p5 from "p5" // loads a 2.6MB (!!!) file, not the minified 388KB file.
import "p5/lib/addons/p5.sound" // loads the non-minified file as well

The p5 NPM package exports the non-minified p5 file. Why? I am looking for a way around this without losing my webpack workflow, so I tried the following:

// npm i --save p5 also comes with minified files
import p5 from "p5/lib/p5.min" //loads the correct minified 388KB file!
import "p5/lib/addons/p5.sound.min" // loads the correct minified sound file and the non-minified (2.6MB) p5.js file.

Everything was going well until I realized p5.sound.min.js was importing an additional unminified copy of p5.js It should not depend on p5.js, rather it should depend on p5.min.js. I cannot fathom a single situation in which one would want non-minified code as a dependency for minified code.

p5.sound.js seems to depend on p5:

// p5.sound.js
...
if (typeof define === 'function' && define.amd)
    define('p5.sound', ['p5'], function (p5) { (factory(p5));});
  else if (typeof exports === 'object')
    factory(require('../p5'));
  else
    factory(root['p5']);
...

p5 seems to use p5.js instead of p5.min.js as the default export (main file):

// p5 package.json
...
"main": "./lib/p5.js",
...

Solution:

p5-sound should be a standalone NPM package with standard a standard dependency on p5. The current situation feels like an anti-pattern. AFAIK the UglifyJSPlugin for Webpack takes p5 and p5 sound (together, 3MB) and minifies it to a 1.35MB bundle, which is awful. Does anyone else have a better solution to this?

@vilvadot did you notice your minified bundle size exceeding 1.2MB as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants