protobuf-javascript icon indicating copy to clipboard operation
protobuf-javascript copied to clipboard

Generalize npm dependency support for generated js code

Open clehene opened this issue 5 years ago • 5 comments

Sister issue of https://github.com/grpc/grpc-web/issues/584 (copying content)

All generated code currently generates js import paths with similar structure with that of the imported .proto files. The one exception for protobuf well-known types - google-protobuf based ont he import prefix: https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.cc#L128

string GetRootPath(const std::string& from_filename,
                   const std::string& to_filename) {
  if (to_filename.find("google/protobuf") == 0) {
    // Well-known types (.proto files in the google/protobuf directory) are
    // assumed to come from the 'google-protobuf' npm package.  We may want to
    // generalize this exception later by letting others put generated code in
    // their own npm packages.
    return "google-protobuf/";
  }

Equivalent code in grpc-web: https://github.com/grpc/grpc-web/blob/master/javascript/net/grpc/web/grpc_generator.cc#L461

This will generate something like

import * as google_api_annotations_pb from './google/api/annotations_pb';
import * as google_protobuf_timestamp_pb from 'google-protobuf/google/protobuf/timestamp_pb';

An import option would allow users to explicitly override the relative path and specify a npm package at .proto import could be the right approach to generalize this.

clehene avatar Jul 03 '19 22:07 clehene

I think this is related to protocolbuffers/protobuf-javascript#65. I would love to see this feature implemented one way or another as it would make protobuf much easier to use.

aberasarte avatar Jul 29 '19 11:07 aberasarte

I think we need an import option js_package (like go_package for go) could use the npm package to override the relative path.

For example:

// in google/api/annotations.proto
option js_package = "google/api/annotations_pb";
// in example.proto
import "google/api/annotations.proto";

That will generate something like:

import * as google_api_annotations_pb from 'google/api/annotations_pb';

create-share avatar Jan 15 '20 10:01 create-share

I think we need an import option js_package (like go_package for go) could use the npm package to override the relative path.

For example:

// in google/api/annotations.proto
option js_package = "google/api/annotations_pb";
// in example.proto
import "google/api/annotations.proto";

That will generate something like:

import * as google_api_annotations_pb from 'google/api/annotations_pb';

This would be awesome!

aberasarte avatar Jan 15 '20 10:01 aberasarte

While we don't have this sorted, I am handling this issue with a simple bash function https://github.com/apssouza22/modern-api-management/blob/master/bin/js-gen.sh#L105

apssouza22 avatar Apr 11 '22 18:04 apssouza22

This is something we should think about supporting.

dibenede avatar Sep 16 '22 22:09 dibenede