An Rsbuild plugin that integrates protobufjs. It compiles .proto files into JavaScript modules that run in browsers or Node.js, and can optionally generate TypeScript declaration files (.d.ts).
- What it does: Automatically matches and transforms
.protofiles - Built-in cache: Uses Rsbuild
cachePathand writes intermediates underprotobufjs - Configurable target: Passes
--targettoprotobufjs-cli(defaultstatic-module) - Type output: Generates
.d.tsby default for better TS DX
pnpm add -D rsbuild-plugin-protobufjs
# or
npm i -D rsbuild-plugin-protobufjs
# or
yarn add -D rsbuild-plugin-protobufjs- peerDependencies:
protobufjs(install it in your project) - dependencies: The plugin uses
protobufjs-cliinternally to runpbjs/pbts
Add the plugin in your Rsbuild configuration:
// rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';
import { protobufjsPlugin } from 'rsbuild-plugin-protobufjs';
export default defineConfig({
plugins: [
protobufjsPlugin({
// optional, see "Options" below
}),
],
});Now import .proto files directly in your source code:
import client from './client.proto';
// With target=static-module (default), the imported object exposes static encode/decode APIs
console.log(client);During build or dev, the plugin will:
- Use
pbjsto compile.protointo a JS module, emitted into Rsbuild's cache directory underprotobufjs. - If
dtsis enabled (default true), runpbtsto emitclient.proto.d.tsnext to the source.protofile.
The plugin exports protobufjsPlugin(options?: Options).
interface Options {
/**
* Files to include. Same as Rspack RuleSetCondition.
* @default /\.proto$/
*/
test?: import('@rsbuild/core').Rspack.RuleSetCondition;
/**
* Value passed to protobufjs-cli `--target`.
* Common values: 'static-module' (default), 'json-module', 'proto2', etc.
* @default 'static-module'
*/
target?: string;
/**
* Whether to generate `.d.ts` for each `.proto`.
* @default true
*/
dts?: boolean;
}- test: Which files to transform. Defaults to all
.protofiles. - target: Forwarded directly to
pbjs --target.static-moduleis recommended for most applications. - dts: Whether to call
pbtsto emit a sibling.d.tsfile next to the source.
// rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';
import { protobufjsPlugin } from 'rsbuild-plugin-protobufjs';
export default defineConfig({
plugins: [
protobufjsPlugin({
test: /\.proto$/, // only process .proto files
target: 'static-module', // generate static module
dts: true, // generate type declarations
}),
],
});Project code:
// src/index.ts
import client from './client.proto';
// Use encode/decode APIs as needed
// Actual exports depend on the chosen target = static-module
console.log(client);The declaration file will be emitted next to the source, e.g., src/client.proto.d.ts.
-
Q: Do I need both protobufjs and protobufjs-cli installed?
- A: Yes.
protobufjsis a peer dependency you install in your project; the plugin depends onprotobufjs-clifor compilation.
- A: Yes.
-
Q: Where are intermediate outputs written?
- A: Under Rsbuild's cache directory, e.g.,
node_modules/.cache/rsbuild/<hash>/protobufjs/. This is an implementation detail; the final code is returned to Rsbuild's pipeline.
- A: Under Rsbuild's cache directory, e.g.,
-
Q: Can I emit JS without
.d.ts?- A: Yes. Set
dts: false.
- A: Yes. Set
-
Q: How do I change pbjs output style?
- A: Use
targetto pass a value supported bypbjs --target, such asjson-module. Different targets produce different module shapes; see protobufjs-cli documentation.
- A: Use