Skip to content

Commit b0076d9

Browse files
committed
Document moduleResolution plugin
1 parent 45a0a5e commit b0076d9

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

docs/api/plugins.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ const plugin: tstl.Plugin = {
144144
emitHost: tstl.EmitHost,
145145
result: tstl.ProcessedFile[],
146146
) {
147+
// Add a comment to the start of all created Lua files
147148
for (const file of result) {
148149
file.code = "-- Comment added by afterPrint plugin\n" + file.code;
149150
}
@@ -163,10 +164,7 @@ import * as tstl from "typescript-to-lua";
163164

164165
const plugin: tstl.Plugin = {
165166
beforeEmit(program: ts.Program, options: tstl.CompilerOptions, emitHost: tstl.EmitHost, result: tstl.EmitFile[]) {
166-
void program;
167-
void options;
168-
void emitHost;
169-
167+
// Add a comment to the start of all output files
170168
for (const file of result) {
171169
file.code = "-- Comment added by beforeEmit plugin\n" + file.code;
172170
}
@@ -175,3 +173,29 @@ const plugin: tstl.Plugin = {
175173

176174
export default plugin;
177175
```
176+
177+
### `moduleResolution`
178+
179+
You can use the `moduleResolution` function to modify how the tstl module resolution works. It provides you with the require path, the file requiring that module, the tsconfig options used to compile the project, and the tstl EmitHost.
180+
181+
You can use this to intercept the regular module resolution and provide your own. If this function returns `undefined`, tstl will fall back on its own module resolution and try to resolve the file as usual.
182+
183+
```ts
184+
import * as tstl from "typescript-to-lua";
185+
186+
const plugin: tstl.Plugin = {
187+
moduleResolution(
188+
moduleIdentifier: string,
189+
requiringFile: string,
190+
options: tstl.CompilerOptions,
191+
emitHost: tstl.EmitHost,
192+
) {
193+
// If "foo" is required, resolve it as 'bar.lua'
194+
if (moduleIdentifier === "foo") {
195+
return "bar";
196+
}
197+
},
198+
};
199+
200+
export default plugin;
201+
```

0 commit comments

Comments
 (0)