Skip to content

Commit

Permalink
feat: add importSuffix option and remove default .js suffix (#612)
Browse files Browse the repository at this point in the history
Adds a new option: importSuffix: which enables setting the suffix used to import
.ts files directly.

The default is to import with no suffix, with foo.ts:

import './foo';

With "importSuffix=.js"

import './foo.js';

Fixes compatibility with TypeScript < 4.7.x.

Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
paralin committed Jul 5, 2022
1 parent 92143df commit 63a8895
Show file tree
Hide file tree
Showing 54 changed files with 574 additions and 78 deletions.
2 changes: 2 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=fileSuffix=<SUFFIX>`, ts-proto will emit generated files using the specified suffix. A `helloworld.proto` file with `fileSuffix=.pb` would be generated as `helloworld.pb.ts`. This is common behavior in other protoc plugins and provides a way to quickly glob all the generated files.

- With `--ts_proto_opt=importSuffix=<SUFFIX>`, ts-proto will emit file imports using the specified suffix. An import of `helloworld.ts` with `fileSuffix=.js` would generate `import "helloworld.js"`. The default is to import without a file extension. Supported by TypeScript 4.7.x and up.

- With `--ts_proto_opt=enumsAsLiterals=true`, the generated enum types will be enum-ish object with `as const`.

- With `--ts_proto_opt=useExactTypes=false`, the generated `fromPartial` method will not use Exact types.
Expand Down
2 changes: 1 addition & 1 deletion integration/avoid-import-conflicts-types-only/simple.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
import type { SimpleEnum as SimpleEnum1, Simple as Simple2 } from './simple2.js';
import type { SimpleEnum as SimpleEnum1, Simple as Simple2 } from './simple2';

export const protobufPackage = 'simple';

Expand Down
2 changes: 1 addition & 1 deletion integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Simple as Simple2,
simpleEnumFromJSON as simpleEnumFromJSON3,
simpleEnumToJSON as simpleEnumToJSON4,
} from './simple2.js';
} from './simple2';
import * as _m0 from 'protobufjs/minimal';

export const protobufPackage = 'simple';
Expand Down
2 changes: 1 addition & 1 deletion integration/barrel-imports/foo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
import { Bar } from './bar.js';
import { Bar } from './bar';
import * as _m0 from 'protobufjs/minimal';

export interface Foo {
Expand Down
2 changes: 1 addition & 1 deletion integration/bytes-node/point.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */
import * as _m0 from 'protobufjs/minimal';
import { BytesValue } from './google/protobuf/wrappers.js';
import { BytesValue } from './google/protobuf/wrappers';

export const protobufPackage = '';

Expand Down
2 changes: 1 addition & 1 deletion integration/fieldmask/fieldmask.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */
import * as _m0 from 'protobufjs/minimal';
import { FieldMask } from './google/protobuf/field_mask.js';
import { FieldMask } from './google/protobuf/field_mask';

export const protobufPackage = '';

Expand Down
4 changes: 2 additions & 2 deletions integration/file-suffix/parent.pb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */
import { ChildEnum, Child, childEnumFromJSON, childEnumToJSON } from './child.pb.js';
import { Timestamp } from './google/protobuf/timestamp.pb.js';
import { ChildEnum, Child, childEnumFromJSON, childEnumToJSON } from './child.pb';
import { Timestamp } from './google/protobuf/timestamp.pb';
import * as _m0 from 'protobufjs/minimal';

export const protobufPackage = 'file_suffix';
Expand Down
8 changes: 4 additions & 4 deletions integration/grpc-js/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
ClientDuplexStream,
ServiceError,
} from '@grpc/grpc-js';
import { Timestamp } from './google/protobuf/timestamp.js';
import { Empty } from './google/protobuf/empty.js';
import { Timestamp } from './google/protobuf/timestamp';
import { Empty } from './google/protobuf/empty';
import {
StringValue,
Int64Value,
Expand All @@ -29,8 +29,8 @@ import {
FloatValue,
DoubleValue,
BoolValue,
} from './google/protobuf/wrappers.js';
import { Struct, ListValue, Value } from './google/protobuf/struct.js';
} from './google/protobuf/wrappers';
import { Struct, ListValue, Value } from './google/protobuf/struct';
import * as _m0 from 'protobufjs/minimal';

export const protobufPackage = 'simple';
Expand Down
Binary file added integration/import-suffix/child.bin
Binary file not shown.
111 changes: 111 additions & 0 deletions integration/import-suffix/child.pb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* eslint-disable */
import * as _m0 from 'protobufjs/minimal';

export const protobufPackage = 'file_suffix';

export enum ChildEnum {
DEFAULT = 0,
FOO = 1,
UNRECOGNIZED = -1,
}

export function childEnumFromJSON(object: any): ChildEnum {
switch (object) {
case 0:
case 'DEFAULT':
return ChildEnum.DEFAULT;
case 1:
case 'FOO':
return ChildEnum.FOO;
case -1:
case 'UNRECOGNIZED':
default:
return ChildEnum.UNRECOGNIZED;
}
}

export function childEnumToJSON(object: ChildEnum): string {
switch (object) {
case ChildEnum.DEFAULT:
return 'DEFAULT';
case ChildEnum.FOO:
return 'FOO';
case ChildEnum.UNRECOGNIZED:
default:
return 'UNRECOGNIZED';
}
}

export interface Child {
name: string;
}

function createBaseChild(): Child {
return { name: '' };
}

export const Child = {
encode(message: Child, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.name !== '') {
writer.uint32(10).string(message.name);
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Child {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseChild();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.name = reader.string();
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): Child {
return {
name: isSet(object.name) ? String(object.name) : '',
};
},

toJSON(message: Child): unknown {
const obj: any = {};
message.name !== undefined && (obj.name = message.name);
return obj;
},

fromPartial<I extends Exact<DeepPartial<Child>, I>>(object: I): Child {
const message = createBaseChild();
message.name = object.name ?? '';
return message;
},
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin
? T
: T extends Array<infer U>
? Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin
? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & Record<Exclude<keyof I, KeysOfUnion<P>>, never>;

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
12 changes: 12 additions & 0 deletions integration/import-suffix/child.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package file_suffix;

message Child {
string name = 1;
}

enum ChildEnum {
DEFAULT = 0;
FOO = 1;
}

0 comments on commit 63a8895

Please sign in to comment.