Create games using TypeScript, Snowpack, and ESLint with this template

paulasantamaria

Paula Santamaría

Posted on May 16, 2021

Create games using TypeScript, Snowpack, and ESLint with this template

NOTE: ⚠️ The GitHub template presented in this post was archived. I created a new one that has exactly the same features (TS, ESLint, hot reload, GitHub Pages) but instead of Snowpack uses Vite, since Snowpack is now deprecated.

I also took the opportunity to update all dependencies to the latest available version:

GitHub logo pawap90 / phaser3-ts-vite-eslint

A modern Phaser template with TypeScript, Vite, ESLint, and GitHub Pages

Deploy Build

A modern Phaser 3 template: Phaser 3 + TypeScript + Vite + ESLint + GitHub Pages

Phaser Version 3.80.1 TypeScript Version 5.2.2 Vite Version 5.3.4 ESLint Version 8.57.0

A modern phaser 3 tempalte with typescript, vite, eslint, and GitHub pages


Table of contents

Dependencies

Quick start

  1. Get this template: Press over the "Use this template" button. This will allow you to create a new repo with this project's structure on your Github account. Then you can clone it to your local machine.

    Alternatively, you can clone this repo to your machine using the following command.

git clone https://github.com/pawap90/phaser3-ts-vite-eslint.git
Enter fullscreen mode Exit fullscreen mode
  1. Install dependencies: Run the following command from the project's root folder:
npm install
Enter fullscreen mode Exit fullscreen mode
  1. Start the local development server:
npm run dev
Enter fullscreen mode Exit fullscreen mode

Go to your browser and navigate to http://localhost:5173. You should see this beauty:

Acho the pup bouncing around

Project structure

├── /.github
│   └── /workflows
│           ├── build.yml
│           └── deploy.yml
├── /public
│       ├── acho.png
│       └── ground.png
├── /src
│   ├── /scenes
│

If you follow me on Twitter or Instagram, you may know I've been trying out Phaser lately.

Phaser is an open-source game framework that allows you to develop games using HTML and Javascript.

I've always been interested in Game development, but I hadn't found the time in the past to really get into it. I tried some things with Unity over the years, but I was focused on University and work, so those projects always got left behind at some point. Now that I graduated, I have more time to explore these kinds of things.

So I started going through some tutorials and doing some experiments with the framework. To get started, all you need is a local web server and getting Phaser through NPM or a CDN, for example. Browsing online, I found some templates that used No.jsde as a web server and installed Phaser through NPM, so I decided to make my own template using a modern tech stack.

And here it is:

GitHub logo pawap90 / phaser3-ts-snowpack-eslint

A modern Phaser 3 template with TypeScript, Snowpack & ESLint

Warning

This repository has been archived.

If you're looking for a Phaser Template, please use this one: github.com/pawap90/phaser3-ts-vite-eslint


Build GitHub Pages Deploy

Phaser Version 3.60.0 TypeScript Version 5.1.6 Snowpack Version 3.8.8 ESLint Version 8.46.0

A modern Phaser 3 template: Phaser 3 + TypeScript + Snowpack + ESLint

A modern Phaser 3 template: Develop your game using Typescript, keep your codebase clean with ESLint, and enjoy lightning-fast live updates thanks to Snowpack.


Table of contents

Dependencies

Quick start

  1. Get this template: Press over the "Use this template" button. This will allow you to create a new repo with this project's structure on your Github account. Then you can clone it to your local machine.

    Alternatively, you can clone this repo to your machine using the following command.

git clone https://github.com/pawap90/phaser3-ts-snowpack-eslint.git
Enter fullscreen mode Exit fullscreen mode
  1. Install dependencies: Run the following command from the project's root folder:
npm install
Enter fullscreen mode Exit fullscreen mode
  1. Start the local development server:
npm start
Enter fullscreen mode Exit fullscreen mode

Go to your browser and navigate to http://localhost:8000. You should see…

About the template

This template allows you to develop your game using TypeScript, keep your codebase clean with ESLint, and enjoy lightning-fast live updates thanks to Snowpack.

I decided to use TypeScirpt instead of JavaScript after doing some tutorials. I tried both options, and I just felt more comfortable with TypeScript.

Build it with Snowpack

When the time came to choose a bundler, I decided to give Snowpack a try since I've heard many good things about it. And honestly, I was blown away by how fast it is. The development server starts in 14ms on my machine, and the live updates are fantastic. I can play around with Phaser and see every update on the browser almost instantly.

I also used Snowpack's built-in optimizations to minify and bundle the build and make it lighter for production. This Snowpack feature is not yet production-ready (according to the docs), but I've done a few tests, published some sample games, and I haven't had any issues with it so far.

Minimalistic

The template is very minimalistic, meaning that it only has what's needed to run and build the project, plus a Sample scene to check everything is working correctly.

Project structure

├───public/                         Public static files
│   ├───assets/                     Sample assets
│   │   ├───banner.png
│   │   ├───acho.png
│   │   └───ground.png
│   └───index.html                  HTML file where our game will be loaded
├───src/                            Game logic goes here
│   ├───scenes/                     Game scenes
│   │   ├───InitialScene.ts         Initial sample scene
│   │   └───PreloaderScene.ts       Scene preloader
│   └───Main.ts                     Phaser game configuration
├───.eslintignore                   Files that should be ignored by ESLint  
├───.eslintrc.js                    ESLint configuration file
├───.gitignore                      Files that should not be pushed to the repo
├───package.json                    Project scripts, dependencies and metadata
├───snowpack.config.js              Snowpack configuration file
└───tsconfig.json                   Typescript configuration file
Enter fullscreen mode Exit fullscreen mode

You can remove everything in the public/assets folder. But I recommend you first run the project once and make sure everything is installed and running properly.

Clean and safe

It's focused on keeping the codebase clean and safe, thanks to TypeScript and ESLint.

TypeScript's configuration contains some flags that set some checks like "strict" and "noImplicitAny" to reduce errors. You can always change that in the tsconfig.json file if you prefer different settings.

I also added a prebuild script that uses tsc and ESLint to compile and lint the project before building it to ensure the build is safe to publish.

So when you run npm run build, the prebuild will be executed first and fail if there are errors. This script could be easily executed in a CI pipeline, for example, to make sure you don't merge or deploy the project if there are issues.

ESLint

I added ESLint to keep the codebase clean and consistent. The configuration can be found in .eslint.js in case you want to add your own rules or modify something.

There are also a few scripts I added to the package.json to check for errors or styling issues:

# Print the list of problems found
npm run lint

# Some of the issues can be automatically fixed using:
npm run lint:fix
Enter fullscreen mode Exit fullscreen mode

Beautiful and classy 🧐🎩

Once you clone the template and install the dependencies, you'll be blessed with this beauty:

Acho the pup bouncing around


If you want to know more about my Game Dev journey, follow me on Instagram or Twitter. I'll be sharing my progress, tips, and tricks, and probably lots of bloopers and fails.

💖 💪 🙅 🚩
paulasantamaria
Paula Santamaría

Posted on May 16, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related