Skip to content

Commit

Permalink
fix: add .js suffix to proto cross-reference imports
Browse files Browse the repository at this point in the history
When importing a cross-referenced file, a line like this is generated:

import { MyMessage } from '../myother/myother'

.. assuming there's a proto at ../myother/myother.proto

But if we add the suffix '.pb.ts' to the generated files:

import { MyMessage } from '../myother/myother.pb'

Is not recognized as a TypeScript import by tsc because of the .pb suffix.

To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file:

import { MyMessage } from '../myother/myother.pb.js'

Fixes stephenh#601

Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
paralin committed Jun 29, 2022
1 parent 723b528 commit 5fb9994
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,6 @@ export function impProto(options: Options, module: string, type: string): Import
if (options.onlyTypes) {
return imp('t:' + importString);
} else {
return imp(importString);
return imp(importString + '.js');
}
}
4 changes: 2 additions & 2 deletions tests/types-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ describe('types', () => {
descr: 'top-level messages',
typeMap: new Map([['.namespace.Message', ['namespace', 'Message', fakeProto]]]),
protoType: '.namespace.Message',
expected: code`${imp('Message@./namespace')}`,
expected: code`${imp('Message@./namespace.js')}`,
},
{
descr: 'nested messages',
typeMap: new Map([['.namespace.Message.Inner', ['namespace', 'Message_Inner', fakeProto]]]),
protoType: '.namespace.Message.Inner',
expected: code`${imp('Message_Inner@./namespace')}`,
expected: code`${imp('Message_Inner@./namespace.js')}`,
},
{
descr: 'value types',
Expand Down

0 comments on commit 5fb9994

Please sign in to comment.