-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Deduplicate helpers and helper types across multiple generated files #1109
Comments
Hi @absidue ; I agree this would be a good improvement. You mention making it conditional based on It would be amazing if you could work on a PR that does this; lmk if you have any questions/want any pointers about where to start. Thanks! |
I have not looked into the codebase much yet, but here is my proposal on how something like this could potentially be implemented. Please let me know what you think about it and if you have answers to some of the questions below. I would suggest generating and outputting the helper file right at the end, that way we only need to do it once. To do that we would need some way of keeping state across the different files that are processed so that we can keep a tally of what we actually need to output into the helpers file. Without knowing the codebase, in my head it makes the most sense to do that in the place that currently writes the helpers to the file, so instead of writing the helper or the type, it would record that it needs it in the "global" state and then generate an import instead. Potential difficulties and open questions:
|
Currently we use the Conditional Output feature of https://github.com/stephenh/ts-poet?tab=readme-ov-file#conditional-output https://github.com/stephenh/ts-proto/blob/main/src/main.ts#L521 I don't remember if ts-poet's conditional output tracking works across files 🤔. Originally ts-poet was very file-oriented, and still is, but it's also got a
Yep, agreed!
Agreed; I think a file name like
ts-poet has some helpers to do this sort of thing.
With this new approach, maybe we should default the "barrel file" / |
Currently when multiple proto files are passed in, the helper functions such as
isSet
,longToNumber
,bytesFromBase64
andbase64FromBytes
and the helper typesMessageFns
,Builtin
,DeepPartial
andKeysOfUnion
are written into every single file. To reduce that duplication it would be great if those helpers and helper types were written into a separate file once and then imported where they are needed, that way they would only appear once in the output. Similar to how TypeScript'simportHelpers
option works, but importing from generated file instead of an external dependency.If that extra file and imports are undesirable for Node.js use, it could be hidden behind the
--ts_proto_opt=env=browser
option, as people targetting web browsers usually want smaller code sizes and use bundlers which are able to handle the extra imports. To retain compatiblity with the existing output, the individual files could re-export the types.Example:
__ts_proto_helpers__.ts
__ts_proto_helpers__.ts
The text was updated successfully, but these errors were encountered: