Skip to content

Creating custom libraries #270

@ark120202

Description

@ark120202

One of the best features of JS/TS ecosystem is the ability to create and reuse code with npm packages.
Currently it's impossible to create consumable libraries for Lua.
While working on my transpiler I have investigated it, so that's what I came up with:

Variant 1 - Transpilation done by application developer

  • Enforces some strict project structure for library authors
  • Requires reimpementing some TypeScript's project organization features, like project references
  • Makes transpiler API less flexible, since new TS programs would be created for each library internally
  • Would make transpilation a lot slower, unless some caching would be added

Variant 2 - Transpilation done by library developer

  • TypeScript and Babel are doing this
  • Allows to create projects that compile to both Lua and JS
  • Allows library author to use different/patched version of tstl and use custom plugins
  • Allows library author to customize build phase as they want (cli, gulp, custom scripts)
  • Allows to use libraries written manually with Lua and .d.ts files
  • Requires development of standard library as a separate package (maybe in a monorepo), so multiple libraries would use only one instance and version conflicts would be avoided with semver. TS is doing the same thing with tslib.
  • Complicates import path resolving process (but also makes it more flexible)
  • Not allows to configure optimizations globablly. For example if Add custom Array type to lualib #262 would be done as an option it won't be possible to use multiple libraries that are using different configurations
  • Libraries would be distributed like in TS - with a .lua and .d.ts files. To generate declarations tsc --emitDeclarationOnly could be used, but...
  • Some of semantics is lost on compilation phase. For example
export class A {
  get foo() {
    return 1;
  }
}
export declare class A {
    readonly foo: number;
}

So that would require customizing declaration generation to insert some metadata, similarity to what Kotlin is doing with generated java class files.

/** @TypeScriptToLuaMetadata {getters:["A.foo"]} */

export declare class A {
    readonly foo: number;
}

I personally prefer second variant, but it comes with more issues to solve.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions