Skip to content

API improvement ideas #6

@dabbott

Description

@dabbott

Overall the compiler API works well, but there are a couple things that I think could be improved to make it easier to figure out when diving in for the first time:

  1. I found the names parseWorkspace and parseFile confusing, since they don't necessarily parse anything (e.g. if everything I need is in helpers already) and they do things beside parsing too (e.g. write files).

    Maybe convertFile and convertWorkspace?

  2. I found it confusing that the return value of parseWorkspace would differ by plugin - the permissive return type of Promise<any> makes it hard to guess what value it will return.

    I think these APIs should return something more restricted (like Promise<void>) for consistency, and if a plugin wants to expose other methods, that's fine. E.g. the tokens plugin could have a createFlatTokens() -> Promise<ConvertedWorkspace> of some kind, and parseWorkspace will be a thin wrapper around that.

  3. I found it confusing that the return value of parseWorkspace would change based on the output option.

    I got tripped up by this for a bit when trying to generate a temporary flat tokens file in the android plugin - it's hard to infer this is the behavior just by looking at the types.

    One approach for configuring output that could work well is to have plugins write all files to an in-memory FS, and only write the in-memory FS to the real FS in bin.ts, based on the output option. This would have a few advantages:

    • We can easily support other variations of output:
      • a --dry-run option where we print a helpful diagnostic message showing a name/snippet of all files that will be written, useful for choosing config options and debugging plugins
      • a JSON-map output where we print the whole file tree as JSON for easy consumption by other tools
    • Plugin authors don't have to handle a missing output option, it'll get taken care of automatically
    • If plugins call each other, or if we allow running multiple (e.g. both js and swift) at the same time: we can track which plugins wrote which files, by adding a separate layer to the in-memory FS for each plugin. A plugin could also be called as a temp intermediate step.
    • Prevent a partially-generated workspace on disk if a plugin fails
    • Easier to test the plugins, since they're side-effect free
    • Easier to import the compiler in browser and other environments

    I was playing around with the in-memory fs packages that webpack uses, and wrote this proof-of-concept to validate that these packages did what I thought: https://gist.github.com/dabbott/a7c23da95b90ef34374144ff5d61670e. We'd need to fiddle around with the details, but so far I'm liking this direction.

  4. We're probably gonna need a way to write binary files soon, for image assets. It looks like we only have a helper for string files at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions