0.40.0, tstl supports module resolution for libraries, which means you can use and create npm packages containing
.lua files. You can also include Lua source files directly into your source code.
- You cannot import
- You must use
- It is recommended you use
- You cannot use
"luaBundle"in packages intended to be included as dependency in another project
tsconfig.json file must at least specify the following...
package.json file should specify the
types property. You should also specify the
main property if your module contains runnable Lua code.
These must be relative paths within your module without the file's extension.
These are set to
"index"by default so if you really don't want to specify these you can keep an
index.luafile at the top level of your package.
package.json you can specify the
files field to mark what files to publish.
This is useful if you don't want to publish your source code.
You can use
npm publish --dry-run to see what files would be published without publishing your package.
- Some files will always be published e.g.
- Modules specified in
"devDependencies"will not be available to the module at runtime.
tsconfig.jsonfile does nothing for users of your module.
And when you're happy, your
package.json has a
description, and you are logged into NPM on your machine... you can run
npm publish to publish your module.
Assuming the module is available on NPM, users of your module can download it like so.
Now they can start using it.
TypeScriptToLua will handle the module resolution from here.
For example projects using external Lua, you can look at the projects used in the TypeScriptToLua tests:
A project using dependencies from its node_modules directory with Lua code. These example dependencies include:
lua-global-with-decls: Lua code + TypeScript declarations defining global functions.
lua-global-without-decls: Lua code defining global functions.
- Declaration file is added manually in lua-global-without-decls.d.ts in the project sources.
lua-module-with-decls: Lua code + TypeScript declarations for 'module' files, i.e Lua files that return a table of exported functions.
lua-module-with-decls: Lua code for 'module' files, i.e Lua files that return a table of exported functions.
- Declaration files are added manually in lua-module-without-decls.d.ts in the project sources.
This project includes Lua files as part of the project's source files. To use the Lua from the files you have to provide declaration files with a matching name and location for each file. For examples
some_dir/library.d.ts. The declaration files contain the TypeScript declarations of the corresponding Lua file. Both Lua and .d.ts files should be checked into your repository!
This project contains two Lua source files:
luafile.lua: Some Lua right next to the .ts files using it.
lua_sources/otherluaFile.lua: Lua in a separate
lua_sourcesdirectory, in case you want to group all your Lua files into one directory.