-
Notifications
You must be signed in to change notification settings - Fork 764
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
Reduce compiled JS file size #270
Comments
A bit more info, as it stands:
A couple more observations:
|
For the protobuf messages, could you check the compiled.js with only the protobuf messages? I.e. it is useful to find out the respective sizes of google-protobuf v.s. goog.net.streams |
Would be super interesting if you could share any news here. The biggest hurdle to adopt grpc-web for our front end people is the size of the dependencies / generated code. |
@codesuki could you share the generated code size v.s. dependencies respectively for your front-end project? Optimizing protobuf-JS is not something we could do easily but grpc-web stub and runtime may have room for optimization. |
Will need to dig into the bundled code but at a glance I believe it's because of the same reason as this: Currently in
Will get back on this thread once I confirmed this. If it works it would drastically reduce the bundle size. |
I think @jjbubudi is right, I'm seeing smaller bundle sizes and we use advanced optimizations for all our code (I don't have exact numbers to share here because there is more compiled into our bundles, but our files are smaller than the sizes mentioned above). If we go ahead and add |
IIUC, Closure Compiler will ignore Finally got it working locally and passing all mocha tests. Here is the result so far:
Will send a PR soon for review and further testing if anyone is interested. |
Wow, that's great news @jjbubudi! However, /** @const {boolean} */
const EXPORT_GRPC_WEB_SYMBOLS = true;
function foo() {}
if (EXPORT_GRPC_WEB_SYMBOLS) {
goog.exportSymbol('foo', foo);
} Let me know when you submit your PR. |
Ah, ok. I've submitted PR #422. Looks like it needs to be further updated to avoid affecting existing Closure users. |
@Yannic Actually, looking deeper at the optimized code, Closure Compiler has stripped away all the Here is the pretty printed version of the new bundle FYI: https://gist.github.com/jjbubudi/86549bd597ffc4d74b30990e7a385f44#file-grpc-web-js-L1287 |
Did @jjbubudi's fix ever end up being available? I've encountered the same issue — with a trivial I've also tried using Closure Compiler via Webpack, however, this predictably fails at Thanks for the hard work — it otherwise works very well. I would just love to be able to strip away the google-protobuf's unused parts somehow, preferably with Webpack. |
@nehbit It went into 1.0.4, see https://bundlephobia.com/result?p=grpc-web@1.0.4 |
I built six single-field protos and my bundle.js grew by 332 KiB. This is unacceptable. So far, it seems to me that the only solution offered is to learn a new language (Closure) and translate our code to it. Is this correct? Now I will try Protobuf.js. |
I believe the references are to the Closure compiler |
* Define and Export RpcOptions interface This allows us to import it in ts-protoc-gen and avoid us having to duplicate the definition. * Fix import staements
I just built a simple "hello world" test, and ended up with about 250k of minified Javascript. The comments above suggest that this issue should be resolved. Am I doing something wrong or is there still a problem somewhere? By the way, thank you for all your work on this. I believe grpc-web will be the future of client-server communications on the web, even though there might be a few blocks at the moment. |
@PeteX thanks for the update and your kind comment! :) Not having much context of this issue myself, i just tried to build the helloworld example and i actually got a 292KB main.js file... I did a quick analysis using the webpack-bundle-analyzer and here's the results: It looks like:
I'm not sure if this is expected or not since i don't have too much context on the history.. but just sharing my results.. :) (Although by a quick look it seems not necessarily easy to trim those size from the google-protobuf.js lib?) P.S. Command i used for webpack-bundle-analyzer:
|
Thanks, I think I understand the situation now. |
In 2022, |
Why was this issue closed? |
@egor-yolabs Hi Egor thanks for the input :) I guess i closed it because the majority of the code size is due to the use of protobuf JS. There isn't much we could do on our side since it's a required dependency. If you'd like, maybe you could consider filing a bug against https://github.com/protocolbuffers/protobuf-javascript based on the comments in #270 (comment) and #270 (comment)? Thanks! :) |
I think we can keep it open for now, but we need to decide on a "reasonable" size so we know what's the target :) Input is welcome :) |
I see that @sayjeyhi recently logged protocolbuffers/protobuf-javascript#124 with them which is related to this issue. |
I'm curious what's the reason you think the dependency we have pulled in is a tiny part of the library, and should not in turn pull in the majority of the protobuf library? if you are aware of any kind of static analysis that would show the percentage of the library we should in fact pull in, it would be helpful :) |
You may not have understood what I meant. I see you ran the webpack-bundle-analyzer above which is what I've been running too on my project. In my project, I have a very simple case which imports the generated enums that were generated from some very simple protos (no service definitions). The generated code contains only two references to Regardless of that, it looks like someone finally figured out how to do grpc in the browser correctly: https://github.com/bufbuild/connect-web |
Yeah this is my question here. Would love to see an analysis of the dependency here to see if the above is indeed true.
The grpc-web package is built using Closure compiler, and the parameters are specified here, where dependency pruning is already enabled. If anyone is able to experiment with the flags can showcase a code size reduction, it would be much appreciated.
I'm glad you found a solution that works for you :) This repo indeed may not have all the latest features, but it is the official solution used internally and will stay up-to-date as the grpc-web protocol evolves. |
I know very little about JS bundling and tree-shaking, but it seems that some of the foundational technology choices here and in Google's JS protobuf runtime make controlling bundle size difficult. It's understandably hard to revisit those decisions without breaking existing users. This is a great place for third-party implementations to experiment and find a better path forward. A good first step toward encouraging a robust gRPC-Web ecosystem would be to formalize the protocol specification: grpc/grpc#30565 (I realize that this is a bit of a tangent, but I thought folks interested in this issue may also want to weigh in on the protocol formalization.) |
I've opened a PR(protocolbuffers/protobuf-javascript#128) on |
Good news guys! I am not sure do we need to update the version of this pkg to use the latest version of Edit: I've opened this PR: #1291 to use the new version and fix this huge bundle size issue. |
Update: Due to an issue in CommonJs, the code size optimization was rolled back in protocolbuffers/protobuf-javascript#143. Good new is that the team is actively working on re-enabling that optimization (comment) 😃 @sayjeyhi FYI :) |
FYI, @dibenede FYI :)
|
I did a test build of helloworld example (similar to above) using #1452, and analyzed the bundle size. And the size of google-protobuf.js went down from 230KB -> 66KB:
@dibenede FYI Before (google-protobuf: 230KB, total: 286KB)After (google-protobuf: 66KB, total: 122KB)This is a significant improvement from before! I recommend users to migrate to google-protobuf I will close this issue for now and we can discuss follow-ups here and re-open or open new issues when necessary. Thanks :) |
As it stands, our npm package
index.js
is 218KiB (which is being compiled by the closure compiler here).With the Echo example echo.proto, that has about 7 message types, 1 service, 3 unary methods and 5 streaming methods:
compiled.js
compiled by the Closure compiler: 317KiBdist/main.js
compiled bywebpack
: 415KiBbrowserify
: 479KiB.d.ts
typings generated, and then compiled bywebpack
: 499KiBI tried
babel-minify
anduglify-js
on some of these JS files and the output are not any particularly smaller.There should be some other things we can do to further minimize the compiled JS code size.
Reference: #79
The text was updated successfully, but these errors were encountered: