Code Monkey home page Code Monkey logo

template-webpack's Introduction

Phaser Webpack Template

This is a Phaser 3 project template that uses webpack for bundling. It supports hot-reloading for quick development workflow and includes scripts to generate production-ready builds.

This Template is also available as a TypeScript version.

Versions

This template has been updated for:

screenshot

Requirements

Node.js is required to install dependencies and run scripts via npm.

Available Commands

Command Description
npm install Install project dependencies
npm run dev Launch a development web server
npm run build Create a production build in the dist folder

Writing Code

After cloning the repo, run npm install from your project directory. Then, you can start the local development server by running npm run dev.

The local development server runs on http://localhost:8080 by default. Please see the webpack documentation if you wish to change this, or add SSL support.

Once the server is running you can edit any of the files in the src folder. Webpack will automatically recompile your code and then reload the browser.

Template Project Structure

We have provided a default project structure to get you started. This is as follows:

  • index.html - A basic HTML page to contain the game.
  • src - Contains the game source code.
  • src/main.js - The main entry point. This contains the game configuration and starts the game.
  • src/scenes/ - The Phaser Scenes are in this folder.
  • public/style.css - Some simple CSS rules to help with page layout.
  • public/assets - Contains the static assets used by the game.

Handling Assets

Webpack supports loading assets via JavaScript module import statements.

This template provides support for both embedding assets and also loading them from a static folder. To embed an asset, you can import it at the top of the JavaScript file you are using it in:

import logoImg from './assets/logo.png'

To load static files such as audio files, videos, etc place them into the public/assets folder. Then you can use this path in the Loader calls within Phaser:

preload ()
{
    //  This is an example of an imported bundled image.
    //  Remember to import it at the top of this file
    this.load.image('logo', logoImg);

    //  This is an example of loading a static image
    //  from the public/assets folder:
    this.load.image('background', 'assets/bg.png');
}

When you issue the npm run build command, all static assets are automatically copied to the dist/assets folder.

Deploying to Production

After you run the npm run build command, your code will be built into a single bundle and saved to the dist folder, along with any other assets your project imported, or stored in the public assets folder.

In order to deploy your game, you will need to upload all of the contents of the dist folder to a public facing web server.

Customizing the Template

Babel

You can write modern ES6+ JavaScript and Babel will transpile it to a version of JavaScript that you want your project to support. The targeted browsers are set in the .babelrc file and the default currently targets all browsers with total usage over "0.25%" but excludes IE11 and Opera Mini.

"browsers": [
 ">0.25%",
 "not ie 11",
 "not op_mini all"
]

Webpack

If you want to customize your build, such as adding a new webpack loader or plugin (i.e. for loading CSS or fonts), you can modify the webpack/config.js file for cross-project changes, or you can modify and/or create new configuration files and target them in specific npm tasks inside of package.json. Please see the Webpack documentation for more information.

Join the Phaser Community!

We love to see what developers like you create with Phaser! It really motivates us to keep improving. So please join our community and show-off your work 😄

Visit: The Phaser website and follow on Phaser Twitter
Play: Some of the amazing games #madewithphaser
Learn: API Docs, Support Forum and StackOverflow
Discord: Join us on Discord
Code: 2000+ Examples
Read: The Phaser World Newsletter

Created by Phaser Studio. Powered by coffee, anime, pixels and love.

The Phaser logo and characters are © 2011 - 2024 Phaser Studio Inc.

All rights reserved.

template-webpack's People

Contributors

arnekeller avatar candelibas avatar charleskyalo avatar chrisk-7777 avatar chrisl8 avatar evoluogames avatar gammafp avatar photonstorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

template-webpack's Issues

Unable to build project on Node version 18 and above

The project does not build on newer versions of node. I tried on Node version 18.12.1 and 19.1.0. I had to revert Node to LTS Gallium 16.18.1 for the project to build.

npm start

> [email protected] start
> webpack-dev-server --config webpack/base.js --open

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from /var/www/html/games/demo
Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/var/www/html/games/demo/node_modules/webpack/lib/util/createHash.js:135:53)
    at NormalModule._initBuildHash (/var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:417:16)
    at handleParseError (/var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:471:10)
    at /var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:503:5
    at /var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:358:12
    at /var/www/html/games/demo/node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (/var/www/html/games/demo/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at iterateNormalLoaders (/var/www/html/games/demo/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
node:internal/crypto/hash:71
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/var/www/html/games/demo/node_modules/webpack/lib/util/createHash.js:135:53)
    at NormalModule._initBuildHash (/var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:417:16)
    at handleParseError (/var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:471:10)
    at /var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:503:5
    at /var/www/html/games/demo/node_modules/webpack/lib/NormalModule.js:358:12
    at /var/www/html/games/demo/node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (/var/www/html/games/demo/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at Array.<anonymous> (/var/www/html/games/demo/node_modules/loader-runner/lib/LoaderRunner.js:205:4) {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

Node.js v18.12.1

Why are so many phaser objects generated in memory without calling any function of phaser?

I have encountered a strange problem。please help me.

  1. I downloaded this project, and I commented out all lines 4-39 in index.js.
  2. After compiling, run npm start and open chrome. At the same time, open the memory tab page of devtools.
  3. It can be seen that the heap snapshot takes up about 50 M memory.
  4. After clicking take snapshot, you will find that there are many phaser objects (gameobject 20 curve 5 ....)in the memory.

How do i release these phaser objects from memory?
Can it leak memory?

20230406_110854.mp4

Phaser declared as a devDependency

Current Behavior 😯

As the title says: the Phaser package is declared under devDependencies instead of dependencies in package.json.

Expected Behavior 🤔

Phaser should be declared under dependencies.

Steps to Reproduce 🕹

Look at the current package.json.

Context 🔦

If a user of this template creates another package from this template package, Phaser -- the core of the template -- would be excluded when running npm install "$package". Although this may seem like a niche issue, it is also accompanied with an easy and necessary solution. This, too, will align this template with its vanilla JavaScript sibling template.

raw-loader missing in dev dependencies

Hi,

so I just tried to run the template by

  1. Cloning the repo
  2. Running npm install
  3. Running npm run build

However building fails with following error:

Hash: 66188cf95a8197a6a965
Version: webpack 3.10.0
Time: 1827ms
            Asset     Size  Chunks                    Chunk Names
project.bundle.js  2.72 MB       0  [emitted]  [big]  main
 [174] (webpack)/buildin/global.js 509 bytes {0} [built]
 [380] ./src/index.js 529 bytes {0} [built]
 [502] (webpack)/buildin/module.js 517 bytes {0} [built]
    + 1013 hidden modules

ERROR in ./node_modules/phaser/src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js
Module not found: Error: Can't resolve 'raw-loader' in '/home/enba/repos/phaser3-project-template'
 @ ./node_modules/phaser/src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js 5:21-62
 @ ./node_modules/phaser/src/renderer/webgl/WebGLRenderer.js
 @ ./node_modules/phaser/src/boot/CreateRenderer.js
 @ ./node_modules/phaser/src/boot/Game.js
 @ ./node_modules/phaser/src/phaser.js
 @ ./src/index.js

ERROR in ./node_modules/phaser/src/renderer/webgl/pipelines/BitmapMaskPipeline.js
Module not found: Error: Can't resolve 'raw-loader' in '/home/enba/repos/phaser3-project-template'
 @ ./node_modules/phaser/src/renderer/webgl/pipelines/BitmapMaskPipeline.js 5:21-58
 @ ./node_modules/phaser/src/renderer/webgl/WebGLRenderer.js
 @ ./node_modules/phaser/src/boot/CreateRenderer.js
 @ ./node_modules/phaser/src/boot/Game.js
 @ ./node_modules/phaser/src/phaser.js
 @ ./src/index.js

ERROR in ./node_modules/phaser/src/renderer/webgl/pipelines/TextureTintPipeline.js
Module not found: Error: Can't resolve 'raw-loader' in '/home/enba/repos/phaser3-project-template'
 @ ./node_modules/phaser/src/renderer/webgl/pipelines/TextureTintPipeline.js 5:21-59
 @ ./node_modules/phaser/src/renderer/webgl/WebGLRenderer.js
 @ ./node_modules/phaser/src/boot/CreateRenderer.js
 @ ./node_modules/phaser/src/boot/Game.js
 @ ./node_modules/phaser/src/phaser.js
 @ ./src/index.js

ERROR in ./node_modules/phaser/src/renderer/webgl/pipelines/TextureTintPipeline.js
Module not found: Error: Can't resolve 'raw-loader' in '/home/enba/repos/phaser3-project-template'
 @ ./node_modules/phaser/src/renderer/webgl/pipelines/TextureTintPipeline.js 4:21-59
 @ ./node_modules/phaser/src/renderer/webgl/WebGLRenderer.js
 @ ./node_modules/phaser/src/boot/CreateRenderer.js
 @ ./node_modules/phaser/src/boot/Game.js
 @ ./node_modules/phaser/src/phaser.js
 @ ./src/index.js

ERROR in ./node_modules/phaser/src/renderer/webgl/pipelines/FlatTintPipeline.js
Module not found: Error: Can't resolve 'raw-loader' in '/home/enba/repos/phaser3-project-template'
 @ ./node_modules/phaser/src/renderer/webgl/pipelines/FlatTintPipeline.js 6:21-56
 @ ./node_modules/phaser/src/renderer/webgl/WebGLRenderer.js
 @ ./node_modules/phaser/src/boot/CreateRenderer.js
 @ ./node_modules/phaser/src/boot/Game.js
 @ ./node_modules/phaser/src/phaser.js
 @ ./src/index.js

ERROR in ./node_modules/phaser/src/renderer/webgl/pipelines/FlatTintPipeline.js
Module not found: Error: Can't resolve 'raw-loader' in '/home/enba/repos/phaser3-project-template'
 @ ./node_modules/phaser/src/renderer/webgl/pipelines/FlatTintPipeline.js 5:21-56
 @ ./node_modules/phaser/src/renderer/webgl/WebGLRenderer.js
 @ ./node_modules/phaser/src/boot/CreateRenderer.js
 @ ./node_modules/phaser/src/boot/Game.js
 @ ./node_modules/phaser/src/phaser.js
 @ ./src/index.js

ERROR in ./node_modules/phaser/src/renderer/webgl/pipelines/BitmapMaskPipeline.js
Module not found: Error: Can't resolve 'raw-loader' in '/home/enba/repos/phaser3-project-template'
 @ ./node_modules/phaser/src/renderer/webgl/pipelines/BitmapMaskPipeline.js 4:21-58
 @ ./node_modules/phaser/src/renderer/webgl/WebGLRenderer.js
 @ ./node_modules/phaser/src/boot/CreateRenderer.js
 @ ./node_modules/phaser/src/boot/Game.js
 @ ./node_modules/phaser/src/phaser.js
 @ ./src/index.js

Obviously, this is due to a missing dev dependency for raw-loader. After installing it manually, everything worked just fine.

IMO raw-loader should be added to the devDependencies to avoid this additional step.

EDIT: Just noticed the latest commit 448133c , added only 2 days ago, states to "Add(ed) raw-loader and updated version". While this is true for the webpack conf, the commit didn't add the dependency in package.json. Seems like the author just forgot to --save-dev'

Assets are missing from dist

I am trying to deploy my code to Firebase Hosting and the assets are not present in the dist folder. Any suggestions on how to fix that from the yarn build command?

@photonstorm

Asset size limit warning on running build command

Downloading the code and running npm run build gave me the following warnings:

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (879 KiB).
This can impact web performance.
Assets: 
  bundle.min.js (952 KiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (879 KiB). This can impact web performance.
Entrypoints:
  main (952 KiB)
      bundle.min.js


WARNING in webpack performance recommendations: 
You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.
For more info visit https://webpack.js.org/guides/code-splitting/

I did not make any changes to the code. I just want to bring this to your attention, to know if this can be ignored/improved.

Update Webpack from v3 to v4

Webpack 4 has been out for a while now, and has improved speed and bundle sizes..

Would you welcome a PR to upgrade?

Cannot build the project

Hi,

The project wont build up with npm run build, all I get is "TypeErrpr: merge is not a function". I tried to google some answers but did not find any solution.

Any idea why getting this error?

Consider to make this repository as template repository

Hi, as i know doing npm install phaser3-project-template in some empty directory is just making this repository to placed at node_modules/ directory, while i think this could be solved by making this repository to a template repository.

here's the reference https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-template-repository.

by doing this we could create a phaser3 project based this repository.

Thanks in advance. 👍

could you make this repo as a template repository?

I'd like to start a project from this template.
With a template repository I could juste create a new repo from this one (without forking).

It would keep a link to this template and all open project built from the template would be explicitly displayed on this repo.
It would build a galery of project ;)

ES 6 Support

Ahoi is there a plan to add ES6 support to this template?

adding scss compiler generates errors

When I attempt to add the following modules to the template at command line:

npm i -D style-loader css-loader sass-loader node-sass

and then add the following to the top of my dockit.js file (which is the same file I am coding my Phaser game in)

import "./styles/style.scss";

I get the following error:

ERROR in ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./node_modules/raw-loader!./src/styles/style.scss
Module build failed:

             ^
      Invalid CSS after "module.exports": expected selector, was '= "\\r\\n"'
      in c:\Users\...\mTraining\js\games\phaser3\projects\wptemplate\src\styles\style.scss (line 1, column 15)
 @ ./src/styles/style.scss 2:14-162
 @ ./src/dockit.js

My style.scss file is blank.

My module rule set looks like this:

  module: {
        rules: [
          {
            test: [ /\.s?css/, /\.vert$/, /\.frag$/ ],
            use: [
                "style-loader", //created style nodes from JS strings and HAS TO BE FIRST
                "css-loader", //translates CSS into CommonJS
                "sass-loader", //compiles Sass to CSS
                'raw-loader',
            ]
          }
        ]
    },

What am I doing wrong? My end goal in all of this is to simply allow scss compiling in addition to everything that is going on.

The logo.png file in src/assets shouldn't it be moved to public/assets?

The example code in this template shows a bouncing Phaser logo on a background image.

But the logo.png file in located in src/assets ? Shouldn't it be moved to public/assets where the background bg.jpg file is also located? Or is there a reason that I'm missing for it to be in a separate folder?

I mean I see there is a difference, after I run npm run build the logo becomes dist\18a7191678e558c5519c3e330eff5f85.png (?) but I guess I don't quite understand the advantage of this above just putting it in the public/assets folder.

Doesn't work on iOS (iPhone, iPad)

I'm workin on iMac and everything works fine on it. I tried to play on my android phone and everything worked fine too (I added --host 0.0.0.0 flag to the npm scripts' start command to access to the game from LAN).

Then I tried to play the game on my iPad but it didn't work (I just see background color but game doesn't start), also tried on simulators iPhone 11 X Pro and iPad 2 on my iMac the result is the same as on my real iPad. On my iPad I looked google chrome console logs (with chrome://inspect) and there weren't any errors...

What is the problem and how to fix it?

How to load MP3 FILE?

HOW TO LOAD MP3 File?

When I import MP3 file In JS File, and I did install url-loader in webpack/base.js and install url-loader --save, It happened a lot of errors;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.