Commit 44a46ff
fix(core): keep Havok physics out of bundles when features.physics is disabled
Summary:
\`create-iwsdk\` defaults to \`--no-physics\` and most scaffolded apps never enable physics, but every IWSDK build was shipping the full Havok physics engine: a separate Havok JS chunk **plus the ~2 MB \`HavokPhysics.wasm\` binary**, plus a stream of physics symbols (\`PhysicsBody\`/\`PhysicsSystem\`/\`HP_World\`/\`MotionType\`) in the main bundle. Verified on \`examples/poke\` (\`features.physics: false\` set explicitly), pre-fix: main bundle 3.0 MB / 791 KB gzip + 2.0 MB WASM + 36 KB Havok-es chunk — all downloaded on first load.
Two static pins were keeping \`babylonjs/havok\` and the physics module in the static module graph regardless of \`features.physics\`:
1. **\`packages/core/src/init/world-initializer.ts\`** statically imported \`{ PhysicsBody, PhysicsManipulation, PhysicsShape, PhysicsSystem }\` from \`../physics/index.js\` and only *used* them inside \`if (physicsEnabled)\`. The import alone was enough to pull the physics graph in.
2. **\`packages/core/src/physics/physics-system.ts\`** statically imported \`{ MotionType, ... }\` from \`babylonjs/havok\`. \`HP_ShapeId\`/\`HP_WorldId\`/\`MassProperties\`/\`HavokPhysicsWithBindings\` are TS type-only (erased), but \`MotionType\` is a runtime \`enum\` — so \`babylonjs/havok\` was a real runtime dep on this code path. The actual engine load on line 87 (\`await import('babylonjs/havok')\`) was already dynamic and would have given proper code-splitting on its own.
And one barrel re-export was blocking tree-shaking: \`iwsdk/core/package.json\` had no \`sideEffects\` declaration, so every module was assumed to have side effects and Rollup couldn't drop unused exports re-exported via \`export * from './physics/index.js'\` in the core barrel.
This change:
- Converts the world-initializer physics imports to \`await import('../physics/index.js')\` inside the \`physicsEnabled\` branch. \`registerFeatureSystems\` and \`initializeWorld\` become \`async\` accordingly; \`initializeWorld\` already returned \`Promise<World>\` so the externally-visible signature is unchanged.
- Switches \`physics-system.ts\`'s havok imports to \`import type { ... }\`. \`MotionType\` is the only one that was a runtime symbol, and the file already accesses it at runtime through the loaded instance (\`this.havok.MotionType.STATIC\` etc.) — the static import was only providing the type annotation on \`let motionType: MotionType\` (line 307), which is compile-erased.
- Adds \`"sideEffects": ["dist/index.js"]\` to \`iwsdk/core/package.json\`. The barrel's banner \`console.log\` (the IWSDK ASCII art) is the only intentional top-level side effect; every other module is \`createComponent\`/\`createSystem\` const-binding + class definitions with no module-load side effects. Marking just the barrel as having side effects lets Rollup tree-shake unused exports — including everything physics if no consumer references a physics symbol.
After the fix on \`examples/poke\`:
- Entry HTML loads only the main bundle (2.9 MB) — no physics code, no Havok strings.
- The physics module becomes a separate 16 KB chunk that is *generated* but never *fetched* unless \`features.physics\` is true.
- Havok-es chunk (36 KB) and \`HavokPhysics.wasm\` (2 MB) are still emitted as cold assets the physics chunk would import on-demand, but no consumer fetches them.
- Browser-downloaded bytes for a \`physics: false\` page drop from ~5 MB to 2.9 MB (~2.1 MB saved on first load).
\`examples/physics\` (\`features.physics: true\`): main bundle still 2.9 MB; physics chunk loads on demand; Havok WASM still fetched as before. No regression.
Reviewed By: zjm-meta
Differential Revision: D105972540
fbshipit-source-id: 7c532b19469cc0344d859bda1fe258df8192d58b1 parent aad5441 commit 44a46ff
3 files changed
Lines changed: 46 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
11 | 34 | | |
12 | 35 | | |
13 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
56 | 54 | | |
57 | 55 | | |
58 | 56 | | |
| |||
200 | 198 | | |
201 | 199 | | |
202 | 200 | | |
203 | | - | |
| 201 | + | |
204 | 202 | | |
205 | 203 | | |
206 | 204 | | |
| |||
234 | 232 | | |
235 | 233 | | |
236 | 234 | | |
237 | | - | |
238 | | - | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
239 | 239 | | |
240 | 240 | | |
241 | 241 | | |
| |||
594 | 594 | | |
595 | 595 | | |
596 | 596 | | |
597 | | - | |
| 597 | + | |
598 | 598 | | |
599 | 599 | | |
600 | 600 | | |
| |||
673 | 673 | | |
674 | 674 | | |
675 | 675 | | |
676 | | - | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
677 | 679 | | |
| 680 | + | |
| 681 | + | |
678 | 682 | | |
679 | 683 | | |
680 | 684 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
9 | 16 | | |
10 | 17 | | |
11 | 18 | | |
| |||
0 commit comments