fix(build): preserve live bindings of mutable UMD exports (pc.app)#8837
Merged
Conversation
The UMD footer bridged the esbuild IIFE namespace to the global `exports` object with `Object.assign(exports, pc)`. `Object.assign` invokes each getter once and copies its *value*, so the mutable `app` export (null until an application is created) was snapshotted to null and `pc.app` never updated. Copy the property descriptors instead, so the getter — and therefore the live binding — is preserved on the UMD namespace. The ESM build was already unaffected as it uses native live bindings. Fixes #8836 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
kpal81xd
approved these changes
Jun 4, 2026
Contributor
|
Verified all good in the Editor - Maybe worth adding a test |
mvaligursky
added a commit
that referenced
this pull request
Jun 4, 2026
…8837) The UMD footer bridged the esbuild IIFE namespace to the global `exports` object with `Object.assign(exports, pc)`. `Object.assign` invokes each getter once and copies its *value*, so the mutable `app` export (null until an application is created) was snapshotted to null and `pc.app` never updated. Copy the property descriptors instead, so the getter — and therefore the live binding — is preserved on the UMD namespace. The ESM build was already unaffected as it uses native live bindings. Fixes #8836 Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
4 tasks
mvaligursky
pushed a commit
that referenced
this pull request
Jun 4, 2026
- Point the override-regression reference to #8837 (the PR that made exports getter-only), not #8836 (the pc.app live-binding issue). - Preserve the original export's enumerability in the override setter instead of hardcoding `enumerable: true`, keeping attribute parity. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
mvaligursky
added a commit
that referenced
this pull request
Jun 4, 2026
…gs (#8839) * fix(build): keep UMD exports overridable while preserving live bindings #8837 fixed the live binding of mutable UMD exports (notably `pc.app`) by copying the esbuild namespace getters onto `exports` with `Object.defineProperties(exports, Object.getOwnPropertyDescriptors(pc))`. That made every export getter-only, which silently broke consumers that reassign members of the global `pc` namespace — e.g. the Editor's classic-script worker overrides `pc.createScript` / `pc.registerScript`. With the assignment failing, the engine's real `createScript` ran in the worker and threw `Cannot read properties of undefined (reading 'scripts')` from `AppBase.getApplication()` (no application exists in the worker). Wrap each export getter in an accessor that delegates reads to the live binding (so `pc.app` still updates) but also has a setter, so assignments like `pc.createScript = ...` redefine the property as a writable value and take effect again. Restores the writable-export behaviour of the pre-2.19 (Rollup) UMD build without regressing the live-binding fix. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * docs/fix(build): address PR review on UMD footer - Point the override-regression reference to #8837 (the PR that made exports getter-only), not #8836 (the pc.app live-binding issue). - Preserve the original export's enumerability in the override setter instead of hardcoding `enumerable: true`, keeping attribute parity. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
mvaligursky
added a commit
that referenced
this pull request
Jun 4, 2026
…gs (#8839) * fix(build): keep UMD exports overridable while preserving live bindings #8837 fixed the live binding of mutable UMD exports (notably `pc.app`) by copying the esbuild namespace getters onto `exports` with `Object.defineProperties(exports, Object.getOwnPropertyDescriptors(pc))`. That made every export getter-only, which silently broke consumers that reassign members of the global `pc` namespace — e.g. the Editor's classic-script worker overrides `pc.createScript` / `pc.registerScript`. With the assignment failing, the engine's real `createScript` ran in the worker and threw `Cannot read properties of undefined (reading 'scripts')` from `AppBase.getApplication()` (no application exists in the worker). Wrap each export getter in an accessor that delegates reads to the live binding (so `pc.app` still updates) but also has a setter, so assignments like `pc.createScript = ...` redefine the property as a writable value and take effect again. Restores the writable-export behaviour of the pre-2.19 (Rollup) UMD build without regressing the live-binding fix. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * docs/fix(build): address PR review on UMD footer - Point the override-regression reference to #8837 (the PR that made exports getter-only), not #8836 (the pc.app live-binding issue). - Preserve the original export's enumerability in the override setter instead of hardcoding `enumerable: true`, keeping attribute parity. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
kpal81xd
added a commit
that referenced
this pull request
Jun 4, 2026
… gate The mocha suite runs against src/ and CI only publints the bundles, so build-output regressions ship undetected (e.g. #8836 pc.app null in UMD, #8839 getter-only exports breaking pc.createScript overrides). - test/bundles/: post-build mocha suite (npm run test:build) loading the built bundles (rel/dbg/prf/min x UMD/ESM) - each loads (UMD global + CJS, ESM import) and boots an Application; runtime exports match src/index.js; pc.app live binding (#8836/#8837); UMD exports overridable (#8839); plus a blanket invariant that every UMD export is a live + overridable accessor. - api-extractor: public API surface gate. npm run api:check verifies the built .d.ts against etc/playcanvas.api.md (compact colored diff on failure); npm run api:update regenerates it. Scratch report under build/.api-extractor/. New dev dependency: @microsoft/api-extractor. - CI: run test:build in the build job; api:check in the typescript-declarations job. Closes #8838
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Since 2.19.0,
pc.appis permanentlynullin the UMD / global build (playcanvas.js), breaking any project or script that reads the globalpc.app(e.g. the classic orbit-cameramouse-inputscript used by the WebXR AR Starter Kit). The ESM build is unaffected.Fixes #8836. Also explains forum thread #42337.
Root cause
2.19.0 migrated the JS builds to esbuild (#8722). The UMD output wraps the engine as an esbuild IIFE whose exports are live getters (
app: () => app), then bridges that namespace to the outer UMDexports(=window.pc) via a hand-written footer:Object.assigninvokes each getter once and copies the value. At module-load time no application exists, soappisnull.window.pc.appbecomes a frozennulldata property; when an app is later constructed and the inner module'sapp = thisruns,window.pc.appnever updates.This only affects mutable exports.
appis the one that bites in practice. Scripts usingthis.appare unaffected (which is why the regression looked project-specific), but scripts reading the globalpc.appbreak.Fix
Copy the property descriptors rather than the values, preserving the getter (and thus the live binding) on the UMD namespace:
Verification
Built both UMD bundles and loaded them as a browser global (
window.pc) in avmsandbox, then constructed anAppBase:pc.appdescriptorpc.app === new AppBase(canvas)Object.assign)value = null(frozen)false— staysnullObject.defineProperties)true— updates correctlyAll 1272 exports remain present; only the binding semantics for mutable exports change.
A regression test would ideally assert
pc.appupdates after construction in the UMD build, but the current mocha suite runs againstsrc/(ESM, where live bindings already work), so there's no build-output test harness to hook into — noting it here as a follow-up.🤖 Generated with Claude Code