@bufbuild/protoc-gen-es
Advanced tools
Comparing version 2.0.0-beta.2 to 2.0.0-beta.3
{ | ||
"name": "@bufbuild/protoc-gen-es", | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta.3", | ||
"license": "Apache-2.0", | ||
"description": "Protocol Buffers code generator for ECMAScript", | ||
"license": "Apache-2.0", | ||
"keywords": [ | ||
"protobuf", | ||
"schema", | ||
"typescript", | ||
"ecmascript", | ||
"protoc-plugin" | ||
], | ||
"repository": { | ||
@@ -26,7 +33,7 @@ "type": "git", | ||
"dependencies": { | ||
"@bufbuild/protobuf": "^2.0.0-beta.2", | ||
"@bufbuild/protoplugin": "2.0.0-beta.2" | ||
"@bufbuild/protobuf": "^2.0.0-beta.3", | ||
"@bufbuild/protoplugin": "2.0.0-beta.3" | ||
}, | ||
"peerDependencies": { | ||
"@bufbuild/protobuf": "2.0.0-beta.2" | ||
"@bufbuild/protobuf": "2.0.0-beta.3" | ||
}, | ||
@@ -33,0 +40,0 @@ "peerDependenciesMeta": { |
@@ -57,6 +57,6 @@ "use strict"; | ||
f.preamble(file); | ||
const { GenDescFile } = f.runtime.codegen; | ||
const { GenFile } = f.runtime.codegen; | ||
const fileDesc = f.importSchema(file); | ||
generateDescDoc(f, file); | ||
f.print(f.export("const", fileDesc.name), ": ", GenDescFile, " = ", pure); | ||
f.print(f.export("const", fileDesc.name), ": ", GenFile, " = ", pure); | ||
f.print(" ", getFileDescCall(f, file, schema), ";"); | ||
@@ -74,9 +74,9 @@ f.print(); | ||
const Shape = f.importShape(desc); | ||
const { GenDescMessage, messageDesc } = f.runtime.codegen; | ||
const { GenMessage, messageDesc } = f.runtime.codegen; | ||
if (schema.options.jsonTypes) { | ||
const JsonType = f.importJson(desc); | ||
f.print(f.export("const", name), ": ", GenDescMessage, "<", Shape, ", ", JsonType, ">", " = ", pure); | ||
f.print(f.export("const", name), ": ", GenMessage, "<", Shape, ", ", JsonType, ">", " = ", pure); | ||
} | ||
else { | ||
f.print(f.export("const", name), ": ", GenDescMessage, "<", Shape, ">", " = ", pure); | ||
f.print(f.export("const", name), ": ", GenMessage, "<", Shape, ">", " = ", pure); | ||
} | ||
@@ -96,9 +96,9 @@ const call = (0, util_1.functionCall)(messageDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]); | ||
const Shape = f.importShape(desc); | ||
const { GenDescEnum, enumDesc } = f.runtime.codegen; | ||
const { GenEnum, enumDesc } = f.runtime.codegen; | ||
if (schema.options.jsonTypes) { | ||
const JsonType = f.importJson(desc); | ||
f.print(f.export("const", name), ": ", GenDescEnum, "<", Shape, ", ", JsonType, ">", " = ", pure); | ||
f.print(f.export("const", name), ": ", GenEnum, "<", Shape, ", ", JsonType, ">", " = ", pure); | ||
} | ||
else { | ||
f.print(f.export("const", name), ": ", GenDescEnum, "<", Shape, ">", " = ", pure); | ||
f.print(f.export("const", name), ": ", GenEnum, "<", Shape, ">", " = ", pure); | ||
} | ||
@@ -111,3 +111,3 @@ const call = (0, util_1.functionCall)(enumDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]); | ||
case "extension": { | ||
const { GenDescExtension, extDesc } = f.runtime.codegen; | ||
const { GenExtension, extDesc } = f.runtime.codegen; | ||
const name = f.importSchema(desc).name; | ||
@@ -118,3 +118,3 @@ const E = f.importShape(desc.extendee); | ||
f.print(f.jsDoc(desc)); | ||
f.print(f.export("const", name), ": ", GenDescExtension, "<", E, ", ", V, ">", " = ", pure); | ||
f.print(f.export("const", name), ": ", GenExtension, "<", E, ", ", V, ">", " = ", pure); | ||
f.print(" ", call, ";"); | ||
@@ -125,7 +125,7 @@ f.print(); | ||
case "service": { | ||
const { GenDescService, serviceDesc } = f.runtime.codegen; | ||
const { GenService, serviceDesc } = f.runtime.codegen; | ||
const name = f.importSchema(desc).name; | ||
const call = (0, util_1.functionCall)(serviceDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]); | ||
f.print(f.jsDoc(desc)); | ||
f.print(f.export("const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), "> = ", pure); | ||
f.print(f.export("const", name), ": ", GenService, "<", getServiceShapeExpr(f, desc), "> = ", pure); | ||
f.print(" ", call, ";"); | ||
@@ -212,6 +212,6 @@ f.print(); | ||
f.preamble(file); | ||
const { GenDescFile } = f.runtime.codegen; | ||
const { GenFile } = f.runtime.codegen; | ||
const fileDesc = f.importSchema(file); | ||
generateDescDoc(f, file); | ||
f.print(f.export("declare const", fileDesc.name), ": ", GenDescFile, ";"); | ||
f.print(f.export("declare const", fileDesc.name), ": ", GenFile, ";"); | ||
f.print(); | ||
@@ -227,10 +227,10 @@ for (const desc of schema.typesInFile(file)) { | ||
const Shape = f.importShape(desc); | ||
const { GenDescMessage } = f.runtime.codegen; | ||
const { GenMessage } = f.runtime.codegen; | ||
generateDescDoc(f, desc); | ||
if (schema.options.jsonTypes) { | ||
const JsonType = f.importJson(desc); | ||
f.print(f.export("declare const", name), ": ", GenDescMessage, "<", Shape, ", ", JsonType, ">", ";"); | ||
f.print(f.export("declare const", name), ": ", GenMessage, "<", Shape, ", ", JsonType, ">", ";"); | ||
} | ||
else { | ||
f.print(f.export("declare const", name), ": ", GenDescMessage, "<", Shape, ">", ";"); | ||
f.print(f.export("declare const", name), ": ", GenMessage, "<", Shape, ">", ";"); | ||
} | ||
@@ -248,9 +248,9 @@ f.print(); | ||
const Shape = f.importShape(desc); | ||
const { GenDescEnum } = f.runtime.codegen; | ||
const { GenEnum } = f.runtime.codegen; | ||
if (schema.options.jsonTypes) { | ||
const JsonType = f.importJson(desc); | ||
f.print(f.export("declare const", name), ": ", GenDescEnum, "<", Shape, ", ", JsonType, ">;"); | ||
f.print(f.export("declare const", name), ": ", GenEnum, "<", Shape, ", ", JsonType, ">;"); | ||
} | ||
else { | ||
f.print(f.export("declare const", name), ": ", GenDescEnum, "<", Shape, ">;"); | ||
f.print(f.export("declare const", name), ": ", GenEnum, "<", Shape, ">;"); | ||
} | ||
@@ -261,3 +261,3 @@ f.print(); | ||
case "extension": { | ||
const { GenDescExtension } = f.runtime.codegen; | ||
const { GenExtension } = f.runtime.codegen; | ||
const name = f.importSchema(desc).name; | ||
@@ -267,3 +267,3 @@ const E = f.importShape(desc.extendee); | ||
f.print(f.jsDoc(desc)); | ||
f.print(f.export("declare const", name), ": ", GenDescExtension, "<", E, ", ", V, ">;"); | ||
f.print(f.export("declare const", name), ": ", GenExtension, "<", E, ", ", V, ">;"); | ||
f.print(); | ||
@@ -273,6 +273,6 @@ break; | ||
case "service": { | ||
const { GenDescService } = f.runtime.codegen; | ||
const { GenService } = f.runtime.codegen; | ||
const name = f.importSchema(desc).name; | ||
f.print(f.jsDoc(desc)); | ||
f.print(f.export("declare const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), ">;"); | ||
f.print(f.export("declare const", name), ": ", GenService, "<", getServiceShapeExpr(f, desc), ">;"); | ||
f.print(); | ||
@@ -331,3 +331,3 @@ break; | ||
f.string(info.base64()), | ||
(0, util_1.arrayLiteral)(deps), | ||
f.array(deps), | ||
]); | ||
@@ -339,16 +339,12 @@ } | ||
function getServiceShapeExpr(f, service) { | ||
const p = []; | ||
function print(...printables) { | ||
p.push(...printables, "\n"); | ||
} | ||
print("{"); | ||
const p = ["{\n"]; | ||
for (const method of service.methods) { | ||
print(f.jsDoc(method, " ")); | ||
print(" ", method.localName, ": {"); | ||
print(" methodKind: ", f.string(method.methodKind), ";"); | ||
print(" input: typeof ", f.importSchema(method.input, true), ";"); | ||
print(" output: typeof ", f.importSchema(method.output, true), ";"); | ||
print(" },"); | ||
p.push(f.jsDoc(method, " "), "\n"); | ||
p.push(" ", method.localName, ": {\n"); | ||
p.push(" methodKind: ", f.string(method.methodKind), ";\n"); | ||
p.push(" input: typeof ", f.importSchema(method.input, true), ";\n"); | ||
p.push(" output: typeof ", f.importSchema(method.output, true), ";\n"); | ||
p.push(" },\n"); | ||
} | ||
print("}"); | ||
p.push("}"); | ||
return p; | ||
@@ -355,0 +351,0 @@ } |
@@ -9,2 +9,1 @@ import { type DescExtension, type DescField } from "@bufbuild/protobuf"; | ||
export declare function functionCall(fn: Printable, args: Printable[], typeParams?: Printable[]): Printable; | ||
export declare function arrayLiteral(elements: Printable[]): Printable; |
@@ -16,3 +16,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.arrayLiteral = exports.functionCall = exports.fieldJsonType = exports.fieldTypeScriptType = void 0; | ||
exports.fieldTypeScriptType = fieldTypeScriptType; | ||
exports.fieldJsonType = fieldJsonType; | ||
exports.functionCall = functionCall; | ||
const protobuf_1 = require("@bufbuild/protobuf"); | ||
@@ -95,3 +97,2 @@ const codegenv1_1 = require("@bufbuild/protobuf/codegenv1"); | ||
} | ||
exports.fieldTypeScriptType = fieldTypeScriptType; | ||
function messageFieldTypeScriptType(field, imports) { | ||
@@ -191,3 +192,2 @@ var _a; | ||
} | ||
exports.fieldJsonType = fieldJsonType; | ||
function functionCall(fn, args, typeParams) { | ||
@@ -200,7 +200,2 @@ let tp = []; | ||
} | ||
exports.functionCall = functionCall; | ||
function arrayLiteral(elements) { | ||
return ["[", commaSeparate(elements), "]"]; | ||
} | ||
exports.arrayLiteral = arrayLiteral; | ||
function commaSeparate(elements) { | ||
@@ -207,0 +202,0 @@ const r = []; |
{ | ||
"name": "@bufbuild/protoc-gen-es", | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta.3", | ||
"license": "Apache-2.0", | ||
"description": "Protocol Buffers code generator for ECMAScript", | ||
"license": "Apache-2.0", | ||
"keywords": [ | ||
"protobuf", | ||
"schema", | ||
"typescript", | ||
"ecmascript", | ||
"protoc-plugin" | ||
], | ||
"repository": { | ||
@@ -26,7 +33,7 @@ "type": "git", | ||
"dependencies": { | ||
"@bufbuild/protobuf": "^2.0.0-beta.2", | ||
"@bufbuild/protoplugin": "2.0.0-beta.2" | ||
"@bufbuild/protobuf": "^2.0.0-beta.3", | ||
"@bufbuild/protoplugin": "2.0.0-beta.3" | ||
}, | ||
"peerDependencies": { | ||
"@bufbuild/protobuf": "2.0.0-beta.2" | ||
"@bufbuild/protobuf": "2.0.0-beta.3" | ||
}, | ||
@@ -33,0 +40,0 @@ "peerDependenciesMeta": { |
110
README.md
# @bufbuild/protoc-gen-es | ||
The code generator plugin for Protocol Buffers for ECMAScript. Learn more about the project at [github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es). | ||
The code generator plugin for Protocol Buffers for ECMAScript. Learn more about the project at | ||
[github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es). | ||
## Installation | ||
`protoc-gen-es` generates base types - messages and enumerations - from your Protocol Buffer | ||
schema. The generated code requires the runtime library [@bufbuild/protobuf](https://www.npmjs.com/package/@bufbuild/protobuf). It is compatible with Protocol Buffer compilers like [buf](https://github.com/bufbuild/buf) and [protoc](https://github.com/protocolbuffers/protobuf/releases). | ||
`protoc-gen-es` generates base types—messages and enumerations—from your Protocol Buffer | ||
schema. The generated code requires the runtime library [@bufbuild/protobuf](https://www.npmjs.com/package/@bufbuild/protobuf). | ||
It's compatible with Protocol Buffer compilers like [buf](https://github.com/bufbuild/buf) and [protoc](https://github.com/protocolbuffers/protobuf/releases). | ||
@@ -17,4 +19,4 @@ To install the plugin and the runtime library, run: | ||
We use peer dependencies to ensure that code generator and runtime library are | ||
compatible with each other. Note that npm installs them automatically, but yarn | ||
We use peer dependencies to ensure that the code generator and runtime library are | ||
compatible with each other. Note that npm installs them automatically, but Yarn | ||
and pnpm do not. | ||
@@ -30,11 +32,10 @@ | ||
Add a new configuration file `buf.gen.yaml`: | ||
Add a new `buf.gen.yaml` configuration file: | ||
```yaml | ||
# buf.gen.yaml defines a local generation template. | ||
# For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml | ||
version: v1 | ||
# Learn more: https://buf.build/docs/configuration/v2/buf-gen-yaml | ||
version: v2 | ||
plugins: | ||
# This will invoke protoc-gen-es and write output to src/gen | ||
- plugin: es | ||
- local: protoc-gen-es | ||
out: src/gen | ||
@@ -46,3 +47,3 @@ opt: | ||
To generate code for all protobuf files within your project, simply run: | ||
To generate code for all Protobuf files within your project, run: | ||
@@ -53,6 +54,6 @@ ```bash | ||
Note that `buf` can generate from various [inputs](https://docs.buf.build/reference/inputs), | ||
not just local protobuf files. | ||
Note that `buf` can generate from various [inputs](https://buf.build/docs/reference/inputs), | ||
not just local Protobuf files. | ||
### With protoc | ||
### With `protoc` | ||
@@ -67,9 +68,9 @@ ```bash | ||
Note that we are adding `node_modules/.bin` to the `$PATH`, so that the protocol | ||
buffer compiler can find them. This happens automatically with npm scripts. | ||
Note that `node_modules/.bin` needs to be added to the `$PATH` so that the Protobuf compiler can find the plugin. This | ||
happens automatically with npm scripts. | ||
Since yarn v2 and above does not use a `node_modules` directory, you need to | ||
change the variable a bit: | ||
If you use Yarn, versions v2 and above don't use a `node_modules` directory, so you need to change the variable a | ||
bit: | ||
```bash | ||
```shellsession | ||
PATH=$(dirname $(yarn bin protoc-gen-es)):$PATH | ||
@@ -82,62 +83,43 @@ ``` | ||
This option controls whether the plugin generates JavaScript, TypeScript, | ||
or TypeScript declaration files. | ||
This option controls whether the plugin generates JavaScript, TypeScript, or TypeScript declaration files. Possible | ||
values: | ||
Possible values: | ||
- `target=js`: Generates a `_pb.js` file for every `.proto` input file. | ||
- `target=ts`: Generates a `_pb.ts` file for every `.proto` input file. | ||
- `target=dts`: Generates a `_pb.d.ts` file for every `.proto` input file. | ||
- `target=js` - generates a `_pb.js` file for every `.proto` input file. | ||
- `target=ts` - generates a `_pb.ts` file for every `.proto` input file. | ||
- `target=dts` - generates a `_pb.d.ts` file for every `.proto` input file. | ||
You can pass multiple values by separating them with `+`—for example, `target=js+dts`. | ||
Multiple values can be given by separating them with `+`, for example | ||
`target=js+dts`. | ||
By default, it generates JavaScript and TypeScript declaration files, which produces the smallest code size and is the | ||
most compatible with various bundler configurations. If you prefer to generate TypeScript, use `target=ts`. | ||
By default, we generate JavaScript and TypeScript declaration files, which | ||
produces the smallest code size and is the most compatible with various | ||
bundler configurations. If you prefer to generate TypeScript, use `target=ts`. | ||
### `import_extension` | ||
By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es) | ||
(and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin)) | ||
does not add a file extensions to import paths. | ||
By default, `protoc-gen-es` doesn't add file extensions to import paths. However, some | ||
environments require an import extension. For example, using ECMAScript modules in Node.js | ||
requires the `.js` extension, and Deno requires `.ts`. With this plugin option, you can add `.js`/`.ts` extensions in | ||
import paths with the given value. Possible values: | ||
Some environments require an import extension. For example, using [ECMAScript modules in Node.js](https://www.typescriptlang.org/docs/handbook/esm-node.html) requires the `.js` extension, and Deno requires `.ts`. With this plugin option, you can add `.js`/`.ts` extensions | ||
in import paths with the given value. For example, set | ||
- `import_extension=none`: Doesn't add an extension. (Default) | ||
- `import_extension=js`: Adds the `.js` extension. | ||
- `import_extension=ts`. Adds the `.ts` extension. | ||
- `import_extension=.js` to add the `.js` extension. | ||
- `import_extension=.ts` to add the `.ts` extension. | ||
- `import_extension=none` to not add an extension. (Default) | ||
### `js_import_style` | ||
By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es) | ||
(and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin)) | ||
generate ECMAScript `import` and `export` statements. For use cases where | ||
CommonJS is difficult to avoid, this option can be used to generate CommonJS | ||
`require()` calls. | ||
By default, `protoc-gen-es` generates ECMAScript `import` and `export` statements. For use cases where CommonJS is | ||
difficult to avoid, this option can be used to generate CommonJS `require()` calls. Possible values: | ||
Possible values: | ||
- `js_import_style=module`: Generates ECMAScript `import`/`export` statements. (Default) | ||
- `js_import_style=legacy_commonjs`: Generates CommonJS `require()` calls. | ||
- `js_import_style=module` generate ECMAScript `import` / `export` statements - | ||
the default behavior. | ||
- `js_import_style=legacy_commonjs` generate CommonJS `require()` calls. | ||
### `keep_empty_files=true` | ||
By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es) | ||
(and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin)) | ||
omit empty files from the plugin output. This option disables pruning of | ||
empty files, to allow for smooth interoperation with Bazel and similar | ||
tooling that requires all output files to be declared ahead of time. | ||
Unless you use Bazel, it is very unlikely that you need this option. | ||
By default, `protoc-gen-es` omits empty files from the plugin output. This option disables pruning of empty files to | ||
allow for smooth interoperation with Bazel and similar tooling that requires all output files to be declared ahead of | ||
time. Unless you use Bazel, you probably don't need this option. | ||
### `ts_nocheck=true` | ||
[protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es) generates | ||
valid TypeScript for current versions of the TypeScript compiler with standard | ||
settings. | ||
In case you use compiler settings that yield an error for generated code, you | ||
can set the plugin option `ts_nocheck=true`. This will generate an annotation at | ||
`protoc-gen-es` generates valid TypeScript for current versions of the TypeScript compiler with standard settings. | ||
If you use compiler settings that yield an error for generated code, setting this option generates an annotation at | ||
the top of each file to skip type checks: `// @ts-nocheck`. | ||
@@ -147,3 +129,3 @@ | ||
Generates JSON types for every Protobuf message and enumeration. Calling `toJson()` | ||
will automatically return the JSON type if available. | ||
Generates JSON types for every Protobuf message and enumeration. Calling `toJson()` automatically returns the JSON type | ||
if available. Learn more about [JSON types](https://github.com/bufbuild/protobuf-es/blob/main/MANUAL.md#json-types). |
35370
686
125
+ Added@bufbuild/protobuf@2.0.0-beta.3(transitive)
+ Added@bufbuild/protoplugin@2.0.0-beta.3(transitive)
- Removed@bufbuild/protobuf@2.0.0-beta.2(transitive)
- Removed@bufbuild/protoplugin@2.0.0-beta.2(transitive)