Skip to content

Commit fa1acc8

Browse files
committed
book: start transpiling chapter, add TS ex
1 parent b9ea8eb commit fa1acc8

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Packing Sprites](./recipes/packing-sprites.md)
2323
- [Usagi Versus X](./recipes/usagi-versus-x.md)
2424
- [User Install vs Project Executable](./recipes/user-install-vs-project-executable.md)
25+
- [Transpiling to Lua](./recipes/transpiling-to-lua.md)
2526
- [Coming from Pico-8](./recipes/coming-from-pico-8.md)
2627
- [Porting to Love2D](./recipes/porting-to-love2d.md)
2728

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Transpiling to Lua
2+
3+
There are a whole host of languages out there that compile to Lua, allowing you
4+
to write your Usagi games in something other than vanilla Lua 5.5. Those
5+
languages might have other features or a style or syntax that you prefer. This
6+
recipe breaks down and shows some examples of how to go about this.
7+
8+
Transpiling works by taking the source language and spitting out a `main.lua`
9+
file that `usagi` then uses. What's nice about this is that you can read through
10+
the Lua code that gets generated to help see what's really happening under the
11+
hood and debug potential issues.
12+
13+
## MoonScript
14+
15+
TODO: https://moonscript.org/
16+
17+
## Teal
18+
19+
TODO: https://teal-language.org/
20+
21+
## TypeScript
22+
23+
Using the [TypeScriptToLua](https://typescripttolua.github.io/) library, you can
24+
write your Usagi games in TypeScript, which is a typed version of JavaScript
25+
that's widely used in web development. You can use it with the `npm` package
26+
manager, possibly even with Bun or Deno too (but that hasn't been tested).
27+
28+
Here's an example of a TypeScript Usagi game:
29+
[https://codeberg.org/brettchalupa/usagi/src/branch/main/examples/transpile/ts_to_lua](https://codeberg.org/brettchalupa/usagi/src/branch/main/examples/transpile/ts_to_lua)
30+
31+
**Note:** the typedefs are currently very minimal and incomplete. Someone
32+
maintaining them as an external package would be a good community project. Or
33+
just write them by hand as you need them.
34+
35+
Usagi relies a lot upon global functions, tables like `gfx` and `input`, and
36+
does not use objects or `self`. So when writing the TS, you'll need to set
37+
special parameters or add comments for the TypeScript compiler. Useful
38+
references:
39+
40+
- [https://typescripttolua.github.io/docs/the-self-parameter](https://typescripttolua.github.io/docs/the-self-parameter)
41+
- [https://typescripttolua.github.io/docs/assigning-global-variables](https://typescripttolua.github.io/docs/assigning-global-variables)
42+
43+
## YueScript
44+
45+
TODO: https://yuescript.org/
46+
47+
## Browse the Examples
48+
49+
The Usagi source code repository has a folder of transpiled examples:
50+
[https://codeberg.org/brettchalupa/usagi/src/branch/main/examples/transpile](https://codeberg.org/brettchalupa/usagi/src/branch/main/examples/transpile)

0 commit comments

Comments
 (0)