Skip to content

Commit bf87953

Browse files
CheatoidPerryvw
andauthored
Add afterEmit plugin hook (#1353)
* Add `afterEmit` plugin hook This function would run after Lua files have been written to disk (in order to allow plugins to perform post-build tasks such as optimizing/minifying/obfuscating/bytecode-compilation/etc). * Update plugins.ts * Create afterEmit.ts * Add test for plugin (cherry picked from commit ab6099c) --------- Co-authored-by: Perry van Wesel <Perryvw@users.noreply.github.com>
1 parent e0e56fe commit bf87953

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/transpilation/plugins.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ export interface Plugin {
4646
result: EmitFile[]
4747
) => ts.Diagnostic[] | void;
4848

49+
/**
50+
* This function is called after translating the input program to Lua, after resolving dependencies, after bundling and writing files to disk.
51+
*/
52+
afterEmit?: (
53+
program: ts.Program,
54+
options: CompilerOptions,
55+
emitHost: EmitHost,
56+
result: EmitFile[]
57+
) => ts.Diagnostic[] | void;
58+
59+
/**
60+
* This function is called when trying to resolve the .lua file corresponding to a Lua require statement. Allows you to provide
61+
* your own module resolution logic. If return value is undefined, regular module resolution is done.
62+
*/
4963
moduleResolution?: (
5064
moduleIdentifier: string,
5165
requiringFile: string,

src/transpilation/transpiler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ export class Transpiler {
9090
}
9191
}
9292

93+
for (const plugin of plugins) {
94+
if (plugin.afterEmit) {
95+
const afterEmitPluginDiagnostics = plugin.afterEmit(program, options, this.emitHost, emitPlan) ?? [];
96+
diagnostics.push(...afterEmitPluginDiagnostics);
97+
}
98+
}
99+
93100
if (options.tstlVerbose) {
94101
console.log("Emit finished!");
95102
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as ts from "typescript";
2+
import * as tstl from "../../../src";
3+
4+
const plugin: tstl.Plugin = {
5+
afterEmit(program: ts.Program, options: tstl.CompilerOptions, emitHost: tstl.EmitHost, result: tstl.EmitFile[]) {
6+
void program;
7+
void options;
8+
void emitHost;
9+
void result;
10+
11+
const diagnostic = {
12+
category: ts.DiagnosticCategory.Message,
13+
messageText: "After emit diagnostic message!",
14+
code: 1234,
15+
file: program.getSourceFiles()[0],
16+
start: undefined,
17+
length: undefined,
18+
} satisfies ts.Diagnostic;
19+
return [diagnostic];
20+
},
21+
};
22+
23+
// eslint-disable-next-line import/no-default-export
24+
export default plugin;

test/transpile/plugins/plugins.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
22
import * as util from "../../util";
3+
import * as ts from "typescript";
34

45
test("printer", () => {
56
util.testModule``
@@ -160,3 +161,24 @@ test("beforeEmit plugin bundle", () => {
160161
expect(f.lua).toContain("-- Comment added by beforeEmit plugin");
161162
}
162163
});
164+
165+
test("afterEmit plugin", () => {
166+
const { diagnostics } = util.testModule`
167+
console.log("Hello, World!");
168+
[].push(1,2,3); // Use lualib code
169+
`
170+
.addExtraFile(
171+
"extrafile.ts",
172+
`
173+
console.log("Hello, Mars!");
174+
`
175+
)
176+
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "afterEmit.ts") }] })
177+
.getLuaResult();
178+
179+
// Expect to see the diagnostic returned by the plugin in the output
180+
const diagnostic = diagnostics.find(d => d.code === 1234);
181+
expect(diagnostic).toBeDefined();
182+
expect(diagnostic?.category).toBe(ts.DiagnosticCategory.Message);
183+
expect(diagnostic?.messageText).toContain("After emit");
184+
});

0 commit comments

Comments
 (0)