Skip to content

Generalize npm dependency support for generated js code #47

@clehene

Description

@clehene

Sister issue of grpc/grpc-web#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions