-
Notifications
You must be signed in to change notification settings - Fork 4k
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
THRIFT-5811: Add ESM support to nodejs codegen #3083
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, we were actually planning to add something similar but for the browser. We can make a follow up PR to add support for esm
on the browser side as well.
While testing this locally we noticed import statements are not changed to the esm format.
The existing PR keeps the require(type)
style instead of changing it to the esm format of:
import * as <Identifier>_ttypes from <Identifier>.mjs
(in t_js_generator.cc line 584)
Other than that it worked as expected 👍
cc: @tri613
That's interesting, because this is our exact use case, but we use the nodejs package in the browser. Is your plan here to support es modules that can directly execute in the browser, with no build tools at all?
I must have missed this, I'll push an update. It also will need syncing with the nodejs CI changes (#3082). |
How do you exercise this code path? All of the tests files inside |
276325d
to
0cc7b74
Compare
Hello! Thanks for this great PR again. 👍
We tried to use this PR's changes to build our own service, and when looking inside the generated files, we noticed that the import statements are like this:
As @thomasbruggink suggested we think this is related to the
We had some issues when integrating the nodejs thrift build into our project, so we thought it would be nice if we could decouple the esm build from the nodejs dependencies. And of course, supporting native esm browser packages would be beneficial too :D |
Does this happen when a thrift file includes another thrift file? I don't think this is something the tests are covering atm. I'll investigate and add a test for it.
It worked for us by polyfilling a few NodeJS dependencies (such as Another part that is missing is browser tests for the NodeJS package, but I also have plans to contribute these too. This does call into question the necessity of a separate nodejs and js package. Does anyone do web development without a bundler currently? |
Client: nodejs This adds a flag to the JS generator to output ES modules instead of CommonJS. This is only valid when targeting node. A lot of the changes here are to test this. The `testAll.sh` script now generates an ES module version of the services and types, and tests the client and the server with these. This has a few knock-on effects. Firstly, any module that imports a generated ES module must itself be an ES module, since CommonJS modules cannot import ES modules. ES modules also do not support `NODE_PATH`, so instead the tests directory is converted into a node package with a `file:` dependency on the root thrift package.
0cc7b74
to
eb99583
Compare
@tri613 @thomasbruggink I've updated this to render includes correctly, plus added a test ( However, CI will fail on this until #3087 is merged, since the current version of eslint and prettier do not support the dynamic import syntax, and the nodejs tests now rely on this. |
At the moment we rely on the web version while also having a bundler in place. It looks to me like the web version is a leaner but less feature rich (although using some older standards like xmlhttprequest). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i have no power in the repository but we've tested this and it works for us ^^
thanks @cameron-martin for making these changes
Client: nodejs
This adds a flag to the JS generator to output ES modules instead of CommonJS. This is only valid when targeting node. A lot of the changes here are to test this.
The
testAll.sh
script now generates an ES module version of the services and types, and tests the client and the server with these. This has a few knock-on effects. Firstly, any module that imports a generated ES module must itself be an ES module, since CommonJS modules cannot import ES modules. ES modules also do not supportNODE_PATH
, so instead the tests directory is converted into a node package with afile:
dependency on the root thrift package.[skip ci]
anywhere in the commit message to free up build resources.