Skip to content

Commit 724969b

Browse files
committed
Update low-level API documentation
1 parent 4e2e2b4 commit 724969b

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

docs/api/overview.md

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,78 @@ console.log(result.transpiledFiles);
9292

9393
On the contrast with high-level API, low-level API requires you to to manage TypeScript project yourself. See [Using the Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) page for the introduction to TypeScript API.
9494

95-
### Transpile
95+
### Transpiler class
9696

97-
**Arguments:**
97+
The `Transpiler` class creates an instance of the tstl transpiler that is used to implement the high-level API. The transpiler allows passing a raw tstl program object and providing different transpilation parameters, such as the EmitHost object to be used to read/write files.
98+
99+
The main usage of the Transpiler is as follows:
100+
101+
```ts
102+
import * as tstl from "typescript-to-lua";
103+
104+
const { emitSkipped, diagnostics } = new tstl.Transpiler().emit(emitOptions);
105+
106+
// Provide a custom emitHost for custom reading/writing of files:
107+
const { emitSkipped, diagnostics } = new tstl.Transpiler(customEmitHost).emit(emitOptions);
108+
```
109+
110+
**Emit options:**
98111

99-
- program: ts.Program - The TypeScript program to transpile (note: unlike the high-level API, compilerOptions is part of the program and cannot be supplied separately).
112+
The Transpiler class `emit` accepts one argument which is an options object you can use to customize the behavior of the transpiler. The options in this object are:
113+
114+
- program: ts.Program - The TypeScript program to transpile (note: unlike the high-level API, `compilerOptions`` is part of the program and cannot be supplied separately).
100115
- _[Optional]_ sourceFiles: ts.SourceFile[] - A collection of `SourceFile`s to transpile, `program.getSourceFiles()` by default.
101116
- _[Optional]_ customTransformers: ts.CustomTransformers - List of extra [TypeScript transformers](../configuration.md#transformers).
102117
- _[Optional]_ plugins: tstl.Plugin[] - List of [TypeScriptToLua plugins](plugins.md).
103-
- _[Optional]_ emitHost: tstl.EmitHost - Provides the methods for reading/writing files, useful in cases where you need something other than regular reading from disk. Defaults to `ts.sys`.
118+
- _[Optional]_ writeFile: ts.WriteFileCallback - Provides a callback to use to emit the final file result, instead of trying to write them to disk.
104119

105120
**Example:**
106121

107122
```ts
108-
const reportDiagnostic = tstl.createDiagnosticReporter(true);
123+
import * as ts from "typescript";
124+
import * as tstl from "typescript-to-lua";
125+
126+
// Parse existing tsconfig.json
109127
const configFileName = path.resolve(__dirname, "tsconfig.json");
110128
const parsedCommandLine = tstl.parseConfigFileWithSystem(configFileName);
111129
if (parsedCommandLine.errors.length > 0) {
112130
parsedCommandLine.errors.forEach(reportDiagnostic);
113131
return;
114132
}
115133

134+
// Set tstl-specific configuration options
135+
parsedCommandLine.options.luaTarget = tstl.LuaTarget.Lua54;
136+
137+
// Create a TS program to feed to the transpiler
116138
const program = ts.createProgram(parsedCommandLine.fileNames, parsedCommandLine.options);
117-
const { transpiledFiles, diagnostics: transpileDiagnostics } = tstl.transpile({ program });
118139

119-
const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
120-
emitResult.forEach(({ name, text }) => ts.sys.writeFile(name, text));
140+
// Create in-memory source file
141+
const extraSourceFile = ts.createSourceFile("my-source-file.ts", "// my ts code");
142+
143+
const emitResults: Record<string, string> = {};
121144

145+
// Call the Typescript-to-Lua transpiler with emit options
146+
const { diagnostics } = new tstl.Transpiler().transpile({
147+
program, // Provide the program to transpile, including compiler options!
148+
sourceFiles: [extraSourceFile],
149+
writeFile(fileName, data) {
150+
emitResults[fileName] = data; // Instead of writing to file, emit files to this object in memory
151+
},
152+
plugins: [
153+
// We can even provide plugins directly
154+
{
155+
beforeTransform(program, CompilerOptions, emitHost) {
156+
console.log("before transforming plugin hook!");
157+
},
158+
afterPrint(program, options, emitHost, result) {
159+
console.log("after printing plugin hook!");
160+
},
161+
},
162+
],
163+
});
164+
165+
// Use TS to report all diagnostics to console
166+
const reportDiagnostic = tstl.createDiagnosticReporter(true);
122167
const diagnostics = ts.sortAndDeduplicateDiagnostics([...ts.getPreEmitDiagnostics(program), ...transpileDiagnostics]);
123168
diagnostics.forEach(reportDiagnostic);
124169
```

0 commit comments

Comments
 (0)