Skip to content

Lualib modules using a plugin to generate lualib#1227

Merged
Perryvw merged 15 commits intomasterfrom
lualib-plugins
Mar 17, 2022
Merged

Lualib modules using a plugin to generate lualib#1227
Perryvw merged 15 commits intomasterfrom
lualib-plugins

Conversation

@Perryvw
Copy link
Copy Markdown
Member

@Perryvw Perryvw commented Mar 8, 2022

This PR contains two big changes:

Lualib files are now modules

  • Lualib sources now use import/export
  • Importing lualib functions now works the same as importing modules (in generated lua)
  • Inlining lualib files is unchanged, except the imported functionality is now local instead of global
  • The lualib dependency tree is no longer manually maintained, instead it is automatically generated during the lualib build.
    • The lualib dependency tree is distributed in our package as a json file.
  • Lualib output is generated with a tstl plugin to minimize lualib-generation-specific code in the main transpiler.

Plugins now provide beforeTransform and afterEmit hooks

To generate the required lualib files we used a plugin that required some extra hooks. Because of this we introduced two extra TSTL plugin functions:

beforeTansform

{
beforeTransform?: (
    program: ts.Program,
    options: CompilerOptions,
    emitHost: EmitHost
) => ts.Diagnostic[] | void;
}

This function on the plugin is called after gathering the TypeScript program and options, but before starting the transformation process. This can be used to set up stuff in the plugin, or to modify the program/options that the transpiler will act on.

afterPrint

{
afterPrint?: (
    program: ts.Program,
    options: CompilerOptions,
    emitHost: EmitHost,
    result: ProcessedFile[]
) => ts.Diagnostic[] | void;
}

This function on the plugin is run after the printing of Lua has completed. It provides the output array as one of its parameters, which can be used to modify the final output before finishing the emit.

@Perryvw Perryvw marked this pull request as ready for review March 10, 2022 21:52
@Perryvw Perryvw requested review from lolleko and tomblind March 10, 2022 21:52
@Zamiell
Copy link
Copy Markdown
Contributor

Zamiell commented Mar 10, 2022

Question: Is it possible to use the new afterPrint hook to put a comment at the beginning of the file? e.g. #1178

@Perryvw
Copy link
Copy Markdown
Member Author

Perryvw commented Mar 10, 2022

Question: Is it possible to use the new afterPrint hook to put a comment at the beginning of the file? e.g. #1178

Yes, that is exaclty what the added test for afterPrint does.

@Zamiell
Copy link
Copy Markdown
Contributor

Zamiell commented Mar 10, 2022

Woot! 🎉

@Perryvw
Copy link
Copy Markdown
Member Author

Perryvw commented Mar 17, 2022

Trying to re-trigger checks

@Perryvw Perryvw closed this Mar 17, 2022
@Perryvw Perryvw reopened this Mar 17, 2022
@Perryvw Perryvw merged commit fbb00d2 into master Mar 17, 2022
@Perryvw Perryvw deleted the lualib-plugins branch March 17, 2022 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants