Skip to content

Commit

Permalink
Added support for cjs output (#303)
Browse files Browse the repository at this point in the history
Fixes #297 

Add support for CommonJS with the new plugin option:

### `js_import_style`

By default,
[protoc-gen-connect-query](https://www.npmjs.com/package/@connectrpc/protoc-gen-connect-query)(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.

#### Example buf.gen.yaml

```
version: v1
plugins:
  - plugin: es
    out: src/gen
    opt: js_import_style=legacy_commonjs
  - plugin: connect-query
    out: src/gen
    opt: js_import_style=legacy_commonjs
 ```
  • Loading branch information
paul-sachs committed Dec 14, 2023
1 parent 036bab5 commit 7ee2820
Show file tree
Hide file tree
Showing 16 changed files with 855 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const config = {
},
},
{
files: ["**/eliza/*", "**/gen/**"], // generated code
files: ["**/eliza/*", "**/gen/**", "**/snapshots/**"], // generated code
rules: {
"eslint-comments/no-unused-enable": "off",
"eslint-comments/no-unused-disable": "off",
Expand Down
14 changes: 14 additions & 0 deletions packages/protoc-gen-connect-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ Unfortunately, not all bundlers and tools have caught up yet, and Deno requires

This option exists for other plugins but is not applicable to `protoc-gen-connect-query` because, unlike most other plugins, it does not generate a maximum of one output file for every input proto file. Instead, it generates one output file per service. If you provide a valid proto file that contains no services, `protoc-gen-connect-query` will have no output.

### `js_import_style`

By default, [protoc-gen-connect-query](https://www.npmjs.com/package/@connectrpc/protoc-gen-connect-query)
(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.

Possible values:

- `js_import_style=module` generate ECMAScript `import` / `export` statements -
the default behavior.
- `js_import_style=legacy_commonjs` generate CommonJS `require()` calls.

## Example Generated Code

See [`eliza.proto`](../../examples/react/basic/eliza.proto) for example inputs, and look [here](../../examples/react/basic/src/gen) to see the outputs those files generate.
2 changes: 1 addition & 1 deletion packages/protoc-gen-connect-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"clean": "rm -rf dist",
"build": "pnpm clean && tsc --project tsconfig.build.json",
"format": "prettier . --write --ignore-path ./.eslintignore && eslint . --fix && license-header",
"generate": "rm -rf snapshots/gen && cd snapshots && buf generate buf.build/connectrpc/eliza:8b8b971d6fde4dc8ba5d96f9fda7d53c",
"generate": "rm -rf snapshots/gen* && cd snapshots && buf generate buf.build/connectrpc/eliza:8b8b971d6fde4dc8ba5d96f9fda7d53c",
"test": "tsc --declaration --declarationDir .type-dump --emitDeclarationOnly"
},
"preferUnplugged": true,
Expand Down
32 changes: 24 additions & 8 deletions packages/protoc-gen-connect-query/snapshots/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
# buf.gen.yaml defines a local generation template.
# For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml
version: v1
plugins:
# js
- plugin: es
out: gen
out: gen_js
- plugin: connect-query
path: ../bin/protoc-gen-connect-query
out: gen_js

# cjs
- plugin: es
out: gen_cjs
opt:
- target=ts
- js_import_style=legacy_commonjs
- plugin: connect-query
path: ../bin/protoc-gen-connect-query
out: gen_cjs
opt:
- js_import_style=legacy_commonjs
- ts_nocheck=false # we only set this for our tests

# ts
- plugin: es
out: gen_ts
opt:
- target=ts
- plugin: connect-query
path: ../bin/protoc-gen-connect-query
out: gen
out: gen_ts
opt:
- target=ts+dts+js
- import_extension=none
- ts_nocheck=false
- target=ts
- ts_nocheck=false # we only set this for our tests
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// @generated by protoc-gen-connect-query v1.0.0 with parameter "target=ts+dts+js,import_extension=none,ts_nocheck=false"
// @generated by protoc-gen-connect-query v1.0.0 with parameter "js_import_style=legacy_commonjs,ts_nocheck=false"
// @generated from file connectrpc/eliza/v1/eliza.proto (package connectrpc.eliza.v1, syntax proto3)
/* eslint-disable */

import { SayRequest, SayResponse } from "./eliza_pb";
import { SayRequest, SayResponse } from "./eliza_pb.js";
import { MethodKind } from "@bufbuild/protobuf";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021-2023 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// @generated by protoc-gen-connect-query v1.0.0 with parameter "js_import_style=legacy_commonjs,ts_nocheck=false"
// @generated from file connectrpc/eliza/v1/eliza.proto (package connectrpc.eliza.v1, syntax proto3)
/* eslint-disable */

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

const { MethodKind } = require("@bufbuild/protobuf");
const { SayRequest, SayResponse } = require("./eliza_pb.js");

/**
* Say is a unary RPC. Eliza responds to the prompt with a single sentence.
*
* @generated from rpc connectrpc.eliza.v1.ElizaService.Say
*/
const say = {
localName: "say",
name: "Say",
kind: MethodKind.Unary,
I: SayRequest,
O: SayResponse,
service: {
typeName: "connectrpc.eliza.v1.ElizaService",
},
};

exports.say = say;
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
// Copyright 2021-2023 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// @generated by protoc-gen-es v1.6.0 with parameter "js_import_style=legacy_commonjs"
// @generated from file connectrpc/eliza/v1/eliza.proto (package connectrpc.eliza.v1, syntax proto3)
/* eslint-disable */
// @ts-nocheck

import type {
BinaryReadOptions,
FieldList,
JsonReadOptions,
JsonValue,
PartialMessage,
PlainMessage,
} from "@bufbuild/protobuf";
import { Message, proto3 } from "@bufbuild/protobuf";

/**
* SayRequest is a single-sentence request.
*
* @generated from message connectrpc.eliza.v1.SayRequest
*/
export declare class SayRequest extends Message<SayRequest> {
/**
* @generated from field: string sentence = 1;
*/
sentence: string;

constructor(data?: PartialMessage<SayRequest>);

static readonly runtime: typeof proto3;
static readonly typeName = "connectrpc.eliza.v1.SayRequest";
static readonly fields: FieldList;

static fromBinary(
bytes: Uint8Array,
options?: Partial<BinaryReadOptions>,
): SayRequest;

static fromJson(
jsonValue: JsonValue,
options?: Partial<JsonReadOptions>,
): SayRequest;

static fromJsonString(
jsonString: string,
options?: Partial<JsonReadOptions>,
): SayRequest;

static equals(
a: SayRequest | PlainMessage<SayRequest> | undefined,
b: SayRequest | PlainMessage<SayRequest> | undefined,
): boolean;
}

/**
* SayResponse is a single-sentence response.
*
* @generated from message connectrpc.eliza.v1.SayResponse
*/
export declare class SayResponse extends Message<SayResponse> {
/**
* @generated from field: string sentence = 1;
*/
sentence: string;

constructor(data?: PartialMessage<SayResponse>);

static readonly runtime: typeof proto3;
static readonly typeName = "connectrpc.eliza.v1.SayResponse";
static readonly fields: FieldList;

static fromBinary(
bytes: Uint8Array,
options?: Partial<BinaryReadOptions>,
): SayResponse;

static fromJson(
jsonValue: JsonValue,
options?: Partial<JsonReadOptions>,
): SayResponse;

static fromJsonString(
jsonString: string,
options?: Partial<JsonReadOptions>,
): SayResponse;

static equals(
a: SayResponse | PlainMessage<SayResponse> | undefined,
b: SayResponse | PlainMessage<SayResponse> | undefined,
): boolean;
}

/**
* ConverseRequest is a single sentence request sent as part of a
* back-and-forth conversation.
*
* @generated from message connectrpc.eliza.v1.ConverseRequest
*/
export declare class ConverseRequest extends Message<ConverseRequest> {
/**
* @generated from field: string sentence = 1;
*/
sentence: string;

constructor(data?: PartialMessage<ConverseRequest>);

static readonly runtime: typeof proto3;
static readonly typeName = "connectrpc.eliza.v1.ConverseRequest";
static readonly fields: FieldList;

static fromBinary(
bytes: Uint8Array,
options?: Partial<BinaryReadOptions>,
): ConverseRequest;

static fromJson(
jsonValue: JsonValue,
options?: Partial<JsonReadOptions>,
): ConverseRequest;

static fromJsonString(
jsonString: string,
options?: Partial<JsonReadOptions>,
): ConverseRequest;

static equals(
a: ConverseRequest | PlainMessage<ConverseRequest> | undefined,
b: ConverseRequest | PlainMessage<ConverseRequest> | undefined,
): boolean;
}

/**
* ConverseResponse is a single sentence response sent in answer to a
* ConverseRequest.
*
* @generated from message connectrpc.eliza.v1.ConverseResponse
*/
export declare class ConverseResponse extends Message<ConverseResponse> {
/**
* @generated from field: string sentence = 1;
*/
sentence: string;

constructor(data?: PartialMessage<ConverseResponse>);

static readonly runtime: typeof proto3;
static readonly typeName = "connectrpc.eliza.v1.ConverseResponse";
static readonly fields: FieldList;

static fromBinary(
bytes: Uint8Array,
options?: Partial<BinaryReadOptions>,
): ConverseResponse;

static fromJson(
jsonValue: JsonValue,
options?: Partial<JsonReadOptions>,
): ConverseResponse;

static fromJsonString(
jsonString: string,
options?: Partial<JsonReadOptions>,
): ConverseResponse;

static equals(
a: ConverseResponse | PlainMessage<ConverseResponse> | undefined,
b: ConverseResponse | PlainMessage<ConverseResponse> | undefined,
): boolean;
}

/**
* IntroduceRequest asks Eliza to introduce itself to the named user.
*
* @generated from message connectrpc.eliza.v1.IntroduceRequest
*/
export declare class IntroduceRequest extends Message<IntroduceRequest> {
/**
* @generated from field: string name = 1;
*/
name: string;

constructor(data?: PartialMessage<IntroduceRequest>);

static readonly runtime: typeof proto3;
static readonly typeName = "connectrpc.eliza.v1.IntroduceRequest";
static readonly fields: FieldList;

static fromBinary(
bytes: Uint8Array,
options?: Partial<BinaryReadOptions>,
): IntroduceRequest;

static fromJson(
jsonValue: JsonValue,
options?: Partial<JsonReadOptions>,
): IntroduceRequest;

static fromJsonString(
jsonString: string,
options?: Partial<JsonReadOptions>,
): IntroduceRequest;

static equals(
a: IntroduceRequest | PlainMessage<IntroduceRequest> | undefined,
b: IntroduceRequest | PlainMessage<IntroduceRequest> | undefined,
): boolean;
}

/**
* IntroduceResponse is one sentence of Eliza's introductory monologue.
*
* @generated from message connectrpc.eliza.v1.IntroduceResponse
*/
export declare class IntroduceResponse extends Message<IntroduceResponse> {
/**
* @generated from field: string sentence = 1;
*/
sentence: string;

constructor(data?: PartialMessage<IntroduceResponse>);

static readonly runtime: typeof proto3;
static readonly typeName = "connectrpc.eliza.v1.IntroduceResponse";
static readonly fields: FieldList;

static fromBinary(
bytes: Uint8Array,
options?: Partial<BinaryReadOptions>,
): IntroduceResponse;

static fromJson(
jsonValue: JsonValue,
options?: Partial<JsonReadOptions>,
): IntroduceResponse;

static fromJsonString(
jsonString: string,
options?: Partial<JsonReadOptions>,
): IntroduceResponse;

static equals(
a: IntroduceResponse | PlainMessage<IntroduceResponse> | undefined,
b: IntroduceResponse | PlainMessage<IntroduceResponse> | undefined,
): boolean;
}

0 comments on commit 7ee2820

Please sign in to comment.