@bufbuild/protoc-gen-es 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,14 +1,7 @@
1
1
  # @bufbuild/protoc-gen-es
2
2
 
3
- This package provides the code generator plugin `protoc-gen-es`. The code it
4
- generates depends on [@bufbuild/protobuf](https://www.npmjs.com/package/@bufbuild/protobuf).
5
-
6
- ## Protocol Buffers for ECMAScript
7
-
8
- A complete implementation of [Protocol Buffers](https://developers.google.com/protocol-buffers)
9
- in TypeScript, suitable for web browsers and Node.js.
10
-
11
- For example, the following definition:
3
+ The code generator for Protocol Buffers for ECMAScript. For example, the
4
+ following definition:
12
5
 
13
6
  ```protobuf
14
7
  message Person {
@@ -31,45 +24,92 @@ pete = Person.fromBinary(bytes);
31
24
  pete = Person.fromJsonString('{"name": "pete", "id": 123}');
32
25
  ```
33
26
 
34
- Learn more at [github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es).
27
+ Learn more about the project at [github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es).
35
28
 
36
29
 
37
30
  ## Installation
38
31
 
39
- ### With npm
32
+ `protoc-gen-es` is a code generator plugin for Protocol Buffer compilers,
33
+ like [buf](https://github.com/bufbuild/buf) and [protoc](https://github.com/protocolbuffers/protobuf/releases).
34
+ It generates base types - messages and enumerations - from your Protocol Buffer
35
+ schema. The generated code requires the runtime library [@bufbuild/protobuf](https://www.npmjs.com/package/@bufbuild/protobuf).
36
+
37
+ To install the plugin and the runtime library, run:
40
38
 
41
39
  ```shell
42
- npm install @bufbuild/protoc-gen-es
40
+ npm install --save-dev @bufbuild/protoc-gen-es
41
+ npm install @bufbuild/protobuf
43
42
  ```
44
43
 
45
- This will install the code generator plugin in `node_modules/.bin/protoc-gen-es`.
46
- Note that npm does not add the executable to your `$PATH`. You can do so with:
44
+ We use peer dependencies to ensure that code generator and runtime library are
45
+ compatible with each other. Note that yarn and pnpm only emit a warning in this case.
47
46
 
48
- ```shell
49
- PATH=$PATH:$(pwd)/node_modules/.bin
47
+
48
+ ## Generating code
49
+
50
+ ### With buf
51
+
52
+ Add a new configuration file `buf.gen.yaml`:
53
+
54
+ ```yaml
55
+ # buf.gen.yaml defines a local generation template.
56
+ # For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml
57
+ version: v1
58
+ plugins:
59
+ # This will invoke protoc-gen-es and write output to src/gen
60
+ - name: es
61
+ out: src/gen
62
+ opt: target=ts
50
63
  ```
51
64
 
52
- ### With yarn
65
+ Add the following script to your `package.json`:
53
66
 
54
- ```shell
55
- yarn add @bufbuild/protoc-gen-es
67
+ ```json
68
+ {
69
+ "name": "your-package",
70
+ "version": "1.0.0",
71
+ "scripts": {
72
+ "generate": "buf generate"
73
+ },
74
+ // ...
75
+ }
56
76
  ```
57
77
 
58
- Note that yarn v2 does not use a `node_modules` directory anymore. To find the path
59
- where yarn stores the executable, run `yarn bin protoc-gen-es` (it is "unplugged"
60
- automatically).
78
+ To generate code for all protobuf files within your project, simply run:
61
79
 
62
- You can always confirm successful installation with:
63
- ```shell
64
- protoc-gen-es --version
80
+ ```bash
81
+ npm run generate
82
+ ```
83
+
84
+ Note that `buf` can generate from various [inputs](https://docs.buf.build/reference/inputs),
85
+ not just local protobuf files.
86
+
87
+
88
+ ### With protoc
89
+
90
+ ```bash
91
+ PATH=$PATH:$(pwd)/node_modules/.bin \
92
+ protoc -I . \
93
+ --es_out src/gen \
94
+ --es_opt target=ts \
95
+ a.proto b.proto c.proto
65
96
  ```
66
97
 
98
+ Note that we are adding `node_modules/.bin` to the `$PATH`, so that the protocol
99
+ buffer compiler can find them. This happens automatically with npm scripts.
100
+
101
+ Since yarn v2 and above does not use a `node_modules` directory, you need to
102
+ change the variable a bit:
103
+
104
+ ```bash
105
+ PATH=$(dirname $(yarn bin protoc-gen-es)):$PATH
106
+ ```
67
107
 
68
108
  ## Plugin options
69
109
 
70
110
  ### `target`
71
111
 
72
- This option controls whether the plugin generates JavaScript, TypeScript,
112
+ This option controls whether the plugin generates JavaScript, TypeScript,
73
113
  or TypeScript declaration files.
74
114
 
75
115
  Possible values:
@@ -84,3 +124,11 @@ By default, we generate JavaScript and TypeScript declaration files, which
84
124
  produces the smallest code size. If you prefer to generate TypeScript, use
85
125
  `target=ts`.
86
126
 
127
+ ### `keep_empty_files=true`
128
+
129
+ By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
130
+ (and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin))
131
+ omit empty files from the plugin output. This option disables pruning of
132
+ empty files, to allow for smooth interoperation with Bazel and similar
133
+ tooling that requires all output files to be declared ahead of time.
134
+ Unless you use Bazel, it is very unlikely that you need this option.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.1.0"
23
+ "@bufbuild/protoplugin": "0.2.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.1.0"
26
+ "@bufbuild/protobuf": "0.2.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -13,15 +13,23 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.declaration = void 0;
16
+ exports.generateDts = void 0;
17
17
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
18
18
  const match_wkt_js_1 = require("./match-wkt.js");
19
- exports.declaration = {
20
- target: "dts",
21
- extension: "_pb.d.ts",
22
- generateEnum,
23
- generateMessage,
24
- };
19
+ function generateDts(schema) {
20
+ for (const file of schema.files) {
21
+ const f = schema.generateFile(file.name + "_pb.d.ts");
22
+ f.preamble(file);
23
+ for (const enumeration of file.enums) {
24
+ generateEnum(schema, f, enumeration);
25
+ }
26
+ for (const message of file.messages) {
27
+ generateMessage(schema, f, message);
28
+ }
29
+ // We do not generate anything for services, and we do not support extensions at this time
30
+ }
31
+ }
32
+ exports.generateDts = generateDts;
25
33
  // prettier-ignore
26
34
  function generateEnum(schema, f, enumeration) {
27
35
  f.print((0, ecmascript_1.makeJsDoc)(enumeration));
@@ -13,16 +13,24 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.generateFieldInfo = exports.javascript = void 0;
16
+ exports.generateFieldInfo = exports.generateJs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
19
  const match_wkt_js_1 = require("./match-wkt.js");
20
- exports.javascript = {
21
- target: "js",
22
- extension: "_pb.js",
23
- generateEnum,
24
- generateMessage,
25
- };
20
+ function generateJs(schema) {
21
+ for (const file of schema.files) {
22
+ const f = schema.generateFile(file.name + "_pb.js");
23
+ f.preamble(file);
24
+ for (const enumeration of file.enums) {
25
+ generateEnum(schema, f, enumeration);
26
+ }
27
+ for (const message of file.messages) {
28
+ generateMessage(schema, f, message);
29
+ }
30
+ // We do not generate anything for services, and we do not support extensions at this time
31
+ }
32
+ }
33
+ exports.generateJs = generateJs;
26
34
  // prettier-ignore
27
35
  function generateEnum(schema, f, enumeration) {
28
36
  const protoN = schema.runtime[enumeration.file.syntax];
@@ -88,11 +96,11 @@ function generateFieldInfo(schema, f, field) {
88
96
  if (field.jsonName !== undefined) {
89
97
  e.push(`jsonName: "`, field.jsonName, `", `);
90
98
  }
91
- switch (field.kind) {
92
- case "scalar_field":
99
+ switch (field.fieldKind) {
100
+ case "scalar":
93
101
  e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
94
102
  break;
95
- case "map_field":
103
+ case "map":
96
104
  e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
97
105
  switch (field.mapValue.kind) {
98
106
  case "scalar":
@@ -106,10 +114,10 @@ function generateFieldInfo(schema, f, field) {
106
114
  break;
107
115
  }
108
116
  break;
109
- case "message_field":
117
+ case "message":
110
118
  e.push(`kind: "message", T: `, field.message, `, `);
111
119
  break;
112
- case "enum_field":
120
+ case "enum":
113
121
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
114
122
  break;
115
123
  }
@@ -19,10 +19,10 @@ function matchWkt(message) {
19
19
  switch (message.typeName) {
20
20
  case "google.protobuf.Any": {
21
21
  const typeUrl = message.fields.find((f) => f.number == 1 &&
22
- f.kind == "scalar_field" &&
22
+ f.fieldKind == "scalar" &&
23
23
  f.scalar === protobuf_1.ScalarType.STRING);
24
24
  const value = message.fields.find((f) => f.number == 2 &&
25
- f.kind == "scalar_field" &&
25
+ f.fieldKind == "scalar" &&
26
26
  f.scalar === protobuf_1.ScalarType.BYTES);
27
27
  if (typeUrl && value) {
28
28
  return {
@@ -35,10 +35,10 @@ function matchWkt(message) {
35
35
  }
36
36
  case "google.protobuf.Timestamp": {
37
37
  const seconds = message.fields.find((f) => f.number == 1 &&
38
- f.kind == "scalar_field" &&
38
+ f.fieldKind == "scalar" &&
39
39
  f.scalar === protobuf_1.ScalarType.INT64);
40
40
  const nanos = message.fields.find((f) => f.number == 2 &&
41
- f.kind == "scalar_field" &&
41
+ f.fieldKind == "scalar" &&
42
42
  f.scalar === protobuf_1.ScalarType.INT32);
43
43
  if (seconds && nanos) {
44
44
  return {
@@ -51,10 +51,10 @@ function matchWkt(message) {
51
51
  }
52
52
  case "google.protobuf.Duration": {
53
53
  const seconds = message.fields.find((f) => f.number == 1 &&
54
- f.kind == "scalar_field" &&
54
+ f.fieldKind == "scalar" &&
55
55
  f.scalar === protobuf_1.ScalarType.INT64);
56
56
  const nanos = message.fields.find((f) => f.number == 2 &&
57
- f.kind == "scalar_field" &&
57
+ f.fieldKind == "scalar" &&
58
58
  f.scalar === protobuf_1.ScalarType.INT32);
59
59
  if (seconds && nanos) {
60
60
  return {
@@ -67,7 +67,7 @@ function matchWkt(message) {
67
67
  }
68
68
  case "google.protobuf.Struct": {
69
69
  const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
70
- if ((fields === null || fields === void 0 ? void 0 : fields.kind) !== "map_field" ||
70
+ if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
71
71
  fields.mapValue.kind !== "message" ||
72
72
  fields.mapValue.message.typeName !== "google.protobuf.Value") {
73
73
  break;
@@ -77,29 +77,29 @@ function matchWkt(message) {
77
77
  case "google.protobuf.Value": {
78
78
  const kind = message.oneofs.find((o) => o.name === "kind");
79
79
  const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
80
- if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.kind) !== "enum_field" ||
80
+ if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
81
81
  nullValue.enum.typeName !== "google.protobuf.NullValue") {
82
82
  return undefined;
83
83
  }
84
84
  const numberValue = message.fields.find((f) => f.number == 2 &&
85
- f.kind == "scalar_field" &&
85
+ f.fieldKind == "scalar" &&
86
86
  f.scalar === protobuf_1.ScalarType.DOUBLE &&
87
87
  f.oneof === kind);
88
88
  const stringValue = message.fields.find((f) => f.number == 3 &&
89
- f.kind == "scalar_field" &&
89
+ f.fieldKind == "scalar" &&
90
90
  f.scalar === protobuf_1.ScalarType.STRING &&
91
91
  f.oneof === kind);
92
92
  const boolValue = message.fields.find((f) => f.number == 4 &&
93
- f.kind == "scalar_field" &&
93
+ f.fieldKind == "scalar" &&
94
94
  f.scalar === protobuf_1.ScalarType.BOOL &&
95
95
  f.oneof === kind);
96
96
  const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
97
- if ((structValue === null || structValue === void 0 ? void 0 : structValue.kind) !== "message_field" ||
97
+ if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
98
98
  structValue.message.typeName !== "google.protobuf.Struct") {
99
99
  return undefined;
100
100
  }
101
101
  const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
102
- if ((listValue === null || listValue === void 0 ? void 0 : listValue.kind) !== "message_field" ||
102
+ if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
103
103
  listValue.message.typeName !== "google.protobuf.ListValue") {
104
104
  return undefined;
105
105
  }
@@ -119,7 +119,7 @@ function matchWkt(message) {
119
119
  }
120
120
  case "google.protobuf.ListValue": {
121
121
  const values = message.fields.find((f) => f.number == 1 && f.repeated);
122
- if ((values === null || values === void 0 ? void 0 : values.kind) != "message_field" ||
122
+ if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
123
123
  values.message.typeName !== "google.protobuf.Value") {
124
124
  break;
125
125
  }
@@ -127,7 +127,7 @@ function matchWkt(message) {
127
127
  }
128
128
  case "google.protobuf.FieldMask": {
129
129
  const paths = message.fields.find((f) => f.number == 1 &&
130
- f.kind == "scalar_field" &&
130
+ f.fieldKind == "scalar" &&
131
131
  f.scalar === protobuf_1.ScalarType.STRING &&
132
132
  f.repeated);
133
133
  if (paths) {
@@ -148,7 +148,7 @@ function matchWkt(message) {
148
148
  if (!value) {
149
149
  break;
150
150
  }
151
- if (value.kind !== "scalar_field") {
151
+ if (value.fieldKind !== "scalar") {
152
152
  break;
153
153
  }
154
154
  return { typeName: message.typeName, value };
@@ -17,24 +17,12 @@ exports.protocGenEs = void 0;
17
17
  const protoplugin_1 = require("@bufbuild/protoplugin");
18
18
  const typescript_js_1 = require("./typescript.js");
19
19
  const javascript_js_1 = require("./javascript.js");
20
- const declaration_1 = require("./declaration");
20
+ const declaration_js_1 = require("./declaration.js");
21
21
  const package_json_1 = require("../package.json");
22
22
  exports.protocGenEs = (0, protoplugin_1.createEcmaScriptPlugin)({
23
23
  name: "protoc-gen-es",
24
24
  version: `v${String(package_json_1.version)}`,
25
- }, (schema) => {
26
- const targets = [typescript_js_1.typescript, javascript_js_1.javascript, declaration_1.declaration].filter((gen) => schema.targets.includes(gen.target));
27
- for (const target of targets) {
28
- for (const file of schema.files) {
29
- const f = schema.generateFile(file.name + target.extension);
30
- f.preamble(file);
31
- for (const enumeration of file.enums) {
32
- target.generateEnum(schema, f, enumeration);
33
- }
34
- for (const message of file.messages) {
35
- target.generateMessage(schema, f, message);
36
- }
37
- // We do not generate anything for services, and we do not support extensions at this time
38
- }
39
- }
25
+ generateTs: typescript_js_1.generateTs,
26
+ generateJs: javascript_js_1.generateJs,
27
+ generateDts: declaration_js_1.generateDts,
40
28
  });
@@ -13,18 +13,26 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.typescript = void 0;
16
+ exports.generateTs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
19
  const match_wkt_js_1 = require("./match-wkt.js");
20
20
  const javascript_js_1 = require("./javascript.js");
21
21
  const ecmascript_2 = require("@bufbuild/protoplugin/ecmascript");
22
- exports.typescript = {
23
- target: "ts",
24
- extension: "_pb.ts",
25
- generateEnum,
26
- generateMessage,
27
- };
22
+ function generateTs(schema) {
23
+ for (const file of schema.files) {
24
+ const f = schema.generateFile(file.name + "_pb.ts");
25
+ f.preamble(file);
26
+ for (const enumeration of file.enums) {
27
+ generateEnum(schema, f, enumeration);
28
+ }
29
+ for (const message of file.messages) {
30
+ generateMessage(schema, f, message);
31
+ }
32
+ // We do not generate anything for services, and we do not support extensions at this time
33
+ }
34
+ }
35
+ exports.generateTs = generateTs;
28
36
  // prettier-ignore
29
37
  function generateEnum(schema, f, enumeration) {
30
38
  const protoN = schema.runtime[enumeration.file.syntax];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.1.0"
23
+ "@bufbuild/protoplugin": "0.2.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.1.0"
26
+ "@bufbuild/protobuf": "0.2.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -18,7 +18,11 @@ import type {
18
18
  DescMessage,
19
19
  DescOneof,
20
20
  } from "@bufbuild/protobuf";
21
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
21
+ import type {
22
+ GeneratedFile,
23
+ Printable,
24
+ Schema,
25
+ } from "@bufbuild/protoplugin/ecmascript";
22
26
  import {
23
27
  getFieldTyping,
24
28
  literalString,
@@ -27,12 +31,19 @@ import {
27
31
  } from "@bufbuild/protoplugin/ecmascript";
28
32
  import { matchWkt } from "./match-wkt.js";
29
33
 
30
- export const declaration = {
31
- target: "dts",
32
- extension: "_pb.d.ts",
33
- generateEnum,
34
- generateMessage,
35
- } as const;
34
+ export function generateDts(schema: Schema) {
35
+ for (const file of schema.files) {
36
+ const f = schema.generateFile(file.name + "_pb.d.ts");
37
+ f.preamble(file);
38
+ for (const enumeration of file.enums) {
39
+ generateEnum(schema, f, enumeration);
40
+ }
41
+ for (const message of file.messages) {
42
+ generateMessage(schema, f, message);
43
+ }
44
+ // We do not generate anything for services, and we do not support extensions at this time
45
+ }
46
+ }
36
47
 
37
48
  // prettier-ignore
38
49
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -120,7 +131,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
120
131
 
121
132
  function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
122
133
  f.print(makeJsDoc(field, " "));
123
- const e: Parameters<typeof f.print> = [];
134
+ const e: Printable = [];
124
135
  e.push(" ", localName(field));
125
136
  const { typing, optional } = getFieldTyping(field, f);
126
137
  if (optional) {
package/src/javascript.ts CHANGED
@@ -14,7 +14,11 @@
14
14
 
15
15
  import type { DescEnum, DescField, DescMessage } from "@bufbuild/protobuf";
16
16
  import { proto2, proto3, ScalarType } from "@bufbuild/protobuf";
17
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
17
+ import type {
18
+ GeneratedFile,
19
+ Printable,
20
+ Schema,
21
+ } from "@bufbuild/protoplugin/ecmascript";
18
22
  import {
19
23
  getFieldExplicitDefaultValue,
20
24
  literalString,
@@ -23,12 +27,19 @@ import {
23
27
  } from "@bufbuild/protoplugin/ecmascript";
24
28
  import { matchWkt } from "./match-wkt.js";
25
29
 
26
- export const javascript = {
27
- target: "js",
28
- extension: "_pb.js",
29
- generateEnum,
30
- generateMessage,
31
- } as const;
30
+ export function generateJs(schema: Schema) {
31
+ for (const file of schema.files) {
32
+ const f = schema.generateFile(file.name + "_pb.js");
33
+ f.preamble(file);
34
+ for (const enumeration of file.enums) {
35
+ generateEnum(schema, f, enumeration);
36
+ }
37
+ for (const message of file.messages) {
38
+ generateMessage(schema, f, message);
39
+ }
40
+ // We do not generate anything for services, and we do not support extensions at this time
41
+ }
42
+ }
32
43
 
33
44
  // prettier-ignore
34
45
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -90,16 +101,16 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
90
101
  // prettier-ignore
91
102
  export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescField) {
92
103
  const protoN = schema.runtime[field.parent.file.syntax];
93
- const e: Parameters<typeof f.print> = [];
104
+ const e: Printable = [];
94
105
  e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
95
106
  if (field.jsonName !== undefined) {
96
107
  e.push(`jsonName: "`, field.jsonName, `", `);
97
108
  }
98
- switch (field.kind) {
99
- case "scalar_field":
109
+ switch (field.fieldKind) {
110
+ case "scalar":
100
111
  e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, ScalarType[field.scalar], ` */, `);
101
112
  break;
102
- case "map_field":
113
+ case "map":
103
114
  e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, ScalarType[field.mapKey], ` */, `);
104
115
  switch (field.mapValue.kind) {
105
116
  case "scalar":
@@ -113,10 +124,10 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
113
124
  break;
114
125
  }
115
126
  break;
116
- case "message_field":
127
+ case "message":
117
128
  e.push(`kind: "message", T: `, field.message, `, `);
118
129
  break;
119
- case "enum_field":
130
+ case "enum":
120
131
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
121
132
  break;
122
133
  }
package/src/match-wkt.ts CHANGED
@@ -37,21 +37,21 @@ type DescWkt =
37
37
  }
38
38
  | {
39
39
  typeName: "google.protobuf.Struct";
40
- fields: DescField & { kind: "map_field" };
40
+ fields: DescField & { fieldKind: "map" };
41
41
  }
42
42
  | {
43
43
  typeName: "google.protobuf.Value";
44
44
  kind: DescOneof;
45
- nullValue: DescField & { kind: "enum_field" };
45
+ nullValue: DescField & { fieldKind: "enum" };
46
46
  numberValue: DescField;
47
47
  stringValue: DescField;
48
48
  boolValue: DescField;
49
- structValue: DescField & { kind: "message_field" };
50
- listValue: DescField & { kind: "message_field" };
49
+ structValue: DescField & { fieldKind: "message" };
50
+ listValue: DescField & { fieldKind: "message" };
51
51
  }
52
52
  | {
53
53
  typeName: "google.protobuf.ListValue";
54
- values: DescField & { kind: "message_field" };
54
+ values: DescField & { fieldKind: "message" };
55
55
  }
56
56
  | {
57
57
  typeName: "google.protobuf.FieldMask";
@@ -59,39 +59,39 @@ type DescWkt =
59
59
  }
60
60
  | {
61
61
  typeName: "google.protobuf.DoubleValue";
62
- value: DescField & { kind: "scalar_field" };
62
+ value: DescField & { fieldKind: "scalar" };
63
63
  }
64
64
  | {
65
65
  typeName: "google.protobuf.FloatValue";
66
- value: DescField & { kind: "scalar_field" };
66
+ value: DescField & { fieldKind: "scalar" };
67
67
  }
68
68
  | {
69
69
  typeName: "google.protobuf.Int64Value";
70
- value: DescField & { kind: "scalar_field" };
70
+ value: DescField & { fieldKind: "scalar" };
71
71
  }
72
72
  | {
73
73
  typeName: "google.protobuf.UInt64Value";
74
- value: DescField & { kind: "scalar_field" };
74
+ value: DescField & { fieldKind: "scalar" };
75
75
  }
76
76
  | {
77
77
  typeName: "google.protobuf.Int32Value";
78
- value: DescField & { kind: "scalar_field" };
78
+ value: DescField & { fieldKind: "scalar" };
79
79
  }
80
80
  | {
81
81
  typeName: "google.protobuf.UInt32Value";
82
- value: DescField & { kind: "scalar_field" };
82
+ value: DescField & { fieldKind: "scalar" };
83
83
  }
84
84
  | {
85
85
  typeName: "google.protobuf.BoolValue";
86
- value: DescField & { kind: "scalar_field" };
86
+ value: DescField & { fieldKind: "scalar" };
87
87
  }
88
88
  | {
89
89
  typeName: "google.protobuf.StringValue";
90
- value: DescField & { kind: "scalar_field" };
90
+ value: DescField & { fieldKind: "scalar" };
91
91
  }
92
92
  | {
93
93
  typeName: "google.protobuf.BytesValue";
94
- value: DescField & { kind: "scalar_field" };
94
+ value: DescField & { fieldKind: "scalar" };
95
95
  };
96
96
 
97
97
  export function matchWkt(message: DescMessage): DescWkt | undefined {
@@ -100,13 +100,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
100
100
  const typeUrl = message.fields.find(
101
101
  (f) =>
102
102
  f.number == 1 &&
103
- f.kind == "scalar_field" &&
103
+ f.fieldKind == "scalar" &&
104
104
  f.scalar === ScalarType.STRING
105
105
  );
106
106
  const value = message.fields.find(
107
107
  (f) =>
108
108
  f.number == 2 &&
109
- f.kind == "scalar_field" &&
109
+ f.fieldKind == "scalar" &&
110
110
  f.scalar === ScalarType.BYTES
111
111
  );
112
112
  if (typeUrl && value) {
@@ -122,13 +122,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
122
122
  const seconds = message.fields.find(
123
123
  (f) =>
124
124
  f.number == 1 &&
125
- f.kind == "scalar_field" &&
125
+ f.fieldKind == "scalar" &&
126
126
  f.scalar === ScalarType.INT64
127
127
  );
128
128
  const nanos = message.fields.find(
129
129
  (f) =>
130
130
  f.number == 2 &&
131
- f.kind == "scalar_field" &&
131
+ f.fieldKind == "scalar" &&
132
132
  f.scalar === ScalarType.INT32
133
133
  );
134
134
  if (seconds && nanos) {
@@ -144,13 +144,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
144
144
  const seconds = message.fields.find(
145
145
  (f) =>
146
146
  f.number == 1 &&
147
- f.kind == "scalar_field" &&
147
+ f.fieldKind == "scalar" &&
148
148
  f.scalar === ScalarType.INT64
149
149
  );
150
150
  const nanos = message.fields.find(
151
151
  (f) =>
152
152
  f.number == 2 &&
153
- f.kind == "scalar_field" &&
153
+ f.fieldKind == "scalar" &&
154
154
  f.scalar === ScalarType.INT32
155
155
  );
156
156
  if (seconds && nanos) {
@@ -165,7 +165,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
165
165
  case "google.protobuf.Struct": {
166
166
  const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
167
167
  if (
168
- fields?.kind !== "map_field" ||
168
+ fields?.fieldKind !== "map" ||
169
169
  fields.mapValue.kind !== "message" ||
170
170
  fields.mapValue.message.typeName !== "google.protobuf.Value"
171
171
  ) {
@@ -179,7 +179,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
179
179
  (f) => f.number == 1 && f.oneof === kind
180
180
  );
181
181
  if (
182
- nullValue?.kind !== "enum_field" ||
182
+ nullValue?.fieldKind !== "enum" ||
183
183
  nullValue.enum.typeName !== "google.protobuf.NullValue"
184
184
  ) {
185
185
  return undefined;
@@ -187,21 +187,21 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
187
187
  const numberValue = message.fields.find(
188
188
  (f) =>
189
189
  f.number == 2 &&
190
- f.kind == "scalar_field" &&
190
+ f.fieldKind == "scalar" &&
191
191
  f.scalar === ScalarType.DOUBLE &&
192
192
  f.oneof === kind
193
193
  );
194
194
  const stringValue = message.fields.find(
195
195
  (f) =>
196
196
  f.number == 3 &&
197
- f.kind == "scalar_field" &&
197
+ f.fieldKind == "scalar" &&
198
198
  f.scalar === ScalarType.STRING &&
199
199
  f.oneof === kind
200
200
  );
201
201
  const boolValue = message.fields.find(
202
202
  (f) =>
203
203
  f.number == 4 &&
204
- f.kind == "scalar_field" &&
204
+ f.fieldKind == "scalar" &&
205
205
  f.scalar === ScalarType.BOOL &&
206
206
  f.oneof === kind
207
207
  );
@@ -209,7 +209,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
209
209
  (f) => f.number == 5 && f.oneof === kind
210
210
  );
211
211
  if (
212
- structValue?.kind !== "message_field" ||
212
+ structValue?.fieldKind !== "message" ||
213
213
  structValue.message.typeName !== "google.protobuf.Struct"
214
214
  ) {
215
215
  return undefined;
@@ -218,7 +218,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
218
218
  (f) => f.number == 6 && f.oneof === kind
219
219
  );
220
220
  if (
221
- listValue?.kind !== "message_field" ||
221
+ listValue?.fieldKind !== "message" ||
222
222
  listValue.message.typeName !== "google.protobuf.ListValue"
223
223
  ) {
224
224
  return undefined;
@@ -240,7 +240,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
240
240
  case "google.protobuf.ListValue": {
241
241
  const values = message.fields.find((f) => f.number == 1 && f.repeated);
242
242
  if (
243
- values?.kind != "message_field" ||
243
+ values?.fieldKind != "message" ||
244
244
  values.message.typeName !== "google.protobuf.Value"
245
245
  ) {
246
246
  break;
@@ -251,7 +251,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
251
251
  const paths = message.fields.find(
252
252
  (f) =>
253
253
  f.number == 1 &&
254
- f.kind == "scalar_field" &&
254
+ f.fieldKind == "scalar" &&
255
255
  f.scalar === ScalarType.STRING &&
256
256
  f.repeated
257
257
  );
@@ -275,7 +275,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
275
275
  if (!value) {
276
276
  break;
277
277
  }
278
- if (value.kind !== "scalar_field") {
278
+ if (value.fieldKind !== "scalar") {
279
279
  break;
280
280
  }
281
281
  return { typeName: message.typeName, value };
@@ -13,32 +13,15 @@
13
13
  // limitations under the License.
14
14
 
15
15
  import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
16
- import { typescript } from "./typescript.js";
17
- import { javascript } from "./javascript.js";
18
- import { declaration } from "./declaration";
16
+ import { generateTs } from "./typescript.js";
17
+ import { generateJs } from "./javascript.js";
18
+ import { generateDts } from "./declaration.js";
19
19
  import { version } from "../package.json";
20
20
 
21
- export const protocGenEs = createEcmaScriptPlugin(
22
- {
23
- name: "protoc-gen-es",
24
- version: `v${String(version)}`,
25
- },
26
- (schema) => {
27
- const targets = [typescript, javascript, declaration].filter((gen) =>
28
- schema.targets.includes(gen.target)
29
- );
30
- for (const target of targets) {
31
- for (const file of schema.files) {
32
- const f = schema.generateFile(file.name + target.extension);
33
- f.preamble(file);
34
- for (const enumeration of file.enums) {
35
- target.generateEnum(schema, f, enumeration);
36
- }
37
- for (const message of file.messages) {
38
- target.generateMessage(schema, f, message);
39
- }
40
- // We do not generate anything for services, and we do not support extensions at this time
41
- }
42
- }
43
- }
44
- );
21
+ export const protocGenEs = createEcmaScriptPlugin({
22
+ name: "protoc-gen-es",
23
+ version: `v${String(version)}`,
24
+ generateTs,
25
+ generateJs,
26
+ generateDts,
27
+ });
package/src/typescript.ts CHANGED
@@ -19,7 +19,11 @@ import type {
19
19
  DescOneof,
20
20
  } from "@bufbuild/protobuf";
21
21
  import { ScalarType } from "@bufbuild/protobuf";
22
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
22
+ import type {
23
+ GeneratedFile,
24
+ Printable,
25
+ Schema,
26
+ } from "@bufbuild/protoplugin/ecmascript";
23
27
  import {
24
28
  localName,
25
29
  getFieldIntrinsicDefaultValue,
@@ -30,12 +34,19 @@ import { matchWkt } from "./match-wkt.js";
30
34
  import { generateFieldInfo } from "./javascript.js";
31
35
  import { literalString } from "@bufbuild/protoplugin/ecmascript";
32
36
 
33
- export const typescript = {
34
- target: "ts",
35
- extension: "_pb.ts",
36
- generateEnum,
37
- generateMessage,
38
- } as const;
37
+ export function generateTs(schema: Schema) {
38
+ for (const file of schema.files) {
39
+ const f = schema.generateFile(file.name + "_pb.ts");
40
+ f.preamble(file);
41
+ for (const enumeration of file.enums) {
42
+ generateEnum(schema, f, enumeration);
43
+ }
44
+ for (const message of file.messages) {
45
+ generateMessage(schema, f, message);
46
+ }
47
+ // We do not generate anything for services, and we do not support extensions at this time
48
+ }
49
+ }
39
50
 
40
51
  // prettier-ignore
41
52
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -145,7 +156,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
145
156
 
146
157
  function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
147
158
  f.print(makeJsDoc(field, " "));
148
- const e: Parameters<typeof f.print> = [];
159
+ const e: Printable = [];
149
160
  e.push(" ", localName(field));
150
161
  const { defaultValue, typingInferrable } =
151
162
  getFieldIntrinsicDefaultValue(field);