-
Notifications
You must be signed in to change notification settings - Fork 3
Description
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:
-
I found the names
parseWorkspace
andparseFile
confusing, since they don't necessarily parse anything (e.g. if everything I need is inhelpers
already) and they do things beside parsing too (e.g. write files).Maybe
convertFile
andconvertWorkspace
? -
I found it confusing that the return value of
parseWorkspace
would differ by plugin - the permissive return type ofPromise<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 acreateFlatTokens() -> Promise<ConvertedWorkspace>
of some kind, andparseWorkspace
will be a thin wrapper around that. -
I found it confusing that the return value of
parseWorkspace
would change based on theoutput
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 theoutput
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
- a
- 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
andswift
) 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.
- We can easily support other variations of output:
-
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.