phaser: Phaser doesn't work with Webpack and require
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
For repro you can use the Webpack boilerplate found here https://github.com/photonstorm/phaser/pull/1924
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 39 (3 by maintainers)
Commits related to this issue
- switch to using phaser from npm per comment: https://github.com/photonstorm/phaser/issues/1974#issuecomment-186923141 — committed to Flet/phaser-es6-webpack by Flet 8 years ago
- Implemented advice from https://github.com/photonstorm/phaser/issues/1974 on including phaser with webpack. — committed to insert-username/lights-out-dont-bite by insert-username 8 years ago
- switch to using phaser from npm per comment: https://github.com/photonstorm/phaser/issues/1974#issuecomment-186923141 — committed to juliendasilva/Fun-Lympic by Flet 8 years ago
Taking @dlindenkreuz’s approach a different direction—you can also expose the files from
node_modulesin Phaser 2.4.5+ so you don’t have to make a custom build and still get minification. Unfortunately all the files have to be global due to a circular reference (Phaser file pullsPIXIglobal var, andpixi.jsreferencesPhaserfrom a function). The expose-loader works well here (I could not get the ProvidePlugin to work).Then in your entry file:
This is similar to making it an webpack external, but at least webpack can bundle it together and minify it now.
Yes, the following should just work:
npm install --save phaserfollowed byimport Phaser from "phaser". I’ve used many other libs this way, and anything else isn’t good enough.None of these are solutions, just leaking bandages. Phaser must evolve accordingly to maintain relevance in an ES6 world. If jQuery can do it, so can Phaser.
@ElegantSudo the issue here (in this thread) isn’t to do with not being ES6, it’s to do with Phaser being one giant global single var, with no module capability at all, with dependencies to other global based libs. Which makes it a pain to ingest in a module based workflow. Which is why Phaser 3 is fully modular, and itself built with webpack2. The code is still ES5, the module format CJS, but it should make threads like this a thing of the past. ES6 will follow, but first the module based version needs completing. One leads to the other.
@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
PIXIvariable to phaser. files inlib/are built using @photonstorm’s method described above https://github.com/photonstorm/phaser/issues/1974#issuecomment-134222165. no probs with minification during build.no need to require pixi in my code files: