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

Tips for packaging the generated code as a npm package ? #522

Closed
alexisvisco opened this issue Feb 25, 2022 · 3 comments
Closed

Tips for packaging the generated code as a npm package ? #522

alexisvisco opened this issue Feb 25, 2022 · 3 comments

Comments

@alexisvisco
Copy link

alexisvisco commented Feb 25, 2022

I am more experienced with go, but our frontend team would like to benefit from the protobuf code generation to import types.

So I give a try to the best typescript proto generator after trying many of them (your is the best IMO).

The generated output look like this:

gen/ts
├── google
│   ├── api
│   │   ├── annotations.ts
│   │   ├── field_behavior.ts
│   │   └── http.ts
│   └── protobuf
│       ├── any.ts
│       ├── descriptor.ts
│       └── struct.ts
├── miimosa
│   ├── authentication
│   │   └── v1
│   │       └── authentication.ts
│   ├── common
│   │   └── v1
│   │       ├── geopoint.ts
│   │       ├── health.ts
│   │       ├── project_state.ts
│   │       ├── project_type.ts
│   │       └── term_frequency.ts
│   ├── dummy
│   │   └── v1
│   │       └── service_dummy.ts
│   ├── loans
│   │   └── v1
│   │       └── loans.ts
│   ├── salesforce
│   │   └── v1
│   │       └── salesforce.ts
│   ├── search
│   │   └── v1
│   │       ├── search.ts
│   │       └── sync_search.ts
│   ├── stats
│   │   └── v1
│   │       └── stats.ts
│   └── v1
│       └── openapiv2.ts
└── protoc-gen-openapiv2
    └── options
        ├── annotations.ts
        └── openapiv2.ts

I saw that the convenient way to publish a library is to have a entry point, an index.js or ts file.

I ask the question to people with more experience than me in the field of publishing libs to npm, especially typescript libs : how can you wrap this files to be use conveniently with types ?

I think the desired output for a lib is an dist/index.js and dist/index.d.ts, how can I got this output ?

@stephenh
Copy link
Owner

Hey @alexisvisco , yeah this is a feature request in #212. I've been procrastinating jump in to it b/c surely at some point symbols are going to conflict (i.e. FooMessage is declared in both file1.proto and file2.proto).

But, dunno, should probably just try an MVP version of it anyway, and see happens.

I might work on this a little bit just to see how good/bad it goes, but no promises, and would be great if you wanted to pick up #212 directly.

(For now closing this one as a dup of 212. Thanks!)

@alexisvisco
Copy link
Author

I created a Go script that basically traverse the directory structure and write export * as ServiceName from 'directory path'

The problem is when you have multiples files in a package.

When there is multiple files, I cannot do an export * as ServiceName from 'directory path' because there is multiple files to merge into a single module, so I created an other file with the name of the service next to the index.ts file.

For instance the leads service has 3 files generated. In the index.ts I am writing export * as Leads from './leads'
And in the leads.ts (next to the index.ts) I just write for each file an `export * from 'filepath'

In theory this should works fine but your generation is giving me a hard time.

There is in each files for a package the same exports :

  • protobufPackage
  • DeepPartial
  • Exact

When doing an export * from 'filepath' there is collision because typescript can't merge them.

@danem
Copy link

danem commented Apr 14, 2022

There is in each files for a package the same exports :

  • protobufPackage
  • DeepPartial
  • Exact

When doing an export * from 'filepath' there is collision because typescript can't merge them.

Setting --ts_proto_opt=exportCommonSymbols=false fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants