·8 min read

Phaser tree shaking vs PixiJS bundle size

Four Vite builds. Phaser 4.2.1 came out at 344 KB gzipped whether I imported one math helper or a whole game. PixiJS 8.22.0 trimmed.

gamedevvitejavascriptcapacitor

Contents

ASMR Logo Craft Block is React 19 inside a Capacitor 8 WebView, and almost everything you see in it is DOM and CSS: about 5,000 lines of stylesheet, 79 keyframe animations, 54 JSX screens. After a run of rendering bugs I looked at what it would take to drop the DOM entirely and draw the whole game into one canvas. That turns into a choice of engine, and the first question I had about Phaser was whether it supports tree shaking: if I only use part of it, do I only ship part of it?

I didn't want the answer from a forum thread, so I built it. Short version: no. Importing one math helper from Phaser costs the same 344 KB gzipped as a complete game. PixiJS trims.

Nothing was migrated. This is the measurement behind a decision, and the game still ships the way it did.

The four builds

A throwaway project with Phaser 4.2.1, PixiJS 8.22.0 and Vite, four entry files, one vite build each. Two small ones and two that look like the start of a game:

// phaser, smallest possible use
import { Math as M } from "phaser";
console.log(M.Clamp(5, 0, 1));

// phaser, a game with a scene and some text
import * as Phaser from "phaser";
new Phaser.Game({ type: Phaser.AUTO, scene: { create() { this.add.text(0, 0, "hi") } } });

// pixi, smallest possible use
import { Rectangle } from "pixi.js";
console.log(new Rectangle(0, 0, 1, 1).width);

// pixi, an app with a sprite and some text
import { Application, Sprite, Text, Texture } from "pixi.js";
const a = new Application(); await a.init();
a.stage.addChild(new Sprite(Texture.WHITE), new Text({ text: "hi" }));
document.body.appendChild(a.canvas);

Vite was installed unpinned. The output contained a rolldown-runtime chunk, so it was a Rolldown-based Vite 8, and npm's publish dates say 8.3.4 was the newest release that day. I didn't record the exact patch, which is the one number here I can't swear to.

Sizes are every emitted .js file run through gzip -9 and summed:

Build Raw Gzipped Files
Phaser, one math helper 1,342 KB 344 KB 1
Phaser, scene and text 1,342 KB 344 KB 1
PixiJS, one Rectangle 204 KB 61 KB 5
PixiJS, sprite and text 553 KB 166 KB 16

The two Phaser rows are not rounded to look alike. They are the same build.

One false start, in case you copy the loop. My first pass reported 0 KB for both PixiJS builds. I was in zsh, which does not split an unquoted variable on newlines, so cat $js handed sixteen filenames to cat as one path that did not exist. Phaser only got through because it built to a single file. Loop over the files and gzip each one.

Does Phaser support tree shaking?

Not with a plain Vite build. The measurement is the answer: when the smallest import and a full game produce identical output, nothing was dropped.

Two facts in the package line up with that. npm view phaser shows the import entry in its exports map pointing at ./dist/phaser.esm.js, one prebuilt file, and the package.json has no sideEffects field, so the bundler is never told any of it is safe to leave out. Phaser's own README quotes the minified build at 345 KB gzipped, which is within a kilobyte of what I got from importing a clamp function.

I did not test which of those two facts does the damage. I never forced sideEffects: false onto Phaser or pointed Vite at its source tree to see what changed, so "Phaser doesn't tree shake because of X" would be a guess. What I measured is the outcome: physics, tilemaps, cameras, sound and the rest come along whether the game touches them or not.

What Phaser offers instead: a custom build

Phaser's documented route to a smaller bundle is a custom build: a separate webpack configuration where you list the modules to include. The README says you can reduce the size "by tweaking the build settings", and the answers on the Phaser forum point the same way.

I didn't try it, so I have no number for it. The reason I didn't is the part that matters for a small studio: it is a second build system living next to Vite, and the list of modules has to be kept in step with the game by hand. Everything else in my games builds through one standard pipeline, and I would rather pay 344 KB than maintain a second one.

PixiJS tree shaking does work

PixiJS 8.22.0 ships a sideEffects array in its package.json. It names the files that must run on import (the environment setup, and a set of init files for spritesheets, events, accessibility, compressed textures and so on), which tells the bundler that everything else can go if nothing imports it. That's why one Rectangle came to 61 KB and a working app to 166 KB.

Even the smallest build emitted init and browserAll chunks, both on that list. They come along whatever you import, so 61 KB is the smallest PixiJS build I got, not the size of a rectangle.

The 16 files in the app build are mostly renderers. PixiJS picks one at runtime and loads it with a dynamic import(). From autoDetectRenderer.mjs in 8.22.0:

const renderPriority = ["webgl", "webgpu", "canvas"];
// …
const { WebGPURenderer } = await import('./gpu/WebGPURenderer.mjs');
const { WebGLRenderer }  = await import('./gl/WebGLRenderer.mjs');
const { CanvasRenderer } = await import('./canvas/CanvasRenderer.mjs');

So the 166 KB counts all three renderers, and a device only loads one of them. I didn't measure what a single device actually loads, so treat 166 KB as the ceiling.

That file also corrected something I had wrong going in. I thought PixiJS v8 had dropped the Canvas fallback, and that a WebView without WebGL would give me a blank screen. In 8.22 the order is WebGL, then WebGPU, then Canvas2D. That's from the code; I have not run it on a device with WebGL blocked.

Phaser vs PixiJS bundle size, for a game in a WebView

For a Capacitor game the download argument is weak. The JavaScript ships inside the APK either way, so the 178 KB gap is paid once, at install. The case for the smaller bundle is start-up: the WebView has to parse and run all of it before the first frame. I believe that cost is real, but I didn't measure it, and I'm not going to put a millisecond figure on it here.

The bigger difference is what each one is:

  • Phaser is an engine. Scenes, input, tweens and screen scaling come built in, at a fixed 344 KB, inside one package.
  • PixiJS is a renderer. It stays small and you pay for what you import, but the engine layer — scenes, input handling, layout — is code you write yourself.

My games are built from shared packages, one job each, each importing only what it needs. A library that trims fits that, and one that arrives whole does not. So the note I left myself says: if a game ever goes full canvas, it goes with PixiJS.

Why nothing moved

The measurement settled which engine, not whether to switch. Two things kept the game where it is.

The first is that a canvas engine would not have fixed the problems I had actually measured. The worst one, freezes of 0.3 to 1.3 seconds on iPhone, came from playing sound through HTML <audio> in WKWebView, and was already fixed. A slow React re-render during effects was fixed with memoisation. The one place a GPU renderer looked like a clear win was a blurred box-shadow on a moving label, and one shadow is not a reason to rewrite a game.

The second is everything the browser does for free that a canvas does not:

  • Text. Wrapping, fitting and emoji, in every language I ship or plan to ship. Arabic needs shaping and right-to-left on top, and PixiJS's fast path, BitmapText, draws from a pre-made glyph atlas.
  • Layout. CSS flex and grid today, plus 25 uses of env(safe-area-inset-*) for notches and foldables. On a canvas, all of that becomes layout code.
  • Scrolling and buttons. The level map and the shop scroll with momentum because the browser does it.
  • Testing. My device tests find elements by 82 test ids across 24 scenarios. A canvas has no elements to find.

Add the 79 keyframe animations, which would have to be redone as tweens and would not match the originals pixel for pixel, and the answer for this game was no.

Takeaway

If you are choosing between Phaser and PixiJS on bundle size, build the smallest thing you would actually ship with each and gzip the output yourself. It takes ten minutes, and it answers "does Phaser support tree shaking" more reliably than any thread. With Phaser 4.2.1 and Vite 8 the answer is no: the floor and the ceiling are both 344 KB. Then decide whether that is the number that matters, because for my game it wasn't.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.