engine
engine copied to clipboard
pc.app is null in ESM build
In UMD build app is added to the pc export which provides pc.app
https://github.com/playcanvas/engine/blob/2a805ddb901c0715deb5d93e4526022be26445f4/src/framework/app-base.js#L97
In the ESM build, app is part of the App-Base module scope and is initialised as part of the app startup. This means that
import { app } from 'playcanvas'
// app === null;
However, importing later it will resolve. This seems error prone and inconsistent with expectations with how imports work. Ideally this should be removed, but may be used by users. There needs to be a solution to migrate away from this.
Definitely used a lot by users, as it is the most common way to reference apps outside of scripts.
To clarify, it is actually exported but it's null until the app has been initialised. Updated comment to reflect this
Here is one example of a use case:
- Some external script wants to subscribe or fire a global event on app:
pc.app.fire('someEvent');. - During development in the console it is convenient to do something like:
pc.app.root.findByName('Entity')for inspecting entities and so on.
Do these cases still work with ESM?
It doesn't really change between ES5/ESM. In either situation pc.app is null until it isn't, so pc.app.fire('someEvent') works once app is available. It's more a question of whether it should be exported