Setting up a development environment for Vite + Phaser 3 + Visual Studio Code

This article can be read in about 4 minutes.
PR

The purpose 

This guide describes setting up a Vite environment using Phaser 3, a 2D game engine for Javascript.

We assume you already have a working environment with Node.js and other necessary dependencies installed.

PR

Use template

Fortunately, a Vite template utilizing Phaser 3 is available, and we will be using it.

GitHub - phaserjs/template-vite: A Phaser 3 project template that uses Vite for bundling
A Phaser 3 project template that uses Vite for bundling - phaserjs/template-vite

Get template

The template can be obtained in either of the following ways.

clone from the repository

Clone from the link below.

(For those wondering, ‘What is a clone?’, please try downloading the zip file below)

GitHub - phaserjs/template-vite: A Phaser 3 project template that uses Vite for bundling
A Phaser 3 project template that uses Vite for bundling - phaserjs/template-vite

After the clone is complete, copy everything except the .git folder to the project folder.

Down load a Zip

access the following page.

GitHub - phaserjs/template-vite: A Phaser 3 project template that uses Vite for bundling
A Phaser 3 project template that uses Vite for bundling - phaserjs/template-vite

Click “Code” near the center of the top of the screen. 

Click Download zip.

After the download completes, extract the contents of the template-vite-main folder within the ZIP file into your project folder.

Initialize project

Open Visual Studio Code and open your project folder from the “Open Folder…” option in the File menu.

Then, in the Visual Studio Code Terminal (select View > Terminal if it’s not visible), run the following command.

npm install

That’s all the preparation. 

Steps to bundle

Modify code

there are code files under Src folder.

Start test server

Run the following command in the terminal

npm run dev

This template is hardcoded to port 8080. 

bundle

Execute the following commands in the terminal.

npm run build

The bundled files are saved to the dist folder.

Verifying the bundled files

Update the setting file

Open package.json. Add a “Preview” line to the “Scripts” section as shown below, and save.

    "scripts": {
        "dev": "vite --config vite/config.dev.mjs",
        "build": "vite build --config vite/config.prod.mjs",
        "preview": "vite preview "
    },

Execute

Execute the following command in the terminal.

npm run preview

A URL will be displayed below. Accessing this URL in a browser will render the bundled file.

➜ Local: http://localhost:4173/
PR

Result

 We are able to create a Vite environment with Phaser3.

comment

Copied title and URL