Skip to content

Commit

Permalink
Update ES6 imports with comment about corresponding proto import path.
Browse files Browse the repository at this point in the history
I was experiencing a long import path in the generated code that doesn't
work. Outputting the proto file that corresponds to a JS import is helpful for
both diagnosing the issue and writing a wrapper for altering the import path to
my satisfaction.

Before:

```js
import * as github_com_gonzojive_rules_ts_proto_example_prefix_greeting_pb from '../../../../../github.com/gonzojive/rules_ts_proto/example/prefix/greeting_pb';
```

After:

```js
import * as github_com_gonzojive_rules_ts_proto_example_prefix_greeting_pb from '../../../../../github.com/gonzojive/rules_ts_proto/example/prefix/greeting_pb'; // proto import: "github.com/gonzojive/rules_ts_proto/example/prefix/greeting.proto"
```
  • Loading branch information
reddaly committed Jan 9, 2023
1 parent e493898 commit c137e11
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions javascript/net/grpc/web/generator/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,18 @@ void PrintES6Imports(Printer* printer, const FileDescriptor* file) {

std::set<string> imports;
for (const auto& entry : GetAllMessages(file)) {
const string& name = entry.second->file()->name();
const string& proto_filename = entry.second->file()->name();
string dep_filename = GetRootPath(file->name(), name) + StripProto(name);
if (imports.find(dep_filename) != imports.end()) {
continue;
}
imports.insert(dep_filename);
// We need to give each cross-file import an alias.
printer->Print("import * as $alias$ from '$dep_filename$_pb';\n", "alias",
ModuleAlias(name), "dep_filename", dep_filename);
printer->Print("import * as $alias$ from '$dep_filename$_pb'; // proto import: \"$proto_filename$\"\n",
"alias", ModuleAlias(name),
"dep_filename", dep_filename,
"proto_filename", proto_filename);
}
}
printer->Print("\n\n");
}
Expand Down

0 comments on commit c137e11

Please sign in to comment.