Skip to content

Commit

Permalink
Support CommonJS in protoc-gen-connect-es (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Dec 13, 2023
1 parent b3ff7c8 commit 8dde71e
Show file tree
Hide file tree
Showing 25 changed files with 2,375 additions and 892 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -144,14 +144,14 @@ $(GEN)/connect: node_modules/.bin/protoc-gen-es packages/connect/buf.gen.yaml $(

$(GEN)/connect-web-test: node_modules/.bin/protoc-gen-es $(BUILD)/protoc-gen-connect-es packages/connect-web-test/buf.gen.yaml Makefile
rm -rf packages/connect-web-test/src/gen/*
npm run -w packages/connect-web-test generate https://github.com/connectrpc/conformance.git#ref=$(CONFORMANCE_VERSION),subdir=proto
npm run -w packages/connect-web-test generate https://github.com/connectrpc/conformance.git#tag=$(CONFORMANCE_VERSION),subdir=proto
npm run -w packages/connect-web-test generate buf.build/connectrpc/eliza:8bde2b90ec0a7f23df3de5824bed0b6ea2043305
@mkdir -p $(@D)
@touch $(@)

$(GEN)/connect-node-test: node_modules/.bin/protoc-gen-es $(BUILD)/protoc-gen-connect-es packages/connect-node-test/buf.gen.yaml Makefile
rm -rf packages/connect-node-test/src/gen/*
npm run -w packages/connect-node-test generate https://github.com/connectrpc/conformance.git#ref=$(CONFORMANCE_VERSION),subdir=proto
npm run -w packages/connect-node-test generate https://github.com/connectrpc/conformance.git#tag=$(CONFORMANCE_VERSION),subdir=proto
@mkdir -p $(@D)
@touch $(@)

Expand Down
3,159 changes: 2,321 additions & 838 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/connect-conformance/package.json
Expand Up @@ -14,7 +14,7 @@
"test:node:client": "./bin/connectconformance --mode client --conf conformance-node.yaml -v ./bin/conformancenodeclient"
},
"dependencies": {
"@bufbuild/protobuf": "^1.4.2",
"@bufbuild/protobuf": "^1.6.0",
"@connectrpc/connect": "1.1.4",
"@connectrpc/connect-node": "1.1.4",
"node-forge": "^1.3.1",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/connect-node-test/src/gen/server/v1/server_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/connect-web-bench/README.md
Expand Up @@ -10,5 +10,5 @@ it like a web server would usually do.

| code generator | bundle size | minified | compressed |
|----------------|-------------------:|-----------------------:|---------------------:|
| connect | 115,808 b | 50,395 b | 13,566 b |
| connect | 116,769 b | 50,660 b | 13,623 b |
| grpc-web | 415,212 b | 300,936 b | 53,420 b |
4 changes: 2 additions & 2 deletions packages/connect-web-bench/package.json
Expand Up @@ -7,8 +7,8 @@
"clean": "rm -rf README.md src/gen"
},
"dependencies": {
"@bufbuild/protobuf": "^1.4.2",
"@bufbuild/protoc-gen-es": "^1.4.2",
"@bufbuild/protobuf": "^1.6.0",
"@bufbuild/protoc-gen-es": "^1.6.0",
"@connectrpc/connect-web": "1.1.4",
"brotli": "^1.3.3",
"esbuild": "^0.19.8",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/connect-web-test/src/gen/server/v1/server_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/connect/package.json
Expand Up @@ -88,7 +88,7 @@
"@bufbuild/protobuf": "^1.4.2"
},
"devDependencies": {
"@bufbuild/protoc-gen-es": "^1.4.2",
"@bufbuild/protoc-gen-es": "^1.6.0",
"@types/jasmine": "^5.0.0",
"jasmine": "^5.1.0",
"undici": "^5.26.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/connect/src/protocol-grpc/gen/status_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/example/package.json
Expand Up @@ -19,7 +19,7 @@
},
"devDependencies": {
"@bufbuild/buf": "^1.27.2",
"@bufbuild/protoc-gen-es": "^1.4.2",
"@bufbuild/protoc-gen-es": "^1.6.0",
"@connectrpc/protoc-gen-connect-es": "^1.1.4",
"@types/express": "^4.17.18",
"esbuild": "^0.19.8",
Expand Down
2 changes: 1 addition & 1 deletion packages/example/src/gen/eliza_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions packages/protoc-gen-connect-es/README.md
Expand Up @@ -125,6 +125,18 @@ in import paths with the given value. For example, set
- `import_extension=none` to remove the `.js` extension
- `import_extension=.ts` to replace the `.js` extension with `.ts`

### `js_import_style`

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

Possible values:
- `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`

Expand Down
6 changes: 3 additions & 3 deletions packages/protoc-gen-connect-es/package.json
Expand Up @@ -20,12 +20,12 @@
},
"preferUnplugged": true,
"dependencies": {
"@bufbuild/protobuf": "^1.4.2",
"@bufbuild/protoplugin": "^1.4.1"
"@bufbuild/protobuf": "^1.6.0",
"@bufbuild/protoplugin": "^1.6.0"
},
"peerDependencies": {
"@connectrpc/connect": "1.1.4",
"@bufbuild/protoc-gen-es": "^1.3.3"
"@bufbuild/protoc-gen-es": "^1.6.0"
},
"peerDependenciesMeta": {
"@connectrpc/connect": {
Expand Down
16 changes: 6 additions & 10 deletions packages/protoc-gen-connect-es/src/declaration.ts
Expand Up @@ -15,11 +15,7 @@
import type { DescService } from "@bufbuild/protobuf";
import { MethodIdempotency, MethodKind } from "@bufbuild/protobuf";
import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
import {
literalString,
makeJsDoc,
localName,
} from "@bufbuild/protoplugin/ecmascript";
import { localName } from "@bufbuild/protoplugin/ecmascript";

export function generateDts(schema: Schema) {
for (const protoFile of schema.files) {
Expand All @@ -34,14 +30,14 @@ export function generateDts(schema: Schema) {
// prettier-ignore
function generateService(schema: Schema, f: GeneratedFile, service: DescService) {
const { MethodKind: rtMethodKind, MethodIdempotency: rtMethodIdempotency } = schema.runtime;
f.print(makeJsDoc(service));
f.print("export declare const ", localName(service), ": {");
f.print(` readonly typeName: `, literalString(service.typeName), `,`);
f.print(f.jsDoc(service));
f.print(f.exportDecl("declare const", localName(service)), ": {");
f.print(` readonly typeName: `, f.string(service.typeName), `,`);
f.print(" readonly methods: {");
for (const method of service.methods) {
f.print(makeJsDoc(method, " "));
f.print(f.jsDoc(method, " "));
f.print(" readonly ", localName(method), ": {");
f.print(` readonly name: `, literalString(method.name), `,`);
f.print(` readonly name: `, f.string(method.name), `,`);
f.print(" readonly I: typeof ", method.input, ",");
f.print(" readonly O: typeof ", method.output, ",");
f.print(" readonly kind: ", rtMethodKind, ".", MethodKind[method.methodKind], ",");
Expand Down
16 changes: 6 additions & 10 deletions packages/protoc-gen-connect-es/src/javascript.ts
Expand Up @@ -15,11 +15,7 @@
import type { DescService } from "@bufbuild/protobuf";
import { MethodIdempotency, MethodKind } from "@bufbuild/protobuf";
import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
import {
literalString,
makeJsDoc,
localName,
} from "@bufbuild/protoplugin/ecmascript";
import { localName } from "@bufbuild/protoplugin/ecmascript";

export function generateJs(schema: Schema) {
for (const protoFile of schema.files) {
Expand All @@ -34,14 +30,14 @@ export function generateJs(schema: Schema) {
// prettier-ignore
function generateService(schema: Schema, f: GeneratedFile, service: DescService) {
const { MethodKind: rtMethodKind, MethodIdempotency: rtMethodIdempotency } = schema.runtime;
f.print(makeJsDoc(service));
f.print("export const ", localName(service), " = {");
f.print(` typeName: `, literalString(service.typeName), `,`);
f.print(f.jsDoc(service));
f.print(f.exportDecl("const", localName(service)), " = {");
f.print(` typeName: `, f.string(service.typeName), `,`);
f.print(" methods: {");
for (const method of service.methods) {
f.print(makeJsDoc(method, " "));
f.print(f.jsDoc(method, " "));
f.print(" ", localName(method), ": {");
f.print(` name: `, literalString(method.name), `,`);
f.print(` name: `, f.string(method.name), `,`);
f.print(" I: ", method.input, ",");
f.print(" O: ", method.output, ",");
f.print(" kind: ", rtMethodKind, ".", MethodKind[method.methodKind], ",");
Expand Down
16 changes: 6 additions & 10 deletions packages/protoc-gen-connect-es/src/typescript.ts
Expand Up @@ -15,11 +15,7 @@
import type { DescService } from "@bufbuild/protobuf";
import { MethodIdempotency, MethodKind } from "@bufbuild/protobuf";
import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
import {
literalString,
makeJsDoc,
localName,
} from "@bufbuild/protoplugin/ecmascript";
import { localName } from "@bufbuild/protoplugin/ecmascript";

export function generateTs(schema: Schema) {
for (const protoFile of schema.files) {
Expand All @@ -39,14 +35,14 @@ function generateService(
) {
const { MethodKind: rtMethodKind, MethodIdempotency: rtMethodIdempotency } =
schema.runtime;
f.print(makeJsDoc(service));
f.print("export const ", localName(service), " = {");
f.print(` typeName: `, literalString(service.typeName), `,`);
f.print(f.jsDoc(service));
f.print(f.exportDecl("const", localName(service)), " = {");
f.print(` typeName: `, f.string(service.typeName), `,`);
f.print(" methods: {");
for (const method of service.methods) {
f.print(makeJsDoc(method, " "));
f.print(f.jsDoc(method, " "));
f.print(" ", localName(method), ": {");
f.print(` name: `, literalString(method.name), `,`);
f.print(` name: `, f.string(method.name), `,`);
f.print(" I: ", method.input, ",");
f.print(" O: ", method.output, ",");
f.print(
Expand Down

0 comments on commit 8dde71e

Please sign in to comment.