TypeScriptToLua 使用typescript 开发lua 功能试用

前边有大概简单介绍TypeScriptToLua 以及一些玩法,以下是一个简单的试用,以及一些说明

测试的功能

会测试关于lua 模块集成(纯lua 脚本以及包含c 调用的),以及class 处理

代码说明

  • 代码结构
├── README.md
├── app.sh
├── package.json
├── src
├── dalongrong.ts
├── first.ts
├── json.d.ts
├── json.lua
├── main.ts
├── myjson.ts
├── things.d.ts
└── things.lua
├── tsconfig.json
└── yarn.lock
  • 代码说明
    package.json
 
{
  "name": "first",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "@types/shortid": "^0.0.29",
    "typescript": "^4.6.3",
    "typescript-to-lua": "^1.4.3"
  },
  "scripts": {
    "build": "tstl",
    "dev": "tstl --watch"
  },
  "dependencies": {}
}

tsconfig.json TypeScriptToLua 使用了与ts 兼容的定义,但是也自定义了一些配置

{
 "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/vscode-typescript-to-lua/master/tsconfig-schema.json",
  "include": [
    "src/*"
   ],
  "compilerOptions": {
    "outDir": "./dist",
    "target":"ESNext",
    "lib": ["esnext","DOM"],
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "strict": true
  },
  "tstl": {
    "noHeader": true,  // 去掉头
    "noImplicitSelf":true, // 禁用self 
    "buildMode": "library",
    "luaTarget": "JIT",
    "noResolvePaths":["rapidjson"]  //  规避tstl 对于lua 模块引用的解析,不然会报错的,比如rapidjson 是引用的,我们不需要解析处理
  }
}

things.lua 纯lua 的模块定义

return {
    {
        foo = 123,
        bar = 456,
    },
    {
        foo = 789,
        bar = 987,
    },
}

things.d.ts 方便强类型的定义,就是普通的ts 定义处理

interface Thing {
  foo:number
  bar:number
}
 
declare const mythings: Thing[]
 
export =  mythings

json.lua 一个引用c lua 模块的集成,处理json的,具体安装可以使用luarocks install rapidjson

local rapidjson = require("rapidjson")
return {
        encode = rapidjson.encode,
        decode = rapidjson.decode
}

json.d.ts json.lua 模块的ts 定义,方便强类型处理(基于namespace,同时需要export ,暴露为一个模块,方便ts import)

declare namespace json {
    function decode(input: string): object;
    function encode(
       input: object,
       params?: {
           indent?: boolean,
           level?: number,
       }
   ): string;
 }
 export = json

dalongrong.ts 一个class 定义

class MyApp {
    static BASE_URL = "https://baidu.com"
    /** @customConstructor MyApp */
    constructor(public name:string,public age:number){
        this.age=age;
        this.name=name;
    }
    static mydemo(){
        return this.BASE_URL;
    }
    login():string{
        return `${this.age}-----${this.name}`
    }
}
export default MyApp
export {
    MyApp
}

main.ts 入口, 对于编写的lua 模块以及ts 定义集成使用

import * as things from "./things";
import * as json from "./json"
import {MyApp} from "./dalongrong"
let app = new MyApp("dalong",333)
console.log(app.login())
console.log(MyApp.mydemo())
console.log(things)
let result = json.encode(app)
console.log(result)

app.sh 处理lua 运行的path 定义

#!/usr/bin/sh
export LUA_PATH="$PWD/dist/?.lua;$PWD/node_modules/?.lua;$PWD/node_modules/@dalongrong/ts-lua-module-app/dist/?.lua;;"
lua dist/main.lua

运行&&效果

  • 构建
yarn build
  • 效果

如下图生成的lua 文件,标红部分是关于一些工具类的集成,比如class 的处理

 

 

 

  • 运行效果
 
sh app.sh

包含了class 以及json 序列化的处理

 

 

 

说明

使用TypeScriptToLua 开发lua 应用还是很不错的,后续看时间情况计划是想试用TypeScriptToLua 做为开发nginx 模块的一个桥,试用ts 的强类型能力解决openresty 模块开发的问题可以提供相关的npm 模块,加速openresty 的开发,moonscript 也是一个不错的选择,但是相比typescript 缺少了强类型的能力
同时我们基于TypeScriptToLua的强类型能力,可以简化下lapis 的开发

参考资料

https://typescripttolua.github.io/docs/configuration
https://github.com/TypeScriptToLua/TypeScriptToLua
https://github.com/rongfengliang/typescript-to-lua-learning
https://luarocks.org/modules/xpol/rapidjson
https://leafo.net/lapis/
https://github.com/leafo/moonscript
https://moonscript.org/

posted on 2022-04-17 22:59  荣锋亮  阅读(626)  评论(0编辑  收藏  举报

导航