phaser官方提供了一个使用了webpack模版的项目,github地址,该项目已经使用了babel和webpack,在此基础上,咱们加入typescript。node
安装typescript和ts-loader:webpack
npm install typescript --save-dev npm install ts-loader --save-dev
在webpack/base.js的配置中增长.ts文件的loader:git
{ test: /\.ts$/, exclude: /node_modules/, use: [ { loader: 'babel-loader' }, { loader: 'ts-loader' }, ] },
根目录下添加tsconfig.json文件,一个参考配置以下github
{ "compilerOptions": { "target": "ES2016", "module": "CommonJS", "sourceMap": true, "noImplicitAny": false, "strict": false }, "include": [ "src/*" ] }
在根目录下新建index.d.ts,web
declare module "*.png" { const content: string; export default content; }
而后修改tsconfig.json文件typescript
{ "compilerOptions": { "target": "ES2016", "module": "CommonJS", "sourceMap": true, "noImplicitAny": false, "strict": false }, "include": [ "src/*", "index.d.ts", ] }
便可按找以下方式加载图片:npm
import * as logo from '../assets/down.png';
一个加入了typescript的phaser模版的github地址json