phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Phaser doesn't work with Webpack and require

Open mkristo opened this issue 11 years ago • 39 comments

Earlier I made an (ugly) pull request https://github.com/photonstorm/phaser/pull/1923 that made Phaser work with Webpack, but since 7a6de818e1c8742f0e4e3eb0ffb3dc80a280a0e5 I'm sorry to say that this doesn't work anymore.

I'm getting

Uncaught ReferenceError: PIXI is not defined

from https://github.com/photonstorm/phaser/blob/6139071ebc918ddd8a1201279ebc7c70e645de8e/src/Phaser.js#L357

For repro you can use the Webpack boilerplate found here https://github.com/photonstorm/phaser/pull/1924

mkristo avatar Aug 03 '15 14:08 mkristo

Yes the way it was previously bundled was completely wrong - PIXI should never have been inside the Phaser UMD, so it's now properly split out. The grunt custom build task has a new flag --split true which will generate Phaser, Pixi, P2 and Creature to their own stand-alone build files, each with the wrappers they need. So you can now bundle them together however you want. As long as they're allowed to exist globally they will be much happier. PIXI needs to come before Phaser in any requirement chain.

photonstorm avatar Aug 03 '15 15:08 photonstorm

I understand now. Yes, this makes much more sense.

But when installing Phaser 2.4.2 via npm it seems like PIXI is still bundled with (at least) the no-physics build. I think it would be desirable to provide the split up modules as well.

Also, we should probably update the Webpack boilerplate to require both PIXI and Phaser. I'd be happy to do this, but the PIXI module needs to be available before this is done.

mkristo avatar Aug 03 '15 15:08 mkristo

FYI, I can currently get Phaser to build by doing this:

global.PIXI = require('pixi.js');
global.p2 = require('p2');
global.Phaser = require('phaser');

In this case, PIXI is set to version

"pixi.js": "2.2.x",

I will add, though that this no longer works in 2.4.3. because the CanvasPool mixin is not defined on PIXI. If you require the CanvasPool file in between PIXI and Phaser, of course, it mixies in correctly, and works.

I'm aware that this involves setting some globals, which isn't ideal- but ti does allow you to use npm to serve all the modules.

I'm going to update my slush generator with these changes as soon as 2.4.3 is on master

the-simian avatar Aug 24 '15 04:08 the-simian

Just to say that I've got a guide to setting up Phaser with Webpack which I'll publish shortly.

I'd just like to add one thing though: don't ever use any version of Pixi that you find in npm with Phaser, as it won't work. You have to use the version we've built specifically or so much will break.

p2 on the other hand should be safe to pull in from npm instead of the build included with Phaser as long as you use the exact same version that Phaser bundles with it, any deviation from that is highly likely to break it.

photonstorm avatar Aug 24 '15 08:08 photonstorm

I noticed that the mixins you were using with PIXI were located in the utils directory. My question is, could we still use the npm version, as long as we mix in all the addons (like the CanvasPool). Are you actually changing the source code itself, currently - or are you modifying PIXI entirely by adding new properties?

If you're doing the latter, it could be possible to still use the npm PIXI and then just require a file you provide in the source that then mixes in all your modifications to PIXI. Of course, the same rules apply - you have to match the right version. I know PIXI is well into v3, but you are (last I checked) locking pixi on v 2.2.8. This could be useful because that way you'd really only have to maintain the parts that deviate from pixi.js source. So to be clear, I am imagining that you could require pixi with npm, and then require a file that basically requires all the addons in the right order, and just plug all the functionality you need right in. You would have the latitude to update or replace existing methods if you needed to. If I knew more specifically the parts that deviate from PIXI I would be happy submit a PR that basically works this way.

If I'm way off, and this wouldn't work in your opinion, I'd like to know more. Any amount you can elaborate on what's different about this PIXI from the core pixi (of the same version) I'd love to know.

In the meantime, is the most correct file to include from the Phaser source for PIXI currently

src/pixi/Pixi.js

right? You'd still need the CanvasPool and stuff like that afterwards, I would assume.

Thank you for taking time to respond to all these issues, I really appreciate the work you're putting in on Phaser!

the-simian avatar Aug 24 '15 11:08 the-simian

The PIXI source in the Phaser repo is vastly different to the last published version of Pixi 2. We've added and fixed so much that isn't in the official repo it's not even close any more.

photonstorm avatar Aug 24 '15 14:08 photonstorm

Here's how to use Phaser with Webpack:

  1. You have to use a custom build of Phaser. You cannot use one of the builds from the dist folder. Here is the grunt custom task I used:

grunt custom --exclude p2,creature,ninja --split true

This will create phaser.js and pixi.js files in the dist folder. Copy those over to your project folder.

  1. In Webpack you need to install the script-loader:

npm install script-loader --save-dev

  1. In your webpack.config.js flag Phaser and Pixi to use the script loader. There are various ways of doing this, but I used this approach:
module.exports = {

    entry: "./src/entry.js",

    output: {
        path: __dirname,
        filename: "bundle.js"
    },

    module: {
        loaders: [
            { test: /pixi.js/, loader: "script" },
            { test: /phaser.js/, loader: "script" }
        ]
    }

};
  1. In my entry.js I just have this:
require('../lib/pixi.js');
require('../lib/phaser.js');
require('./main.js');

main.js contains my test game, which is just a basic Phaser example.

  1. Then build it.

Using the split Phaser / PIXI libs and the Webpack script loader I was able to bundle my game up quite happily without any changes to the core Phaser source.

photonstorm avatar Aug 24 '15 14:08 photonstorm

Thank you for responding so quickly!

I will try this out as soon as possible

the-simian avatar Aug 24 '15 14:08 the-simian

I'm getting the error Phaser.game is not a function,

lslima91 avatar Aug 31 '15 16:08 lslima91

Check you followed the instructions above exactly as written. If it still doesn't work post your project structure and conf file up.

photonstorm avatar Aug 31 '15 16:08 photonstorm

Ok I got it now it works after cleaning my project folder. I didn't bother to check what script loader does..

lslima91 avatar Aug 31 '15 16:08 lslima91

I'm happy with the solution provided above.

@photonstorm: Should I close the issue, or do you want it to be open?

mkristo avatar Sep 02 '15 13:09 mkristo

I'll leave it for now until I've moved the above instructions somewhere more public.

photonstorm avatar Sep 02 '15 14:09 photonstorm

I had the same problem days ago. With Webpack + Bower. To be able to use commonJS-style i did this simple config

var webpackConfig = {
    // ...
    externals: {
        "phaser": "Phaser"
    }
    // ...
};

and then

var phaser = require('phaser');

In my opnion. The bower should keep the main like that, but still export all submodules sources. To easly make custom builds from bower overrides.

Or like lodash, exporting allsubmodules in npm. E.g. : https://www.npmjs.com/package/lodash.shuffle

Currently, the only way to do custom build is from grunt

webcaetano avatar Oct 09 '15 13:10 webcaetano

could we get a more full set of instructions for this in a public place?

seand88 avatar Dec 04 '15 22:12 seand88

I try to write a shim module for ES6 and webpack.. https://github.com/amowu/phaser-shim

amowu avatar Dec 06 '15 19:12 amowu

Thanks, @amowu! It works!

kinday avatar Dec 06 '15 20:12 kinday

@amowu nice. You guys also may like my phaserjs boilerplate. It's ES6+Gulp+Webpack

Phaser also are called by require(); And stats.js too.

webcaetano avatar Dec 06 '15 22:12 webcaetano

@webcaetano 👍

amowu avatar Dec 07 '15 06:12 amowu

@photonstorm using script-loader prevents minification because the module is loaded as a string and then evaluated (see https://github.com/webpack/script-loader/issues/1).

i used following webpack config to load pixi with imports-loader, then directly provided as PIXI variable to phaser. files in lib/are built using @photonstorm's method described above https://github.com/photonstorm/phaser/issues/1974#issuecomment-134222165. no probs with minification during build.

{
  resolve: {
    alias: {
      "pixi": path.join(__dirname, "lib/pixi.js"),
      "phaser": path.join(__dirname, "lib/phaser.js")
    }
  },
  module: {
    loaders: [
      {test: /phaser\.js$/, include: path.join(__dirname, 'lib'), loader: 'imports?PIXI=pixi'},
    ]
  }
}

no need to require pixi in my code files:

var Phaser = require("phaser");
// ES6
import Phaser from "phaser"

dlindenkreuz avatar Jan 04 '16 18:01 dlindenkreuz