Hi, I'm new to grpc-web and I was checking your Web basics tutorial when I noticed that the client code generator only supports Closure and common.js
Now that ES Modules are supported on all modern browsers, is there a chance that you'll have a generator for them? That would allow the library to be used without any build process which would be great imho
This isn't a direct answer, but the TypeScript output _does_ support modules. If you're not using TS in your project, a workaround would be to re-compile from the TS output of protoc down to ES6.
@jonahbron
I tried this, it was a bit annoying to configure but I managed to get it working
Here's a sample bash script for generating the ts files and the tsconfig.json to compile it to esm in case anyone has this same problem
protoc -I ./protos protos/*.proto --js_out=import_style=commonjs:./protos/ts --grpc-web_out=import_style=typescript,mode=grpcwebtext:./protos/ts
{
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"allowJs": true,
"outDir": "../esm",
"rootDir": "./",
"strict": false,
"moduleResolution": "node",
"esModuleInterop": true
}
}
Then again this only worked for the grpc-web_out files, the other files were still generated as commonjs modules, but I guess it's something
This actually seems to be a real problem when trying to use grpc-web with something like create-react-app or any Babel 7 Typescript. Since they don't support commonjs only ESModules. Currently the only solution seems to be to manually rollup the exports using something like this https://github.com/rollup/rollup-plugin-commonjs. Would love to find a more permanent solution though.
ESM modules would be really nice.
In the mean time, if you use grpc web with stencil and rollup, see this comment https://github.com/ionic-team/stencil/issues/1343#issuecomment-589528554
That would allow the library to be used without any build process which would be great imho
By the way, this is only true if both grpc-web AND the generated client-stubs has esm-support, so this issue has to be solved in two parts:
esm-compatible grpc-web-client:
import_style=typescript-option :+1: EchoServiceClient.ts above, is to update the import paths to be esm-compatible._Example:_
| Generated import paths | Modified to be esm-compatible | CDN alternative |
|------------------|-----------------------------------------|----------------|
|
|
|
|
protoc-gen-grpc-web-version:$ brew info protoc-gen-grpc-web
protoc-gen-grpc-web: stable 1.2.1 (bottled)
grpc-web-package to be esm-compatible.After modifying the paths in Part 1, I will still get an error if I try to run my generated client directly in a browser, because the modules imported from ../node-modules/grpc-web/index.js and https://cdn.jsdelivr.net/npm/[email protected]/index.min.js are NOT esm-compatible.
Neither is './echo_pb.js', and it's depedency ../node-modules/google-protobuf/google-protobuf.js, for that matter, but that is out of scope of this repo.
grpc-web-package can be converted to be esm-compatible in a PR here --> https://github.com/Arxcis/grpc-web/pull/1grpc-web here --> https://github.com/Arxcis/grpc-web/pull/2es_modules-folder, with transpiled .js files.closure-library~ the code I have generated has a few circular dependencies of top-level values, which the browser's ES modules loader is not too happy about. (details here --> https://github.com/Arxcis/grpc-web/pull/2)
Most helpful comment
This actually seems to be a real problem when trying to use grpc-web with something like create-react-app or any Babel 7 Typescript. Since they don't support commonjs only ESModules. Currently the only solution seems to be to manually rollup the exports using something like this https://github.com/rollup/rollup-plugin-commonjs. Would love to find a more permanent solution though.