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:
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?
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.
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 usetsc
to emit.d.ts
and.js
files from them.Unfortunately the structure of
@bxpb/protoc-plugin
as simply a plugin toprotoc
makes this a little tricky, as we aren't really supposed to edit the actual file system as atsc
compilation would do. I see two potential ways of making this happen: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 toprotoc
to generate the files.typescript
APIs are to perform an in-memory compilation. Module resolution might get weird here.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?.ts
files there. Then invoketsc
as a subprocess on the directory to generate.js
and.d.ts
files. Finally, read the output files back to return toprotoc
so they can be written to the correct location.protoc
plugins are supposed to work.