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