Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CommonJS support as a plugin option #648

Merged
merged 11 commits into from Dec 7, 2023
Merged

Conversation

timostamm
Copy link
Member

@timostamm timostamm commented Dec 7, 2023

This PR adds support for generating JavaScript for the CommonJS module system.

When the plugin option js_import_style is set to legacy_commonjs, generated JavaScript will use require() to import, and the exports variable to export.

Here is a shortened example, generated from example.proto:

// @generated by protoc-gen-es v1.5.1 with parameter "target=js+dts,js_import_style=legacy_commonjs"
// @generated from file extra/example.proto (package docs, syntax proto3)

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

const { proto3 } = require("@bufbuild/protobuf");

/**
 * @generated from message docs.User
 */
const User = ...

exports.User = User;

As you can see, strict mode is enabled. For best interoperability, we define __esModule.

For plugin authors:

  • To support CommonJS output, your code must use exportDecl instead of printing an export statement. For example, use f.print(f.exportDecl("abstract class", "MyClass"), " {}") instead of f.print("export abstract class MyClass {}").
  • In case you are overriding the built-in transpilation, your transpile function needs to handle the new 4th argument jsImportStyle to support CommonJS output.

Closes #587.

@timostamm timostamm requested review from smaye81, srikrsna-buf, a team, jhump and mfridman and removed request for a team and jhump December 7, 2023 12:51
docs/writing_plugins.md Outdated Show resolved Hide resolved
@smaye81
Copy link
Member

smaye81 commented Dec 7, 2023

@timostamm
Copy link
Member Author

Do you also want to update the docs here? https://github.com/bufbuild/protobuf-es/blob/main/docs/generated_code.md?plain=1#L81-L86

Thanks! Updated in 526fe50.

Copy link
Member

@mfridman mfridman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@timostamm timostamm merged commit 8f13a7d into main Dec 7, 2023
5 checks passed
@timostamm timostamm deleted the tstamm/cjs-plugin-option branch December 7, 2023 17:49
@smaye81 smaye81 mentioned this pull request Dec 12, 2023
smaye81 added a commit that referenced this pull request Dec 12, 2023
## What's Changed
* Add CommonJS support as a plugin option by @timostamm in
[#648](#648)
* Add `GeneratedFile.jsDoc()` and other features to
`@bufbuild/protoplugin` by @timostamm in
[#649](#649)

**Full Changelog**:
v1.5.1...v1.6.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CommonJS support as a plugin option
3 participants