Skip to content
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

JavaScript: es6 module generation #75

Open
tbillington opened this issue Feb 5, 2018 · 21 comments
Open

JavaScript: es6 module generation #75

tbillington opened this issue Feb 5, 2018 · 21 comments
Labels
enhancement New feature or request javascript triaged Issue has been triaged

Comments

@tbillington
Copy link

Asking because I couldn't find anything about generating es6 modules instead of closure or common via protoc (https://developers.google.com/protocol-buffers/docs/reference/javascript-generated#package).

Is it possible or on the roadmap?

@lfmunoz
Copy link

lfmunoz commented May 2, 2018

Yes it isn't specified anywhere what would be the correct syntax. My guess is this:
protoc --js_out=import_style=es6,binary:${DEST} -I ${SRC} ${PROTO_FILE}

but the code generated isn't showing any imports or exports so it must not be working. Can someone confirm what the correct syntax is?

@TeBoring TeBoring added javascript enhancement New feature or request labels May 14, 2018
@jwall149
Copy link

jwall149 commented Jul 10, 2018

My version is

$ protoc --version
libprotoc 3.5.0

It doesn't even complain if any random import_stype provide: protoc --js_out=import_style=random,binary:${DEST} -I ${SRC} ${PROTO_FILE}

@xfxyjwf xfxyjwf self-assigned this Aug 22, 2018
@xfxyjwf
Copy link

xfxyjwf commented Aug 22, 2018

es6 module isn't supported yet.

@juanjoDiaz
Copy link

Any chance of ES6 imports happening soon?

Currently blocking grpc/grpc-web#237

@sikmike
Copy link

sikmike commented Jul 25, 2019

Are there any news about ES6 support? I am trying to use grpc-web with commonjs in Vue.js, but get an error.

@thesayyn
Copy link

thesayyn commented Jan 23, 2020

You should consider using https://github.com/thesayyn/protoc-gen-ts. It generates classes ES6 compatible.

@seishun
Copy link

seishun commented Feb 17, 2020

Would you accept ES6 support in a pull request?

@Asher-
Copy link

Asher- commented Dec 28, 2020

Why is it considered to be in any way acceptable to still be relying on ES5 code 5 years after its deprecation?

@ppeou
Copy link

ppeou commented Jul 31, 2021

Please enhance protoc compiler to generate es6 modules.

@gunters63
Copy link

gunters63 commented Feb 22, 2022

I made a patch for an older commit of protobuf for creating ES6 compatible code. It works fine for me, although it is far from being good enough for a pull request:

protobufjs/protobuf.js#1567

@acozzette acozzette transferred this issue from protocolbuffers/protobuf May 16, 2022
@dibenede
Copy link
Contributor

This is a reasonable request, but not planned.

@dibenede dibenede added the triaged Issue has been triaged label Sep 23, 2022
@smnbbrv
Copy link

smnbbrv commented Oct 24, 2022

This is very disappointing. Some libraries already reject to support non-module projects and you don't plan to support modules 👍

Such a good idea of protobuf / grpc(-web) and such a poor implementation and maintenance that people who just want to use it need to use third party libs. Non-tree-shakable, builder pattern in javascript (what???), no typescript / Promises / rxjs support, no alignment to the modern technologies and the outside world (actually this issue prevents any project with "type": "module" from using protobuf) and a lack, if not missing, normal, human-readable documentation at one place. I really love protobuf and grpc, and I'm also an author of third-party plugin (actually for Angular, which should be supported by Google and not by me) and many workarounds, but I'm so tired of all the problems I need to face to just be able to use it, that I really think on quitting. All this outweights all the pros that protobuf gives.

The following is maybe not that kind, but that's what I feel. The current state of JS support just looks like a stub that's done for only one reason: to write "we support all the languages" or "here is the implementation, if you don't want to use it it's your problem". If you want your technology to stay alive you should probably reconsider what you are doing and at the very first target all the issues above and finally get away of the stone age. Every project has problems, every framework has issues, but at least they should be targeted and there should be a clear future.

@gunters63
Copy link

I can highly recommend https://github.com/timostamm/protobuf-ts as an alternative code generator. Works great in my 100% Typescript codebase. It even still uses protoc.exe under the hood.

@gonzojive
Copy link

What would be the expected output for ES6 module-style imports? It doesn't seem too hard to modify the generator C++ to support this once the desired behavior is fully specified.


Perhaps the following is a bit of an aside - I'm not too familiar with Typescript imports: What makes sense to me is that a generated .js (and .d.ts) file for a proto would have a canonical "import path" to use when importing it. This would make it easy to generate the imports for dependencies, and it would also make it easier to determine how to import a proto type from user code. However, associating a module with a canonical, absolute import path seems like a big pain in JS/JS based on casual Googling around 1, 2.

@smnbbrv
Copy link

smnbbrv commented Dec 4, 2022

Actually all imports should be done with native JS imports and to support "type": "module" it should also include the file extension, for both typescript and JavaScript it should be '.js'. This should also be adapted in well-known-types which actually made it impossible for me to proceed with patching the protoc's original output, cause changing the well-known-types in the lib was already too much (and if I remember it right they are provided by direct file reference, that's why they aren't processed properly by node.js that would normally see that the library's type is not module and gracefully treat it as it does with legacy libs).


Offtopic: Wouldn't it be better to make a separate generator for JavaScript like the guys already did with Golang? Extending C++ compiler is not something one expects while generating JS. This would give a chance to generate a modern tree-shakeable code with direct typescript support

@Asher-
Copy link

Asher- commented Dec 4, 2022

All imports should be done with native ES6 "import" statements. Code should not require a loader to load. That's just poor form.

@safanaj
Copy link

safanaj commented Jan 3, 2023

what about this super tiny patch? https://github.com/protocolbuffers/protobuf-javascript/pull/150/files , what do you think?

@gonzojive
Copy link

https://github.com/protocolbuffers/protobuf-javascript/pull/156/files is pretty close to completing this but needs testing and support for the built-in well-known-types.

@codedread
Copy link

With Angular updating their application-builder to use esbuild it seems like protoc --js_out=import_style=commonjs will generate:

var jspb = require('google-protobuf')

will prevent the esbuild from working, meaning we are stuck in webpack land.

It is unclear to me how long the old builder support will be maintained by the Angular team.

@dibenede
Copy link
Contributor

With Angular updating their application-builder to use esbuild it seems like protoc --js_out=import_style=commonjs will generate:

var jspb = require('google-protobuf')

will prevent the esbuild from working, meaning we are stuck in webpack land.

It is unclear to me how long the old builder support will be maintained by the Angular team.

@codedread I followed up with the Angular folks about this and you should be able to continue to use commonjs require()'s with esbuild. They noted that additional configuration may be needed for the Vite dev server:

"The development server has an option (prebundle) which can be used to fully disable the feature or exclude specific packages. In the case of google-protobuf generated files with require, one of these options would be needed with the tradeoff being longer rebuilds during development. An example subset of the configuration would be similar to the folllowing:"

"serve": {
  "executor": "@angular-devkit/build-angular:dev-server",
  "options": {
    "prebundle": false
  }
}

If you've encountered a specific issue during a migration/prototype, I'm happy to try to dig into it.

@stasberkov
Copy link

Why is this not yet implemented? Is protobuf-javascript abandoned project and we should use other projects instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request javascript triaged Issue has been triaged
Projects
None yet
Development

No branches or pull requests