dgp1130 / bxpb

Browser Extension Protocol Buffer Client/Service Implementation
MIT License
0 stars 0 forks source link

Generate `.ts` files instead of `.d.ts` files #4

Closed dgp1130 closed 4 years ago

dgp1130 commented 4 years ago

In https://github.com/dgp1130/bxpb/commit/7ee936c6092d09f049cdf6316a186dbf4e4fe686 and https://github.com/dgp1130/bxpb/issues/1#issuecomment-638586303, we discovered that hand-writing .d.ts files is a great way to shoot yourself in the foot. Instead, we should generate .ts files and then use tsc to emit .d.ts and .js files from them.

Unfortunately the structure of @bxpb/protoc-plugin as simply a plugin to protoc makes this a little tricky, as we aren't really supposed to edit the actual file system as a tsc compilation would do. I see two potential ways of making this happen:

  1. Use typescript as a library in @bxpb/protoc-plugin to create an AST from input source .proto files. Then compile the AST completely in-memory to get an output .js and .d.ts file as simple strings. Those strings can then be returned to protoc to generate the files.
    • I'm not sure how flexible typescript APIs are to perform an in-memory compilation. Module resolution might get weird here.
    • I suspect that we could stub all the filesystem APIs in CompilerHost to read from a virtual file system managed by BXPB (node_modules/ might get tricky here). That might be sufficient to do everything in-memory?
  2. Make a temp directory and generate the .ts files there. Then invoke tsc as a subprocess on the directory to generate .js and .d.ts files. Finally, read the output files back to return to protoc so they can be written to the correct location.
    • This gets pretty hacky and is definitely not how protoc plugins are supposed to work.
    • Future Bazel integration may not like this strategy, as you're not supposed to write to undeclared output files.
      • Not sure if Bazel exempts temp directories.
      • If that is a problem, we could handle this at the action level by declaring the temp files and passing them to the BXPB compiler, but this would be a pretty ugly workaround.
dgp1130 commented 4 years ago

Obsolete per project shutdown as described in https://github.com/dgp1130/bxpb/issues/1#issuecomment-647053141.