Package Exports
- @bufbuild/protoc-gen-es/bin/protoc-gen-es
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@bufbuild/protoc-gen-es) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@bufbuild/protoc-gen-es
The code generator plugin for Protocol Buffers for ECMAScript. Learn more about the project at 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. It is compatible with Protocol Buffer compilers like buf and protoc.
To install the plugin and the runtime library, run:
npm install --save-dev @bufbuild/protoc-gen-es
npm install @bufbuild/protobufWe use peer dependencies to ensure that code generator and runtime library are compatible with each other. Note that npm installs them automatically, but yarn and pnpm do not.
Generating code
With buf
npm install --save-dev @bufbuild/bufAdd a new configuration file buf.gen.yaml:
# buf.gen.yaml defines a local generation template.
# For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml
version: v1
plugins:
# This will invoke protoc-gen-es and write output to src/gen
- plugin: es
out: src/gen
opt:
# Add more plugin options here
- target=tsTo generate code for all protobuf files within your project, simply run:
npx buf generateNote that buf can generate from various inputs,
not just local protobuf files.
With protoc
PATH=$PATH:$(pwd)/node_modules/.bin \
protoc -I . \
--es_out src/gen \
--es_opt target=ts \
a.proto b.proto c.protoNote 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.
Since yarn v2 and above does not use a node_modules directory, you need to
change the variable a bit:
PATH=$(dirname $(yarn bin protoc-gen-es)):$PATHPlugin options
target
This option controls whether the plugin generates JavaScript, TypeScript, or TypeScript declaration files.
Possible values:
target=js- generates a_pb.jsfile for every.protoinput file.target=ts- generates a_pb.tsfile for every.protoinput file.target=dts- generates a_pb.d.tsfile for every.protoinput file.
Multiple values can be given by separating them with +, for example
target=js+dts.
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=.js
By default, protoc-gen-es
(and all other plugins based on @bufbuild/protoplugin)
uses a .js file extensions in import paths, even in TypeScript files.
This is unintuitive, but necessary for ECMAScript modules in Node.js.
Unfortunately, not all bundlers and tools have caught up yet, and Deno
requires .ts. With this plugin option, you can replace .js extensions
in import paths with the given value. For example, set
import_extension=noneto remove the.jsextension.import_extension=.tsto replace the.jsextension with.ts.
js_import_style
By default, protoc-gen-es
(and all other plugins based on @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.
Possible values:
js_import_style=modulegenerate ECMAScriptimport/exportstatements - the default behavior.js_import_style=legacy_commonjsgenerate CommonJSrequire()calls.
keep_empty_files=true
By default, protoc-gen-es (and all other plugins based on @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.
ts_nocheck=false
By default, protoc-gen-es
(and all other plugins based on @bufbuild/protoplugin)
generate an annotation at the top of each file: // @ts-nocheck.
We generate the annotation to support a wide range of compiler configurations and
future changes to the language. But there can be situations where the annotation
shadows an underlying problem, for example an unresolvable import. To remove
the annotation and to enable type checks, set the plugin option ts_nocheck=false.