Skip to content

Conversation

@bjornharrtell
Copy link
Collaborator

@bjornharrtell bjornharrtell commented Dec 1, 2020

Modernize TS/JS code gen by doing a reworked TS only code gen.

TODO

  • Split output into modules (files)
  • Devise a sensible alias/prefix for flatbuffers imports to not collide with user symbols
  • Track needed imports
  • Alias imported symbols if same name from different namespaces
  • Do not generate extra ;
  • Fix wrong static method generation
  • Drop flatbuffers namespace completely
  • var to let or const
  • Remove ns wrapping/prefixing
  • Fix object API
  • Remove obsolete generator options
  • Fix and verify JavaScriptFlexBuffersTest.js
  • Remove jsdoc type annotations

Documentation

  • Decide what to do about Compiler.md, JavaScriptUsage.md, Tutorial.md and javascript_sample.sh

TODO (undecided)

  • Resolve closer relative paths in imports
  • Avoid unused imports
  • Proper indentation
  • Const correctness
  • noImplicitAny compliant generated source

@aardappel
Copy link
Collaborator

@krojew

@krojew
Copy link
Contributor

krojew commented Dec 3, 2020

This is a good first step. Next would be splitting per table as java does?

@bjornharrtell
Copy link
Collaborator Author

@krojew yep might be, I've added a TODO list in the issue description.

@krojew
Copy link
Contributor

krojew commented Dec 14, 2020

How's it going? Do you need any help?

@bjornharrtell
Copy link
Collaborator Author

@krojew afraid I still haven't gotten free time for it, if you want to get started now please do! I aim to get some alone and free time in xmax holidays to possibly pick this up so somewhere 25-30 december. Keep me in the loop. :)

@bjornharrtell
Copy link
Collaborator Author

@krojew will try to get somewhere with this today. What's your status?

@krojew
Copy link
Contributor

krojew commented Dec 26, 2020

I haven't managed to get any free time to work on this yet. But at least, I think we know how to proceed - remove JS support and output a file per table, as Java does. This implies no more --get-all support, but I don't think it's a problem.

@bjornharrtell
Copy link
Collaborator Author

I'm slowly realizing this will require more than just simple tweaks to existing code and I'm unfortunately quite far from being able to get somewhere. Ideally I would like to have a better setup/testing story than to compile, run the flatc binary and inspect output.

@bjornharrtell
Copy link
Collaborator Author

@krojew we opted to use kebab-case in #6095. When generating code all other languages seem to use PascalCase as folder and file names. I wonder if it would not be better to use PascalCase for TS too, for consistency and also to some extent simplicity?

@krojew
Copy link
Contributor

krojew commented Dec 27, 2020

I think we should keep kebab case, since we would cause problems for Angular users otherwise.

@bjornharrtell
Copy link
Collaborator Author

@krojew ok I have no strong opinion so agreed.

@bjornharrtell
Copy link
Collaborator Author

@krojew getting somewhere, now generating separate files in kebab case. (still large amount of work remaining on actual code output though)

@bjornharrtell bjornharrtell changed the title [TS/JS] TS/ES6 modules spike [TS/JS] New gen TS code gen Dec 27, 2020
@bjornharrtell
Copy link
Collaborator Author

For future reference this is what imports could look like:

import { SIZE_PREFIX_LENGTH } from '../../js/constants'
import { Long, createLong } from '../../js/long'
import { Offset, Table } from '../../js/types'
import { Builder } from '../../js/builder'
import { ByteBuffer } from '../../js/byte-buffer'
import { Encoding } from '../../js/encoding'

@bjornharrtell
Copy link
Collaborator Author

@krojew any ideas on how to alias flatbuffers imports? A flatbuffers_ prefix or simply _ perhaps?

@krojew
Copy link
Contributor

krojew commented Dec 27, 2020

I suggest _flatbuffers_, since it's both unique and obvious.

@bjornharrtell
Copy link
Collaborator Author

@krojew output looks promising now however not really tested at this point. Feel free to do an early review.

@bjornharrtell
Copy link
Collaborator Author

bjornharrtell commented Dec 28, 2020

@krojew instead of _flatbuffers_ prefix we could reexport all of flatbuffers public API in a single entry point flatbuffers module and import with * as flatbuffers from it.

Copy link
Contributor

@krojew krojew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also remove JS-specific flags.

"target": "ES6",
"lib": ["ES2015", "ES2020.BigInt", "DOM"],
"noImplicitAny": false,
"strict": false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test for maxium strict support.

@bjornharrtell
Copy link
Collaborator Author

@krojew I'm not 100% confident about my approach to import tracking and namespace aliasing when needed which replaces the need for prefix/namespace wrappings. But I've just now verified it seems to be working in my external project where the end result bundle ends up almost 40% smaller so that's a nice side-effect of this work.

@krojew
Copy link
Contributor

krojew commented Dec 28, 2020

I would need to see regenerated test files to have an opinion. Right now, it's hard to see what the actual output is.

@bjornharrtell

This comment has been minimized.

@bjornharrtell
Copy link
Collaborator Author

Rebased with more success (I sure hope so).

@krojew
Copy link
Contributor

krojew commented Jan 15, 2021

@aardappel can you merge?

@krojew
Copy link
Contributor

krojew commented Jan 19, 2021

@aardappel any news on this?

@aardappel
Copy link
Collaborator

Thanks for all the hard work :)

@aardappel aardappel merged commit 760c657 into google:master Jan 19, 2021
@bjornharrtell bjornharrtell deleted the ts-spike branch January 19, 2021 22:06
opts.natural_utf8 = true;
} else if (arg == "--no-js-exports") {
opts.skip_js_exports = true;
} else if (arg == "--goog-js-export") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work now? I have a bunch of users internally who are setting this flag and it no longer is there.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjornharrtell it is hard for me to see what changed here since it such a big merge. how does --goog-js-export work now that the flag is removed?

@aardappel

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems unwise to have removed that flag or any code related to it, since we couldn't test that externally. Apologies if I didn't spot that during this huge PR and huge review..

But this has been merged for a while now, so all we can do is try and fix this forward.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory is vague at this point but I think the general idea was that TS is the only target and exports using es modules. If someone needs another module format they'd have to transpile it themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ CI Continuous Integration codegen Involving generating code from schema documentation Documentation javascript json typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants