Skip to content

Commit 5052022

Browse files
authored
Noresolve paths and json modules (#83)
* Add json modules page * Add noresolvepaths to configuration page * fix prettier
1 parent a8dc84e commit 5052022

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

docs/configuration.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ You can use our [VS Code extension](editor-support.md) or manually specify the J
2323
}
2424
```
2525

26-
| Option | Values | Description |
27-
| -------------------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
28-
| `luaTarget` | `"JIT"`, `"5.3"`, `"5.2"`, `"5.1"`, `"universal"` (default: `"universal"`) | Specifies the Lua version you want to generate code for. Choosing `universal` makes TypeScriptToLua generate code compatible with all supported Lua targets. |
29-
| `noImplicitSelf` | `true`, `false` (default: `false`) | If true, treats all project files as if they were prefixed with<br />`/** @noSelfInFile **/`. |
30-
| `noHeader` | `true`, `false` (default: `false`) | Set this to true if you don't want to include our header in the output. |
31-
| `luaLibImport` | `"inline"`, `"require"`, `"always"`, `"none"` (default: `"require"`) | We polyfill certain JavaScript features with Lua functions, this option specifies how these functions are imported into the Lua output. |
32-
| `sourceMapTraceback` | `true`, `false` (default: `false`) | Overrides Lua's `debug.traceback` to apply sourcemaps to Lua stacktraces. This will make error messages point to your original TypeScript code instead of the generated Lua. |
33-
| `luaBundle` | File path (relative to the `tsconfig.json`) | Will bundle all output lua files into a single bundle file. Requires **luaBundleEntry** to be set! |
34-
| `luaBundleEntry` | File path (relative to the `tsconfig.json`) | This should be the name/path of the TS file in your project that will serve as entry point to the bundled code. |
35-
| `luaPlugins` | `Array<{ name: string; import?: string }>` | List of [TypeScriptToLua plugins](api/plugins.md). |
36-
| `buildMode` | `"default"`, `"library"` (default: `"library"`) | Use `buildMode: "library"` to build [publishable library packages](publishing-modules.md). |
37-
| `tstlVerbose` | `true`, `false` (default: `false`) | Output additional logging when performing a tstl build, to help diagnose issues. |
26+
| Option | Values | Description |
27+
| -------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
28+
| `luaTarget` | `"JIT"`, `"5.3"`, `"5.2"`, `"5.1"`, `"universal"` (default: `"universal"`) | Specifies the Lua version you want to generate code for. Choosing `universal` makes TypeScriptToLua generate code compatible with all supported Lua targets. |
29+
| `noImplicitSelf` | `true`, `false` (default: `false`) | If true, treats all project files as if they were prefixed with<br />`/** @noSelfInFile **/`. |
30+
| `noHeader` | `true`, `false` (default: `false`) | Set this to true if you don't want to include our header in the output. |
31+
| `luaLibImport` | `"inline"`, `"require"`, `"always"`, `"none"` (default: `"require"`) | We polyfill certain JavaScript features with Lua functions, this option specifies how these functions are imported into the Lua output. |
32+
| `sourceMapTraceback` | `true`, `false` (default: `false`) | Overrides Lua's `debug.traceback` to apply sourcemaps to Lua stacktraces. This will make error messages point to your original TypeScript code instead of the generated Lua. |
33+
| `luaBundle` | File path (relative to the `tsconfig.json`) | Will bundle all output lua files into a single bundle file. Requires **luaBundleEntry** to be set! |
34+
| `luaBundleEntry` | File path (relative to the `tsconfig.json`) | This should be the name/path of the TS file in your project that will serve as entry point to the bundled code. |
35+
| `luaPlugins` | `Array<{ name: string; import?: string }>` | List of [TypeScriptToLua plugins](api/plugins.md). |
36+
| `buildMode` | `"default"`, `"library"` (default: `"library"`) | Use `buildMode: "library"` to build [publishable library packages](publishing-modules.md). |
37+
| `tstlVerbose` | `true`, `false` (default: `false`) | Output additional logging when performing a tstl build, to help diagnose issues. |
38+
| `noResolvePaths` | `Array<string>` | An array of require paths that will **NOT** be resolved. For example `["require1", "sub.require2"]` will stop tstl from trying to resolve Lua sources for `require("require1")` and `require("sub.require2")`. |
3839

3940
## Standard options
4041

docs/json-modules.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Importing JSON Modules
3+
---
4+
5+
TypeScriptToLua supports importing JSON files into your TypeScript. This is done by translating the JSON file to Lua and simply including this Lua file as part of the transpiler output.
6+
7+
# Example
8+
9+
Consider the following project files:
10+
11+
```
12+
./
13+
├── main.ts
14+
├── myjsondata.json
15+
└── tsconfig.json
16+
```
17+
18+
## Modifying tsconfig.json
19+
20+
To be able to import JSON files you have to enable the `resolveJsonModule` option in tsconfig.json, and set `moduleResolution` to node:
21+
22+
```json title=tsconfig.json
23+
{
24+
"compilerOptions": {
25+
"moduleResolution": "node",
26+
"resolveJsonModule": true
27+
}
28+
}
29+
```
30+
31+
## Json data
32+
33+
The file containing JSON is just standard JSON:
34+
35+
```json title=myjsondata.json
36+
{
37+
"property1": "foo",
38+
"property2": "bar"
39+
}
40+
```
41+
42+
## Importing JSON data
43+
44+
Now to access your data, simply import it from your TS file:
45+
46+
```ts title=main.ts
47+
import * as myJson from "./myjsondata.json";
48+
49+
const p1 = myJson.property1;
50+
```

sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"advanced/writing-declarations",
1111
"advanced/compiler-annotations",
1212
"advanced/language-extensions",
13+
"json-modules",
1314
"jsx",
1415
{
1516
"type": "category",

0 commit comments

Comments
 (0)