-
-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customize Lua require path #1371
Comments
Have you thought about writing a small transpiler plugin? (https://typescripttolua.github.io/docs/api/plugins) |
Writing a plugin looks like a great option - easier to share and less brittle than a shell script. Thanks! If plugins seem like the right solution to this kind of problem, feel free to close this issue. |
I played around with this a bit and was able to achieve what I wanted quite easily with a simple plugin (copy-pasted below for reference in case anyone else has the same question): import ts from "typescript";
import * as tstl from "typescript-to-lua";
const REQUIRE_PATH_REGEX = /require\("(.+)"\)/g;
// require paths that we don't want to transform
// in my case, this is another Lua plugin that my plugin depends on
const RAW_IMPORT_PATHS = ["lspconfig"];
const plugin: tstl.Plugin = {
beforeEmit(
_program: ts.Program,
_options: tstl.CompilerOptions,
_emitHost: tstl.EmitHost,
result: tstl.EmitFile[]
) {
for (const file of result) {
file.code = file.code.replaceAll(
REQUIRE_PATH_REGEX,
(match, path: unknown) => {
if (typeof path !== "string" || RAW_IMPORT_PATHS.includes(path)) {
return match;
}
return `require("typescript.${path}")`;
}
);
}
},
};
export default plugin; Thanks for the guidance @lolleko! From my end this is an ideal solution so I'm going to close this issue. |
Hello! I'm using TypeScriptToLua to write typescript.nvim, a Neovim plugin. First, thanks for the awesome project! I maintain another, much larger Lua-only plugin, and I much prefer working in TypeScript. I also apologize if my description is lacking in detail or otherwise confusing.
The issue is as follows: If a Neovim user loads my plugin by calling
require("typescript")
in their config, the editor searches its runtime path (generally plugin paths + config paths) for one of the following:lua/typescript.lua
filelua/typescript/init.lua
entrypointAt the moment, I'm using the following option to compile my code into a single
init.lua
. When users install my plugin, either manually or via a plugin manager, it's placed on Neovim's runtime path, which makes the aboverequire
call work as expected:Ideally, instead of using a bundle, I'd like to use
outDir
to mirror the structure of the source TypeScript files. This would have a few advantages:However, this doesn't currently work from Neovim. Let's say I remove the
luaBundle
options above in favor ofoutDir
and start with the following TypeScript file:The resulting
require
call at the top of the transpiled file then (naturally) looks like this:Unfortunately, this won't work, because when the above code is run in Neovim, the editor looks for a
lua/config.lua
orlua/config/init.lua
in its runtime path (which may exist somewhere but won't match what we actually want).The ideal solution on my end would be to modify the
require
path so that the above call points attypescript.config
instead of justconfig
. I played around with all thetsconfig.json
options I could find and wasn't able to achieve that result, so I'm curious if there's a way to achieve this that I'm not aware of. (This may also be an XY problem that I've banged my head against for too long.)Otherwise, I'm not sure whether it makes sense to add this as a feature, since I don't know if other use cases exist, but it would definitely be a boon for the Neovim community. (I've also considered just writing a script to modify these paths manually, but that's more brittle and raises the threshold for plugin authors to start using TSTL.)
The text was updated successfully, but these errors were encountered: