Skip to content

Commit

Permalink
Support JavaScript output (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored Jan 4, 2022
1 parent 085b04e commit 4304289
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 100 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ New features:
- The new plugin option `add_pb_suffix` adds the suffix `_pb` to all file names, see #186.
- Add service/method name to RpcError, see #197
Thanks to @jcready for the contribution.
- Support JavaScript output via plugin option `output_javascript`, see #200.


Bug fixes:
Expand Down
70 changes: 61 additions & 9 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protobuf-ts manual
- [Code size vs speed](#code-size-vs-speed)
- [Running in the Web Browser](#running-in-the-web-browser)
- [Running in Node.js](#running-in-nodejs)
- [Outputting JavaScript](#outputting-javascript)
- [RPC support](#rpc-support)
- [Generic RPC clients](#generic-rpc-clients)
- [RPC options](#rpc-options)
Expand Down Expand Up @@ -138,21 +139,56 @@ Available plugin options:
- "force_exclude_all_options"
By default, custom options are included in the metadata and can be blacklisted
with our option (ts.exclude_options). Set this option if you are certain you
with our option (ts.exclude_options). Set this option if you are certain you
do not want to include any options at all.
- "keep_enum_prefix"
By default, if all enum values share a prefix that corresponds with the enum's name,
the prefix is dropped from the value names. Set this option to disable this behavior.
- "keep_enum_prefix"
By default, if all enum values share a prefix that corresponds with the enum's
name, the prefix is dropped from the value names. Set this option to disable
this behavior.
- "ts_nocheck"
Generate a @ts-nocheck annotation at the top of each file. This will become the
- "ts_nocheck"
Generate a @ts-nocheck annotation at the top of each file. This will become the
default behaviour in the next major release.
- "add_pb_suffix"
- "disable_ts_nocheck"
Do not generate a @ts-nocheck annotation at the top of each file. Since this is
the default behaviour, this option has no effect.
- "add_pb_suffix"
Adds the suffix `_pb` to the names of all generated files. This will become the
default behaviour in the next major release.
- "output_typescript"
Output TypeScript files. This is the default behavior.
- "output_javascript"
Output JavaScript for the currently recommended target ES2020. The target may
change with a major release of protobuf-ts.
By default, the ECMAScript module system is used (`import` and `export`).
Along with JavaScript files, this always outputs TypeScript declaration files.
- "output_javascript_es2015"
Output JavaScript for the ES2015 target.
- "output_javascript_es2016"
Output JavaScript for the ES2016 target.
- "output_javascript_es2017"
Output JavaScript for the ES2017 target.
- "output_javascript_es2018"
Output JavaScript for the ES2018 target.
- "output_javascript_es2019"
Output JavaScript for the ES2019 target.
- "output_javascript_es2020"
Output JavaScript for the ES2020 target.
- "output_legacy_commonjs"
Use CommonJS instead of the default ECMAScript module system.
- "client_none"
Do not generate rpc clients.
Only applies to services that do *not* use the option `ts.client`.
Expand Down Expand Up @@ -353,7 +389,7 @@ Some things to note:
## IMessageType
The `IMessageType` provides the following methods:
The `IMessageType` interface provides the following methods:
- `create(): T`
Expand Down Expand Up @@ -1324,7 +1360,6 @@ benchmark is located in `packages/benchmarks`.
Note that ts-proto doesn't support JSON with `outputJsonMethods=false`. pbf has a very limited feature set.
## Running in the Web Browser
`protobuf-ts` works in the browser. The runtime and generated code is compatible
Expand Down Expand Up @@ -1362,6 +1397,23 @@ for more information.
## Outputting JavaScript
By default, `protobuf-ts` outputs TypeScript files, but can alternatively output
JavaScript for different runtimes. This might save you an additional build
step, for example if you want to publish the generated code as a npm package.
To output JavaScript, simply set the
[plugin option](#the-protoc-plugin) `output_javascript`, which will output
JavaScript for the recommended target. The recommended target will change with
`protobuf-ts` releases. If you want to stick to a specific target, use
`output_javascript_es2015` for example.
By default, the ECMAScript module system is used. If you are stuck with an
older project that still requires CommonJS, set the plugin option
`output_legacy_commonjs`.
## RPC support
`protobuf-ts` provides several options for RPC clients and servers. By default,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ official [language guide](https://developers.google.com/protocol-buffers/docs/ov
- [x] uses standard [TypeScript enums](MANUAL.md#enum-representation)
- [x] runs [in the Web Browser](MANUAL.md#running-in-the-web-browser) and in [Node.js](MANUAL.md#running-in-nodejs)
- [x] uses an [algebraic data type for oneof](MANUAL.md#oneof-representation) groups
- [x] can generate TypeScript [or JavaScript](MANUAL.md#outputting-javascript)


Read the [MANUAL](MANUAL.md) to learn more.
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-framework/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export * from './symbol-table';
export * from './type-names';
export * from './google/protobuf/descriptor';
export * from './google/protobuf/compiler/plugin';
export * from './typescript-check';
export * from './typescript-compile';
export * from './typescript-comments';
export * from './typescript-import-manager';
export * from './typescript-method-from-text';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-framework/src/plugin-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type ResolvedOptions<T extends OptionsSpec> = {
*
*
*/
export abstract class PluginBase<T extends GeneratedFile> {
export abstract class PluginBase<T extends GeneratedFile = GeneratedFile> {


abstract generate(request: CodeGeneratorRequest): Promise<T[]> | T[];
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/spec/protobufts-plugin-long.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getFixtureCodeGeneratorRequest} from "./support/helpers";
import {ProtobuftsPlugin} from "../src/protobufts-plugin";
import {OutFile} from "src/out-file";
import {GeneratedFile} from "@protobuf-ts/plugin-framework/src";

const stringSnippets = [
// Message
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('Generated code for long type', function () {

// Generate typescript code for msg-longs.proto.
// The `parameter` is forwarded to the request.
function generateTypescript(parameter: string): OutFile {
function generateTypescript(parameter: string): GeneratedFile {
let plugin = new ProtobuftsPlugin('test');
let request = getFixtureCodeGeneratorRequest({
parameter,
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/spec/protobufts-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {getFixtureCodeGeneratorRequest} from "./support/helpers";
import {ProtobuftsPlugin} from "../src/protobufts-plugin";
import * as ts from "typescript";
import {setupCompiler} from "@protobuf-ts/plugin-framework";
import {OutFile} from "../src/out-file";


describe('protobuftsPlugin', function () {
Expand Down Expand Up @@ -104,7 +105,7 @@ describe('protobuftsPlugin', function () {
// check each .proto for ts compilation errors
for (let fileDescriptor of request.protoFile) {
it(`${fileDescriptor.name}`, function () {
const file = generatedFiles.find(f => f.fileDescriptor === fileDescriptor);
const file = generatedFiles.find(f => f instanceof OutFile && f.fileDescriptor === fileDescriptor);
expect(file).toBeDefined('Missing associated typescript file!');
const problems = diagnostics.filter(d => d.file?.fileName === file?.getFilename());
if (problems.length > 0) fail(ts.formatDiagnostics(problems, host));
Expand Down
Loading

0 comments on commit 4304289

Please sign in to comment.