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 the option to transpile target files #228

Merged
merged 43 commits into from
Sep 27, 2022
Merged

Conversation

smaye81
Copy link
Member

@smaye81 smaye81 commented Sep 14, 2022

This adds the ability to transpile generated .ts files into .js and/or .d.ts files. Users can provide the following 3 functions to generate code in their plugin:

  • generateTs
  • generateJs
  • generateDts

The generateTs function is required, but the latter two are optional. If one or the other (or both) are not provided, then the framework will auto-transpile the generated TypeScript files to generate the desired code. Note that this is all dependent on the provided target outs. For example, if target=js and no generateJs or generateDts is provided, the framework will only transpile JavaScript files (ignoring declaration files). In essence, the framework will only transpile files if needed and even then, will only transpile the ones being requested.

It is worth noting that transpilation is an expensive process so if performance is a concern, plugin authors should pass all 3 functions as the plugin code generation process is much faster (protoc-gen-es uses this approach).

When auto-transpiling, the framework uses an older, pinned version of TypeScript for stability (at press time 4.5.2). The compiler options used are intentionally lenient so that the odds of generating code under most circumstances are high. If this is not desirable for plugin authors, they also have the option to provide their own transpile function as part of the plugin initialization configuration. If they provide their own, then this transpile function will be invoked instead and the framework's transpilation process will be bypassed.

For an example of implementing a transpiler, see packages/protoplugin/src/ecmascript/transpile.ts.

Copy link
Member

@timostamm timostamm left a comment

Choose a reason for hiding this comment

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

Very excited for this :)

packages/example/package.json Outdated Show resolved Hide resolved
packages/example/package.json Show resolved Hide resolved
packages/protoc-gen-es/src/javascript.ts Outdated Show resolved Hide resolved
packages/protoc-gen-es/src/transpile.ts Outdated Show resolved Hide resolved
packages/protoc-gen-es/src/transpile.ts Outdated Show resolved Hide resolved
packages/protoplugin/src/ecmascript/generated-file.ts Outdated Show resolved Hide resolved
packages/protoplugin/src/ecmascript/generated-file.ts Outdated Show resolved Hide resolved
packages/protoplugin/src/ecmascript/schema.ts Outdated Show resolved Hide resolved
packages/protoplugin/src/create-es-plugin.ts Outdated Show resolved Hide resolved
packages/protoplugin/src/ecmascript/schema.ts Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
packages/protoplugin/package.json Outdated Show resolved Hide resolved
packages/protoplugin/src/create-es-plugin.ts Outdated Show resolved Hide resolved
packages/protoplugin/src/ecmascript/schema.ts Show resolved Hide resolved
packages/protoplugin/src/ecmascript/schema.ts Outdated Show resolved Hide resolved
packages/protoplugin/src/ecmascript/transpile.ts Outdated Show resolved Hide resolved
packages/protoplugin-test/src/initial.test.ts Outdated Show resolved Hide resolved
@smaye81 smaye81 marked this pull request as ready for review September 22, 2022 17:48
@smaye81 smaye81 changed the title [WIP] Allow for providing a transpile function at plugin initialization Add the option to transpile target files Sep 22, 2022
Copy link
Member

@timostamm timostamm left a comment

Choose a reason for hiding this comment

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

This looks pretty great to me, just needs a little bit of tidying up, see the two comments below.

Can you remove packages/protoplugin-test/descriptorset.bin and then add it to .gitignore? I know we check in generated source files, but this binary file is not very useful in git.

packages/protoplugin/src/ecmascript/schema.ts Show resolved Hide resolved
packages/protoplugin/src/ecmascript/transpile.ts Outdated Show resolved Hide resolved
@smaye81 smaye81 merged commit 00c324a into main Sep 27, 2022
@smaye81 smaye81 deleted the sayers/require_ts_only branch September 27, 2022 16:01
smaye81 added a commit that referenced this pull request Oct 25, 2022
This release includes the following:

## 🚨 Breaking Changes 🚨 
- #233 - If you have been inspecting fields (`DescField`) from
createDescriptorSet(), you will need to update your code to use the new
`fieldKind` discriminator. The `kind` property no longer contains values
such as `map_field` and `message_field` for their respective types of
`DescField`. Instead, a new type has been added named `fieldKind`, which
allows you to further identify the kind of field a `DescField`
represents (`map`, `enum`, `message`, etc.)

- #228 - If you have been using `createEcmaScriptPlugin` to create your
own plugin, the signature has changed. Previously, the function accepted
a single generator function for generating your output files. This has
been split up into separate functions for TypeScript (`generateTs`),
JavaScript (`generateJs`), and declaration files (`generateDts`). See
the [plugin
docs](https://github.com/bufbuild/protobuf-es/blob/main/docs/writing_plugins.md)
for more information on usage.

## Enhancements
* Add plugin example for creating a Twirp client by @smaye81 #250
* Export printable type by @fubhy #255
* Extend documentation for PlainMessage and PartialMessage by @timostamm
#248
* Add convenience functions for retrieving custom options by @smaye81
#244
* Clean up descriptor discrimination by @timostamm #233
* Add a plugin option to keep empty files by @timostamm #229
* Add the option to transpile target files by @smaye81 #228

## Bugfixes
* Fix TypeScript file generation bug by @smaye81 #253
* Fix: arbitrary imports by @fubhy #254

## New Contributors
@fubhy made their first contributions in #254 and #255.
smaye81 added a commit that referenced this pull request Nov 11, 2022
smaye81 added a commit that referenced this pull request Nov 11, 2022
This release includes the following:

## 🚨 Breaking Changes 🚨 
- #233 - If you have been inspecting fields (`DescField`) from
createDescriptorSet(), you will need to update your code to use the new
`fieldKind` discriminator. The `kind` property no longer contains values
such as `map_field` and `message_field` for their respective types of
`DescField`. Instead, a new type has been added named `fieldKind`, which
allows you to further identify the kind of field a `DescField`
represents (`map`, `enum`, `message`, etc.)

- #228 - If you have been using `createEcmaScriptPlugin` to create your
own plugin, the signature has changed. Previously, the function accepted
a single generator function for generating your output files. This has
been split up into separate functions for TypeScript (`generateTs`),
JavaScript (`generateJs`), and declaration files (`generateDts`). See
the [plugin
docs](https://github.com/bufbuild/protobuf-es/blob/main/docs/writing_plugins.md)
for more information on usage.

## Enhancements
* Add plugin example for creating a Twirp client by @smaye81 #250
* Export printable type by @fubhy #255
* Extend documentation for PlainMessage and PartialMessage by @timostamm
#248
* Add convenience functions for retrieving custom options by @smaye81
#244
* Clean up descriptor discrimination by @timostamm #233
* Add a plugin option to keep empty files by @timostamm #229
* Add the option to transpile target files by @smaye81 #228

## Bugfixes
* Fix TypeScript file generation bug by @smaye81 #253
* Fix: arbitrary imports by @fubhy #254

## New Contributors
@fubhy made their first contributions in #254 and #255.
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.

2 participants