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

V2: Remove keep_empty_files option and change default to always generate #842

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions docs/writing_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ Protobuf-ES: Writing Plugins
========================
Code generator plugins can be created using the npm packages [@bufbuild/protoplugin](https://npmjs.com/package/@bufbuild/protoplugin) and [@bufbuild/protobuf](https://npmjs.com/package/@bufbuild/protobuf). This is a detailed overview of the process of writing a plugin.

- [Introduction](#introduction)
- [Writing a plugin](#writing-a-plugin)
- [Installing the plugin framework and dependencies](#installing-the-plugin-framework-and-dependencies)
- [Setting up your plugin](#setting-up-your-plugin)
- [Providing generator functions](#providing-generator-functions)
- [Overriding transpilation](#overriding-transpilation)
- [Generating a file](#generating-a-file)
- [Walking through the schema](#walking-through-the-schema)
- [Printing to a generated file](#printing-to-a-generated-file)
- [As a variadic function](#as-a-variadic-function)
- [As a template literal tagged function](#as-a-template-literal-tagged-function)
- [Importing](#importing)
- [Importing from an NPM package](#importing-from-an-npm-package)
- [Importing from `protoc-gen-es` generated code](#importing-from-protoc-gen-es-generated-code)
- [Importing from the `@bufbuild/protobuf` runtime](#importing-from-the-bufbuildprotobuf-runtime)
- [Type-only imports](#type-only-imports)
- [Why use `f.import()`?](#why-use-fimport)
- [Exporting](#exporting)
- [Parsing plugin options](#parsing-plugin-options)
- [Using custom Protobuf options](#using-custom-protobuf-options)
- [Testing](#testing)
- [Examples](#examples)
- [Protobuf-ES: Writing Plugins](#protobuf-es-writing-plugins)
- [Introduction](#introduction)
- [Writing a plugin](#writing-a-plugin)
- [Installing the plugin framework and dependencies](#installing-the-plugin-framework-and-dependencies)
- [Setting up your plugin](#setting-up-your-plugin)
- [Providing generator functions](#providing-generator-functions)
- [Overriding transpilation](#overriding-transpilation)
- [Generating a file](#generating-a-file)
- [Walking through the schema](#walking-through-the-schema)
- [Printing to a generated file](#printing-to-a-generated-file)
- [As a variadic function](#as-a-variadic-function)
- [As a template literal tagged function](#as-a-template-literal-tagged-function)
- [Importing](#importing)
- [Importing from an NPM package](#importing-from-an-npm-package)
- [Importing from `protoc-gen-es` generated code](#importing-from-protoc-gen-es-generated-code)
- [Importing from the @bufbuild/protobuf runtime](#importing-from-the-bufbuildprotobuf-runtime)
- [Type-only imports](#type-only-imports)
- [Why use `f.import()`?](#why-use-fimport)
- [Exporting](#exporting)
- [Parsing plugin options](#parsing-plugin-options)
- [Using custom Protobuf options](#using-custom-protobuf-options)
- [Testing](#testing)
- [Examples](#examples)

## Introduction

Expand Down Expand Up @@ -369,7 +370,7 @@ automatically generate the correct export for CommonJS.

### Parsing plugin options

The plugin framework recognizes a set of pre-defined key/value pairs that can be passed to all plugins when executed (i.e. `target`, `keep_empty_files`, etc.), but if your plugin needs to be passed additional parameters, you can specify a `parseOption` function as part of your plugin initialization.
The plugin framework recognizes a set of pre-defined key/value pairs that can be passed to all plugins when executed (i.e. `target`, `import_extension`, etc.), but if your plugin needs to be passed additional parameters, you can specify a `parseOption` function as part of your plugin initialization.

```ts
parseOption(key: string, value: string | undefined): void;
Expand Down
9 changes: 0 additions & 9 deletions packages/protoc-gen-es/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ Possible values:
the default behavior.
- `js_import_style=legacy_commonjs` generate CommonJS `require()` calls.

### `keep_empty_files=true`

By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
(and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin))
omit empty files from the plugin output. This option disables pruning of
empty files, to allow for smooth interoperation with Bazel and similar
tooling that requires all output files to be declared ahead of time.
Unless you use Bazel, it is very unlikely that you need this option.

### `ts_nocheck=true`

[protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es) generates
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2021-2024 Buf Technologies, Inc.
//
// 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-twirp-es v2.0.0-alpha.2 with parameter "target=ts"
// @generated from file customoptions/default_host.proto (package customoptions, syntax proto3)
/* eslint-disable */

82 changes: 0 additions & 82 deletions packages/protoplugin-test/src/keep_empty_files.test.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/protoplugin/src/ecmascript/generated-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export interface GeneratedFile {
*
* The preamble is always placed at the very top of the generated file,
* above import statements.
*
* A file with a preamble but no other content is still considered empty,
* and will not be generated unless the plugin option keep_empty_files=true
* is set.
*/
preamble(file: DescFile): void;

Expand Down
18 changes: 0 additions & 18 deletions packages/protoplugin/src/ecmascript/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface ParsedParameter {
targets: Target[];
tsNocheck: boolean;
bootstrapWkt: boolean;
keepEmptyFiles: boolean;
rewriteImports: RewriteImports;
importExtension: string;
jsImportStyle: "module" | "legacy_commonjs";
Expand All @@ -34,7 +33,6 @@ export function parseParameter(
let targets: Target[] = ["js", "dts"];
let tsNocheck = false;
let bootstrapWkt = false;
let keepEmptyFiles = false;
const rewriteImports: RewriteImports = [];
let importExtension = "";
let jsImportStyle: "module" | "legacy_commonjs" = "module";
Expand Down Expand Up @@ -120,21 +118,6 @@ export function parseParameter(
throw new PluginOptionError(raw);
}
break;
case "keep_empty_files": {
Copy link
Member

Choose a reason for hiding this comment

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

Users who set this option in their buf.gen.yaml will get an error with this change, and they'll wonder why. Can we provide a helpful error message in this case? I guess that's a bit tricky, because we'll no longer support this option, but plugin users can't use it either if we error on it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this should be handled as part of the upgrade guide.

switch (value) {
case "true":
case "1":
keepEmptyFiles = true;
break;
case "false":
case "0":
keepEmptyFiles = false;
break;
default:
throw new PluginOptionError(raw);
}
break;
}
default:
if (parseExtraOption === undefined) {
throw new PluginOptionError(raw);
Expand All @@ -160,7 +143,6 @@ export function parseParameter(
rewriteImports,
importExtension,
jsImportStyle,
keepEmptyFiles,
sanitizedParameter,
};
}
Expand Down
4 changes: 1 addition & 3 deletions packages/protoplugin/src/ecmascript/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ export function createSchema(
return genFile;
},
getFileInfo() {
return generatedFiles
.map((f) => f.getFileInfo())
.filter((fi) => parameter.keepEmptyFiles || fi.content.length > 0);
return generatedFiles.map((f) => f.getFileInfo());
},
prepareGenerate(newTarget) {
target = newTarget;
Expand Down