@bufbuild/protoc-gen-es 2.12.0 → 2.13.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 +8 -4
- package/dist/cjs/package.json +5 -4
- package/dist/cjs/src/protoc-gen-es-plugin.js +51 -17
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @bufbuild/protoc-gen-es
|
|
2
2
|
|
|
3
|
-
The code generator plugin for Protocol Buffers for ECMAScript. Learn more about the project at [
|
|
3
|
+
The code generator plugin for Protocol Buffers for ECMAScript. Learn more about the project at [protobufes.com](https://protobufes.com/).
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -107,15 +107,19 @@ By default, [protoc-gen-es] inserts its version number at the top of each genera
|
|
|
107
107
|
|
|
108
108
|
### `json_types=true`
|
|
109
109
|
|
|
110
|
-
Generates JSON types for every Protobuf message and enumeration. Calling `toJson()` automatically returns the JSON type if available. Learn more about [JSON types](https://
|
|
110
|
+
Generates JSON types for every Protobuf message and enumeration. Calling `toJson()` automatically returns the JSON type if available. Learn more about [JSON types](https://protobufes.com/reference/json-types/).
|
|
111
111
|
|
|
112
112
|
### `valid_types` (experimental)
|
|
113
113
|
|
|
114
114
|
Generates a Valid type for every Protobuf message. Possible values:
|
|
115
115
|
|
|
116
|
-
- `valid_types=legacy_required`: Message fields with the `required` label, or the Edition feature `features.field_presence=LEGACY_REQUIRED`, are generated as non-optional properties.
|
|
116
|
+
- `valid_types=legacy_required`: Message fields with the `required` label, or the Edition feature `features.field_presence=LEGACY_REQUIRED`, are generated as non-optional properties.
|
|
117
117
|
- `valid_types=protovalidate_required`: Message fields with protovalidate's [`required` rule](https://buf.build/docs/reference/protovalidate/rules/field_rules/#required) rule are generated as non-optional properties.
|
|
118
118
|
|
|
119
119
|
You can combine both options with `+`—for example, `valid_types=legacy_required+protovalidate_required`.
|
|
120
120
|
|
|
121
|
-
Learn more about [Valid types](https://
|
|
121
|
+
Learn more about [Valid types](https://protobufes.com/reference/valid-types/).
|
|
122
|
+
|
|
123
|
+
### `erasable_syntax=true` (experimental)
|
|
124
|
+
|
|
125
|
+
Generates Protobuf enums as an object with `as const` for [running TypeScript natively in Node.js](https://nodejs.org/learn/typescript/run-natively), and for compatibility with the `tsconfig` option [erasableSyntaxOnly](https://www.typescriptlang.org/tsconfig/#erasableSyntaxOnly). See [Enums vs Objects](https://protobufes.com/reference/generated-code/#enums-vs-objects) for details.
|
package/dist/cjs/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Protocol Buffers code generator for ECMAScript",
|
|
6
6
|
"keywords": [
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"ecmascript",
|
|
11
11
|
"protoc-plugin"
|
|
12
12
|
],
|
|
13
|
+
"homepage": "https://protobufes.com/",
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
15
16
|
"url": "https://github.com/bufbuild/protobuf-es.git",
|
|
@@ -32,14 +33,14 @@
|
|
|
32
33
|
},
|
|
33
34
|
"preferUnplugged": true,
|
|
34
35
|
"dependencies": {
|
|
35
|
-
"@bufbuild/protobuf": "2.
|
|
36
|
-
"@bufbuild/protoplugin": "2.
|
|
36
|
+
"@bufbuild/protobuf": "2.13.0",
|
|
37
|
+
"@bufbuild/protoplugin": "2.13.0"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
40
|
"upstream-protobuf": "*"
|
|
40
41
|
},
|
|
41
42
|
"peerDependencies": {
|
|
42
|
-
"@bufbuild/protobuf": "2.
|
|
43
|
+
"@bufbuild/protobuf": "2.13.0"
|
|
43
44
|
},
|
|
44
45
|
"peerDependenciesMeta": {
|
|
45
46
|
"@bufbuild/protobuf": {
|
|
@@ -35,6 +35,7 @@ function parseOptions(options) {
|
|
|
35
35
|
legacyRequired: false,
|
|
36
36
|
protovalidateRequired: false,
|
|
37
37
|
};
|
|
38
|
+
let erasableSyntax = false;
|
|
38
39
|
for (const { key, value } of options) {
|
|
39
40
|
switch (key) {
|
|
40
41
|
case "json_types":
|
|
@@ -57,18 +58,23 @@ function parseOptions(options) {
|
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
break;
|
|
61
|
+
case "erasable_syntax":
|
|
62
|
+
if (!["true", "1", "false", "0"].includes(value)) {
|
|
63
|
+
throw "please provide true or false";
|
|
64
|
+
}
|
|
65
|
+
erasableSyntax = ["true", "1"].includes(value);
|
|
66
|
+
break;
|
|
60
67
|
default:
|
|
61
68
|
throw new Error();
|
|
62
69
|
}
|
|
63
70
|
}
|
|
64
|
-
return { jsonTypes, validTypes };
|
|
71
|
+
return { jsonTypes, validTypes, erasableSyntax };
|
|
65
72
|
}
|
|
66
73
|
// This annotation informs bundlers that the succeeding function call is free of
|
|
67
74
|
// side effects. This means the symbol can be removed from the module during
|
|
68
75
|
// tree-shaking if it is unused.
|
|
69
76
|
// See https://github.com/bufbuild/protobuf-es/pull/470
|
|
70
77
|
const pure = "/*@__PURE__*/";
|
|
71
|
-
// biome-ignore format: want this to read well
|
|
72
78
|
function generateTs(schema) {
|
|
73
79
|
for (const file of schema.files) {
|
|
74
80
|
const f = schema.generateFile(file.name + "_pb.ts");
|
|
@@ -98,7 +104,12 @@ function generateTs(schema) {
|
|
|
98
104
|
break;
|
|
99
105
|
}
|
|
100
106
|
case "enum": {
|
|
101
|
-
|
|
107
|
+
if (schema.options.erasableSyntax) {
|
|
108
|
+
generateObjEnum(f, desc, "ts");
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
generateEnumShape(f, desc);
|
|
112
|
+
}
|
|
102
113
|
if (schema.options.jsonTypes) {
|
|
103
114
|
generateEnumJsonShape(f, desc, "ts");
|
|
104
115
|
}
|
|
@@ -144,7 +155,6 @@ function generateTs(schema) {
|
|
|
144
155
|
}
|
|
145
156
|
}
|
|
146
157
|
}
|
|
147
|
-
// biome-ignore format: want this to read well
|
|
148
158
|
function generateJs(schema) {
|
|
149
159
|
for (const file of schema.files) {
|
|
150
160
|
const f = schema.generateFile(file.name + "_pb.js");
|
|
@@ -177,12 +187,12 @@ function generateJs(schema) {
|
|
|
177
187
|
f.print(" ", call, ";");
|
|
178
188
|
f.print();
|
|
179
189
|
}
|
|
180
|
-
//
|
|
190
|
+
// generate enum
|
|
181
191
|
{
|
|
182
192
|
f.print(f.jsDoc(desc));
|
|
183
193
|
f.print(f.export("const", f.importShape(desc).name), " = ", pure);
|
|
184
|
-
const
|
|
185
|
-
const call = (0, util_js_1.functionCall)(
|
|
194
|
+
const enumFn = schema.options.erasableSyntax ? f.runtime.codegen.objEnum : f.runtime.codegen.tsEnum;
|
|
195
|
+
const call = (0, util_js_1.functionCall)(enumFn, [f.importSchema(desc)]);
|
|
186
196
|
f.print(" ", call, ";");
|
|
187
197
|
f.print();
|
|
188
198
|
}
|
|
@@ -212,7 +222,6 @@ function generateJs(schema) {
|
|
|
212
222
|
}
|
|
213
223
|
}
|
|
214
224
|
}
|
|
215
|
-
// biome-ignore format: want this to read well
|
|
216
225
|
function generateDts(schema) {
|
|
217
226
|
for (const file of schema.files) {
|
|
218
227
|
const f = schema.generateFile(file.name + "_pb.d.ts");
|
|
@@ -239,7 +248,12 @@ function generateDts(schema) {
|
|
|
239
248
|
break;
|
|
240
249
|
}
|
|
241
250
|
case "enum": {
|
|
242
|
-
|
|
251
|
+
if (schema.options.erasableSyntax) {
|
|
252
|
+
generateObjEnum(f, desc, "dts");
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
generateEnumShape(f, desc);
|
|
256
|
+
}
|
|
243
257
|
if (schema.options.jsonTypes) {
|
|
244
258
|
generateEnumJsonShape(f, desc, "dts");
|
|
245
259
|
}
|
|
@@ -304,7 +318,6 @@ function generateDescDoc(f, desc) {
|
|
|
304
318
|
text: lines.join("\n"),
|
|
305
319
|
});
|
|
306
320
|
}
|
|
307
|
-
// biome-ignore format: want this to read well
|
|
308
321
|
function getFileDescCall(f, file, schema) {
|
|
309
322
|
// Schema provides files with source retention options. Since we do not want to
|
|
310
323
|
// embed source retention options in generated code, we use FileDescriptorProto
|
|
@@ -330,7 +343,6 @@ function getFileDescCall(f, file, schema) {
|
|
|
330
343
|
}
|
|
331
344
|
return (0, util_js_1.functionCall)(fileDesc, [f.string(info.base64())]);
|
|
332
345
|
}
|
|
333
|
-
// biome-ignore format: want this to read well
|
|
334
346
|
function getServiceShapeExpr(f, service) {
|
|
335
347
|
const p = ["{\n"];
|
|
336
348
|
for (const method of service.methods) {
|
|
@@ -344,7 +356,6 @@ function getServiceShapeExpr(f, service) {
|
|
|
344
356
|
p.push("}");
|
|
345
357
|
return p;
|
|
346
358
|
}
|
|
347
|
-
// biome-ignore format: want this to read well
|
|
348
359
|
function generateEnumShape(f, enumeration) {
|
|
349
360
|
f.print(f.jsDoc(enumeration));
|
|
350
361
|
f.print(f.export("enum", f.importShape(enumeration).name), " {");
|
|
@@ -358,7 +369,34 @@ function generateEnumShape(f, enumeration) {
|
|
|
358
369
|
f.print("}");
|
|
359
370
|
f.print();
|
|
360
371
|
}
|
|
361
|
-
|
|
372
|
+
function generateObjEnum(f, enumeration, target) {
|
|
373
|
+
const { name } = f.importShape(enumeration);
|
|
374
|
+
f.print(f.jsDoc(enumeration));
|
|
375
|
+
if (target == "ts") {
|
|
376
|
+
f.print(f.export("const", name), " = {");
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
f.print(f.export("declare const", name), ": {");
|
|
380
|
+
}
|
|
381
|
+
for (const value of enumeration.values) {
|
|
382
|
+
if (enumeration.values.indexOf(value) > 0) {
|
|
383
|
+
f.print();
|
|
384
|
+
}
|
|
385
|
+
f.print(f.jsDoc(value, " "));
|
|
386
|
+
f.print(" ", (target == "ts" ? "" : "readonly "), value.localName, ": ", value.number, ",");
|
|
387
|
+
}
|
|
388
|
+
f.print("}", (target == "ts" ? " as const" : ""), ";");
|
|
389
|
+
f.print();
|
|
390
|
+
generateObjEnumType(f, enumeration, target);
|
|
391
|
+
}
|
|
392
|
+
function generateObjEnumType(f, enumeration, target) {
|
|
393
|
+
const { name } = f.importShape(enumeration);
|
|
394
|
+
f.print(f.jsDoc(enumeration));
|
|
395
|
+
const declaration = target == "ts" ? "type" : "declare type";
|
|
396
|
+
const unknown = enumeration.open ? [" | ", f.runtime.UnknownEnum] : [];
|
|
397
|
+
f.print(f.export(declaration, name), " = (typeof ", name, ")[keyof typeof ", name, "]", unknown, ";");
|
|
398
|
+
f.print();
|
|
399
|
+
}
|
|
362
400
|
function generateEnumJsonShape(f, enumeration, target) {
|
|
363
401
|
f.print(f.jsDoc(enumeration));
|
|
364
402
|
const declaration = target == "ts" ? "type" : "declare type";
|
|
@@ -377,7 +415,6 @@ function generateEnumJsonShape(f, enumeration, target) {
|
|
|
377
415
|
f.print(f.export(declaration, f.importJson(enumeration).name), " = ", values, ";");
|
|
378
416
|
f.print();
|
|
379
417
|
}
|
|
380
|
-
// biome-ignore format: want this to read well
|
|
381
418
|
function generateMessageShape(f, message, target) {
|
|
382
419
|
const { Message } = f.runtime;
|
|
383
420
|
const declaration = target == "ts" ? "type" : "declare type";
|
|
@@ -392,7 +429,6 @@ function generateMessageShape(f, message, target) {
|
|
|
392
429
|
f.print("};");
|
|
393
430
|
f.print();
|
|
394
431
|
}
|
|
395
|
-
// biome-ignore format: want this to read well
|
|
396
432
|
function generateMessageValidShape(f, message, validTypes, target) {
|
|
397
433
|
const declaration = target == "ts" ? "type" : "declare type";
|
|
398
434
|
if (!(0, valid_types_js_1.messageNeedsCustomValidType)(message, validTypes)) {
|
|
@@ -412,7 +448,6 @@ function generateMessageValidShape(f, message, validTypes, target) {
|
|
|
412
448
|
f.print("};");
|
|
413
449
|
f.print();
|
|
414
450
|
}
|
|
415
|
-
// biome-ignore format: want this to read well
|
|
416
451
|
function generateMessageShapeMember(f, member, validTypes) {
|
|
417
452
|
switch (member.kind) {
|
|
418
453
|
case "oneof":
|
|
@@ -447,7 +482,6 @@ function generateMessageShapeMember(f, member, validTypes) {
|
|
|
447
482
|
break;
|
|
448
483
|
}
|
|
449
484
|
}
|
|
450
|
-
// biome-ignore format: want this to read well
|
|
451
485
|
function generateMessageJsonShape(f, message, target) {
|
|
452
486
|
const exp = f.export(target == "ts" ? "type" : "declare type", f.importJson(message).name);
|
|
453
487
|
f.print(f.jsDoc(message));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Protocol Buffers code generator for ECMAScript",
|
|
6
6
|
"keywords": [
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"ecmascript",
|
|
11
11
|
"protoc-plugin"
|
|
12
12
|
],
|
|
13
|
+
"homepage": "https://protobufes.com/",
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
15
16
|
"url": "https://github.com/bufbuild/protobuf-es.git",
|
|
@@ -32,14 +33,14 @@
|
|
|
32
33
|
},
|
|
33
34
|
"preferUnplugged": true,
|
|
34
35
|
"dependencies": {
|
|
35
|
-
"@bufbuild/protobuf": "2.
|
|
36
|
-
"@bufbuild/protoplugin": "2.
|
|
36
|
+
"@bufbuild/protobuf": "2.13.0",
|
|
37
|
+
"@bufbuild/protoplugin": "2.13.0"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
40
|
"upstream-protobuf": "*"
|
|
40
41
|
},
|
|
41
42
|
"peerDependencies": {
|
|
42
|
-
"@bufbuild/protobuf": "2.
|
|
43
|
+
"@bufbuild/protobuf": "2.13.0"
|
|
43
44
|
},
|
|
44
45
|
"peerDependenciesMeta": {
|
|
45
46
|
"@bufbuild/protobuf": {
|