engine icon indicating copy to clipboard operation
engine copied to clipboard

pc.app is null in ESM build

Open marklundin opened this issue 1 year ago • 4 comments

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.

marklundin avatar Mar 11 '24 11:03 marklundin

Definitely used a lot by users, as it is the most common way to reference apps outside of scripts.

Maksims avatar Mar 11 '24 14:03 Maksims

To clarify, it is actually exported but it's null until the app has been initialised. Updated comment to reflect this

marklundin avatar Mar 11 '24 14:03 marklundin

Here is one example of a use case:

  1. Some external script wants to subscribe or fire a global event on app: pc.app.fire('someEvent');.
  2. 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?

Maksims avatar Mar 11 '24 14:03 Maksims

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

marklundin avatar Mar 11 '24 14:03 marklundin