-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
[TS] Codegen should import from individual modules #6145
Comments
Started to look at this again now but caught #6286. I believe we should also avoid generating I think this will require so many differences from the existing combined JS/TS generator that a new generator class will be needed and the old one can be kept around for those who want old style JS generation. The new generator could be TS generation only and leave JS targets to transpilation. Do you agree with this @krojew? |
What about |
I tried looking into this and it seems we have a giant mess after #6094. How do you import flatbuffers into TS now? |
@krojew not sure what you mean. Import should be the same as nothing have changed with regard to @types/flatbuffers, i.e it's assuming flatbuffers namespace exist globally. That said the situation could be improved in two ways:
|
That's the problem I ran into. I assumed @types will not be needed anymore, since we have native TS. But you can't import * from flatbuffers without them. I think typings should be removed altogether. We can then optimize imports, fix tests and introduce proper long type. |
Fully agreed @krojew but not that easy as I'm not sure we can remove the typings and still rely on a global "flatbuffers" so a large chunk of the work needs to be done in one go. |
We don't need to rely on global flatbuffers being present. By default, we add an import statement (which was pretty useless in the old days, which everyone disabled) so we just need a flatbuffers package to be present. But that leads to some problems which must be solved:
Now it's a good time to make big breaking changes, since it's likely we'll have a major version bump. |
Ok, so in my opinion it would be good to get rid of these modules exporting namespaces (they exist only to be compatible with current code gen):
But that means code gen needs to change for both TS and JS to produce code (TS and ES6-level JS) that imports from individual modules. However I also think that in doing so, the code gen should also produce exported individual modules instead of the namespaced single module thing it does now otherwise all of this would look messy. If an end user needs ES5 compatible JS it should be up to that end user to transpile it to that target. |
I'm a bit concerned about the legacy JS support in such case, although the changes are reasonable. What do you think @aardappel ? |
@krojew we don't need to break compatibility. If we keep the mentioned modules around we can leave JS code gen as is and focus on improving the TS code gen only. |
Codegen is one thing, but we need to make drastic changes to the lib itself. |
Tried some experiments and looks like #6094 broke some things. Our current FB lib is no longer compatible with @types/flatbuffers and also not compatible with with autogenerated import. |
Can you add a PR which makes TS tests pass with fb import enabled? |
Unsure at this point but haven't had much time yet. As I can't make tests work as is (with no FB import AFAIK) I need to dig deeper. |
Just as a general comment as someone who just started dealing with the Flatbuffer typescript libraries, it would be great to have the imports set up so that we don't need to deal with namespaces on top of modules and adding the actual |
@jkuszmaul this would be great to have. @bjornharrtell what do you think about not fixing current implementation to work with or without built-in imports and simply switching to a modularized version? |
@krojew agreed. And to me it then makes sense to treat JS the same. Or possibly only support TS codegen with JS through compilation of TS only. |
@bjornharrtell can you look into this? |
@krojew I'm willing to but unsure when I get enough time for it. For me this will require a substantial effort to get to completion. |
@krojew hard for me to tell how many people will still need older JS. If they do, how easy is it to transpile, and how hard is it to provide a native version for us? I know internally at Google we have people using JS with the Closure compiler, and I wonder if all this stuff will break them, and how. |
Transpilation is pretty easy - one just needs to specify desired JS flavor and tell TS to transpile it. In this sense, we will support all possible JS versions, including ones working right now. |
@krojew and @aardappel I think we should be more clear about what we aim for with regard to code gen, npm package and what's possible for flatbuffers 1.x vs 2.x. For the npm package I believe it is possible to make it offer two variants for maximum compatibility:
I believe we might already be there but needs verification and there is the issue with the test failures. For the code gen I see three variants possible:
To reduce amount of work I would suggest dropping code gen 2 above because it does not have much value. Users that want that target should be able to transpile 3 to 2 on their own. If there is to be a breaking flatbuffers 2.x release in the future I would also consider dropping compatibility (the 1:s above) to further reduce work and maintenance. I do not know closure compiler setups can deal with this but it doesn't seem sensible to let that dictate a 2.x to force many years of maintenance of ES5 level JavaScript support? |
My opinion is we should drop package numer 1 right now and go straight to modules. As far as codegen is involved, I believe we should keep options 2 and 3 to allow users to choose between JS and TS. After all, if we publish a transpiled package with typings, there's no reason not to support both JS and TS generation, especially given the similarities. |
@krojew ok agreed but I would implement it so that code gen internally is only TS and depend on tsc to compile to JS target. @aardappel do you agree? I think it's reasonable to expect that any living project that depends on JS should be able to handle ES6-level code and modules. |
I think that's just adding more work. The existing codegen can only use different imports and remove |
@krojew hmm yes, I think you are right. For whatever reason I had gotten into my mind that I needed a fresh start on the code gen, perhaps because I found it too messy to make the |
@krojew now I remember an additional reason why I thought TS/JS code gen need a fresh start for modules... current code gen generates old style namespaced output into a single script file when it really should be separate files/modules to be properly modernized. Thoughts? |
We can remove namespace from TS output, but that would be incompatible with the
To be honest, I'm all for the last option. |
@krojew mabye I'm misunderstanding something here but current code gen for JS/TS outputs a single file regardless of |
No, it outputs a file per .fbs. It only outputs single files with |
@krojew I meant it does not split namespaces/tables into separate modules similar to other languages that have support for that. And unless you do that you will have to simulate namespace in some way inside a single module/file... |
I've experimented with a spike at #6302 that doesn't change anything else than dropping the prefix (adding imports not automated yet) and it seems possible to make it work but I still feel quite strongly that if we are going after modern modularized TS/JS in general the code gen should reflect that and also create similar modularized code. |
Ok, I get what you mean. We can do the same thing java generator does and split them. As for lib imports - we need to alias them (or namespace in another way), because the user can still create tables named Builder etc. |
Again, I am not familiar with what's common in this eco-system. I will defer to what you guys think is reasonable. |
I'm slowly starting to lean towards dropping direct codegen for JS, and rely on transpilation. This would include an additional step for our users, but it's less maintenance for us, which is more or less a copy of TS. Ideally, it would be grat if flatc would detect tsc and do the transpilation in flight. What do you think @bjornharrtell @aardappel ? |
@krojew I agree it should make the codegen a bit more simple and as we have concluded the codegen needs a bit of work. I agree that if flatc could just detect and use tsc it would be nice, but as JavaScript have so many flavors that you might want to tweak (ES5, ES6 etc..) perhaps it's best to leave it up to the user. |
That's one of the arguments that's pushing me towards eliminating direct JS support - given current zoo of JS flavors, it might be a good idea to have the user decide. We can try to make flatc help with this somehow in the future. |
@aardappel do you think such approach is acceptable? It would simplify generation and testing a lot. |
Hmm, not sure if I like the idea of Then again, if it was explict with a flag, i.e. Coincidentally, I recently came across https://github.com/sheredom/subprocess.h which is something we would need, to portably run a tool on all OSes. Since currently FlatBuffers has no "dependencies" such a single header lib could be nice to use. |
We can leave it to the user. We should also update the documentation then with a JS tutorial involving tsc. |
This most definitely should be linked to #6353 |
Since this issue is linked to 2.0 release, how are we doing on it? |
@bjornharrtell this can be closed with the new TS generation, right? |
@krojew yep! |
This will allow more more optimal and tree-shakable final output also when transpiled to modern JS.
NOTE: Not relevant for current JS target as it's ES5-ish. Might not be worth pursuing a modern JS target as that can be transpiled to from the TS target.
The text was updated successfully, but these errors were encountered: