Jump to content

Setting up Phaser in VSCode + TypeScript, how to import it correctly?


Horscht
 Share

Recommended Posts

I'm currently trying to get my first Phaser project working, fiddled around a whole day trying to get it all set up but since I'm completely new to writing bigger JS projects with modules, I have no idea how any of this works, even after googling and reading a million guides.

I want to write TypeScript in VSCode. So first I installed phaser with 

npm install phaser --save

Then I import Phaser in my app.ts with

import Phaser = require("phaser-ce");
Did I pick the right syntax out of the 50 different ones? Whatever, this is the one I chose. So, while im coding I now got code completion and type hints working, but after compiling to JS, I now have multiple problems:
 
First of all, the compiler adds code to my main JS file which tries to access a global module or exports object which obviously doesn't exist. Why? Why does it assume this exists? How can I turn this off? No idea.
This is what it pastes righto onto line 2 of the main JS file:
Object.defineProperty(exports, "__esModule", { value: true });

And since "exports" doesn't exist unless I hack it into the HTML file, it gives me an error. But now the more important question is... how exactly do I import Phaser? As far as I know you can't really import other JS files into another? (At least not in ES5 as far as I know).

(Great, after switching my text color to an illegible light gray, forcing me to manually recolor all the text to black, now the text editor here creates a new paragraph instead of a new line everytime I press enter. Whyyyyyyy...........)

In order to get code hints I need to import Phaser in my TypeScript file with the import statement, but if I use anything other than "module": "commonjs" in my tsconfig.json It can't find phaser anymore: 

// tsconfig.json
// ...
    "compilerOptions": {
        "module": "commonjs",
// ...

// game.ts
import Phaser = require("phaser-ce");
// WORKS


// tsconfig.json
// ...
    "compilerOptions": {
        "module": "anything other than commonjs",
// ...

// game.ts
import * as Phaser from "phaser-ce"; // Can't use same syntax. Why? I don't know.
// TypeScriptCompiler error:
// Cannot find module 'phaser-ce'

But of course commonjs doesn't work in the browser so... I tried instead to load the typings manually by copying the into /node_modules/@types/phaser and then instead of importing Phaser, instead referencing the typings with:

/// <reference types="phaser"/>

And including phaser.js in my index.htm

But since Phaser itself has a bunch of dependencies that it tries to load with require() which of course doesn't exist and even if it did couldn't work, how am I supposed to do this correctly?

It just whines about Pixi not being defined and some other stuff.

I read http://phaser.io/tutorials/how-to-use-phaser-with-typescript but it never mentions how Pixi etc is supposed to be included and doesn't say anything about require, export, module errors, it just works apparently, but not for me.

Which of the 50 different module systems does Phaser use, how does it export itself? RequireJS? AMD? UMD? ES6? ES2015? I have no idea how many different methods there are and how to tell which one is being used.

How do I import it correctly? Do I need to manually add ALL *.js files of phaser into the <head> of my index.htm?

This is all waaaaay too confusing....., help please :(

 

Edit:

I got a basic version working for now by just scrapping all the import stuff, setting tsconfig.json "module" to "none", adding a ///<reference types="phaser-ce"/> at the top of my game.ts and just including phaser.js in the html file. How it gets it's dependencies I still don't know but at least it works now. I also have no Idea where it gets the typings from, I guess it must be magic.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...