Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[WIP] Refactors to support running in node - #9440

Draft
davepagurek wants to merge 18 commits into
mainfrom
wgpu-refactors
Draft

davepagurek wants to merge 18 commits into
mainfrom
wgpu-refactors

Conversation

@davepagurek

Copy link
Copy Markdown
Contributor

WIP! Trying out some stuff to get a nodejs build to work.

  • Ignoring event handling for now, need to support that somehow later
  • I'm using subpath imports to swap out a platform file for methods that should have separate implementations on node. Just doing the quick thing for now, these APIs probably need more consideration
  • p5.Image is gonna be a big one so I'm just avoiding it
  • It still doesn't actually run yet. But different errors is still progress right???

Comment thread src/app.js
Comment on lines +67 to +68
import { _globalInit } from './core/init';
import { waitForDocumentReady } from '#platform';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea for is to use the app.node.js entry point instead of reusing this one. The other entry point simply don't have the global mode stuff. Would that solution work instead?

If some kind of global mode sketch is preferred, I think the experimental new instance mode p5.ni.js probably can fit in for it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that can work, but might not solve the whole problem, because currently the platform-specific calls are scattered kind of everywhere. Having one #platform file to import from works around that. An alternative could be to have app.node.js put an object on the p5 global with those, so that the references everywhere in p5 could grab it from e.g. p5.platform.handleUncaughtErrors?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can still use the #platform import idea with the app.node.js approach. The question is more around the intended usage pattern:

  1. What import would a node.js app use vs a browser. The idea for the app.node.js is to import p5 from "p5/node" and frontend apps import p5 from "p5" which bundlers can treeshake.
  2. The node.js build don't need to be bundled, just transformed where necessary, meaning app.node.esm.js won't be needed as node.js can import the transformed app.node.js from p5/node exports and resolve normally from there. Unless the idea is to support kinda Deno style URL import where a singled bundled file would be more straightforward?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see. remind me, how does it work currently if you import p5 from an environment that does tree shaking? iirc we still serve built files to some degree to have things like the GLSL imports work? If so, is that system flexible enough to be used for node too, possibly just through an alternate entrypoint if we think initialization would be easier?

If some kind of global mode sketch is preferred, I think the experimental new instance mode p5.ni.js probably can fit in for it?

I think supporting something something like that would be good. I also wonder how possible it is for "true" global mode to work in node. Does globalThis work similarly in node where if you make a function setup() {} it's accessible on globalThis like in the browser? If we hit any weirdness then a p5.ni approach sounds like the right call and we can move that into the core.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently all entry points to each modules will be passed to rolldown for transform which will package up anything that is not internal code nor dependencies (except those that don't have ESM exports), others like GLSL shaders will be inlined as part of this step. Then in user code they will import p5 which points to these transformed code, tree shaking will be done by the user's bundler provided they have the right settings.

For node it works like a typical ESM environment meaning the initialization we depend on with script tag don't run at the right time I believe, another thing is that global mode initialization is guarded behind the load event on window to wait for user's sketch code to load and be attached to window, and I think node does not have an equivalent. If you mean to try to run global mode by manually doing new p5(), I'm not sure, maybe it will work, maybe it has some weird edge cases.

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

Successfully merging this pull request may close these issues.

2 participants