@bufbuild/protoplugin 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/create-es-plugin.d.ts +8 -3
  3. package/dist/cjs/create-es-plugin.js +19 -133
  4. package/dist/cjs/ecmascript/export-declaration.d.ts +7 -0
  5. package/dist/cjs/ecmascript/export-declaration.js +24 -0
  6. package/dist/cjs/ecmascript/gencommon.d.ts +1 -3
  7. package/dist/cjs/ecmascript/gencommon.js +1 -75
  8. package/dist/cjs/ecmascript/generated-file.d.ts +46 -7
  9. package/dist/cjs/ecmascript/generated-file.js +79 -17
  10. package/dist/cjs/ecmascript/index.d.ts +21 -6
  11. package/dist/cjs/ecmascript/index.js +30 -8
  12. package/dist/cjs/ecmascript/jsdoc.d.ts +8 -0
  13. package/dist/cjs/ecmascript/jsdoc.js +90 -0
  14. package/dist/cjs/ecmascript/parameter.d.ts +13 -0
  15. package/dist/cjs/ecmascript/parameter.js +161 -0
  16. package/dist/cjs/ecmascript/schema.d.ts +4 -6
  17. package/dist/cjs/ecmascript/schema.js +21 -33
  18. package/dist/cjs/ecmascript/transpile.d.ts +1 -1
  19. package/dist/cjs/ecmascript/transpile.js +4 -1
  20. package/dist/cjs/index.d.ts +5 -1
  21. package/dist/esm/create-es-plugin.d.ts +8 -3
  22. package/dist/esm/create-es-plugin.js +19 -133
  23. package/dist/esm/ecmascript/export-declaration.d.ts +7 -0
  24. package/dist/esm/ecmascript/export-declaration.js +20 -0
  25. package/dist/esm/ecmascript/gencommon.d.ts +1 -3
  26. package/dist/esm/ecmascript/gencommon.js +0 -72
  27. package/dist/esm/ecmascript/generated-file.d.ts +46 -7
  28. package/dist/esm/ecmascript/generated-file.js +79 -17
  29. package/dist/esm/ecmascript/index.d.ts +21 -6
  30. package/dist/esm/ecmascript/index.js +23 -2
  31. package/dist/esm/ecmascript/jsdoc.d.ts +8 -0
  32. package/dist/esm/ecmascript/jsdoc.js +86 -0
  33. package/dist/esm/ecmascript/parameter.d.ts +13 -0
  34. package/dist/esm/ecmascript/parameter.js +157 -0
  35. package/dist/esm/ecmascript/schema.d.ts +4 -6
  36. package/dist/esm/ecmascript/schema.js +21 -32
  37. package/dist/esm/ecmascript/transpile.d.ts +1 -1
  38. package/dist/esm/ecmascript/transpile.js +4 -1
  39. package/dist/esm/index.d.ts +5 -1
  40. package/package.json +2 -2
@@ -0,0 +1,86 @@
1
+ // Copyright 2021-2023 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ export function createJsDocBlock(textOrDesc, indentation) {
15
+ const text = typeof textOrDesc == "string" ? textOrDesc : createTextForDesc(textOrDesc);
16
+ return {
17
+ kind: "es_jsdoc",
18
+ text,
19
+ indentation,
20
+ toString() {
21
+ if (text.trim().length == 0) {
22
+ return "";
23
+ }
24
+ let lines = text.split("\n");
25
+ if (lines.length === 0) {
26
+ return "";
27
+ }
28
+ lines = lines.map((l) => l.split("*/").join("*\\/"));
29
+ lines = lines.map((l) => (l.length > 0 ? " " + l : l));
30
+ const i = indentation !== null && indentation !== void 0 ? indentation : "";
31
+ return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
32
+ },
33
+ };
34
+ }
35
+ function createTextForDesc(desc) {
36
+ var _a, _b;
37
+ const comments = desc.getComments();
38
+ let text = "";
39
+ if (comments.leading !== undefined) {
40
+ text += comments.leading;
41
+ if (text.endsWith("\n")) {
42
+ text = text.substring(0, text.length - 1);
43
+ }
44
+ }
45
+ if (comments.trailing !== undefined) {
46
+ if (text.length > 0) {
47
+ text += "\n\n";
48
+ }
49
+ text += comments.trailing;
50
+ if (text.endsWith("\n")) {
51
+ text = text.substring(0, text.length - 1);
52
+ }
53
+ }
54
+ if (text.length > 0) {
55
+ text += "\n\n";
56
+ }
57
+ text = text
58
+ .split("\n")
59
+ .map((line) => (line.startsWith(" ") ? line.substring(1) : line))
60
+ .join("\n");
61
+ switch (desc.kind) {
62
+ case "enum_value":
63
+ text += `@generated from enum value: ${desc.declarationString()};`;
64
+ break;
65
+ case "field":
66
+ text += `@generated from field: ${desc.declarationString()};`;
67
+ break;
68
+ default:
69
+ text += `@generated from ${desc.toString()}`;
70
+ break;
71
+ }
72
+ let deprecated = desc.deprecated;
73
+ switch (desc.kind) {
74
+ case "enum":
75
+ case "message":
76
+ case "service":
77
+ deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
78
+ break;
79
+ default:
80
+ break;
81
+ }
82
+ if (deprecated) {
83
+ text += "\n@deprecated";
84
+ }
85
+ return text;
86
+ }
@@ -0,0 +1,13 @@
1
+ import { Target } from "./target.js";
2
+ import { RewriteImports } from "./import-path.js";
3
+ export interface ParsedParameter {
4
+ targets: Target[];
5
+ tsNocheck: boolean;
6
+ bootstrapWkt: boolean;
7
+ keepEmptyFiles: boolean;
8
+ rewriteImports: RewriteImports;
9
+ importExtension: string;
10
+ jsImportStyle: "module" | "legacy_commonjs";
11
+ sanitizedParameter: string;
12
+ }
13
+ export declare function parseParameter(parameter: string | undefined, parseExtraOption: ((key: string, value: string) => void) | undefined): ParsedParameter;
@@ -0,0 +1,157 @@
1
+ // Copyright 2021-2023 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { PluginOptionError } from "../error.js";
15
+ export function parseParameter(parameter, parseExtraOption) {
16
+ let targets = ["js", "dts"];
17
+ let tsNocheck = true;
18
+ let bootstrapWkt = false;
19
+ let keepEmptyFiles = false;
20
+ const rewriteImports = [];
21
+ let importExtension = ".js";
22
+ let jsImportStyle = "module";
23
+ const rawParameters = [];
24
+ for (const { key, value, raw } of splitParameter(parameter)) {
25
+ // Whether this key/value plugin parameter pair should be
26
+ // printed to the generated file preamble
27
+ let sanitize = false;
28
+ switch (key) {
29
+ case "target":
30
+ targets = [];
31
+ for (const rawTarget of value.split("+")) {
32
+ switch (rawTarget) {
33
+ case "js":
34
+ case "ts":
35
+ case "dts":
36
+ if (targets.indexOf(rawTarget) < 0) {
37
+ targets.push(rawTarget);
38
+ }
39
+ break;
40
+ default:
41
+ throw new PluginOptionError(raw);
42
+ }
43
+ }
44
+ value.split("+");
45
+ break;
46
+ case "ts_nocheck":
47
+ switch (value) {
48
+ case "true":
49
+ case "1":
50
+ tsNocheck = true;
51
+ break;
52
+ case "false":
53
+ case "0":
54
+ tsNocheck = false;
55
+ break;
56
+ default:
57
+ throw new PluginOptionError(raw);
58
+ }
59
+ break;
60
+ case "bootstrap_wkt":
61
+ switch (value) {
62
+ case "true":
63
+ case "1":
64
+ bootstrapWkt = true;
65
+ break;
66
+ case "false":
67
+ case "0":
68
+ bootstrapWkt = false;
69
+ break;
70
+ default:
71
+ throw new PluginOptionError(raw);
72
+ }
73
+ break;
74
+ case "rewrite_imports": {
75
+ const parts = value.split(":");
76
+ if (parts.length !== 2) {
77
+ throw new PluginOptionError(raw, "must be in the form of <pattern>:<target>");
78
+ }
79
+ const [pattern, target] = parts;
80
+ rewriteImports.push({ pattern, target });
81
+ // rewrite_imports can be noisy and is more of an implementation detail
82
+ // so we strip it out of the preamble
83
+ sanitize = true;
84
+ break;
85
+ }
86
+ case "import_extension": {
87
+ importExtension = value === "none" ? "" : value;
88
+ break;
89
+ }
90
+ case "js_import_style":
91
+ switch (value) {
92
+ case "module":
93
+ jsImportStyle = value;
94
+ break;
95
+ case "legacy_commonjs":
96
+ jsImportStyle = value;
97
+ break;
98
+ default:
99
+ throw new PluginOptionError(raw);
100
+ }
101
+ break;
102
+ case "keep_empty_files": {
103
+ switch (value) {
104
+ case "true":
105
+ case "1":
106
+ keepEmptyFiles = true;
107
+ break;
108
+ case "false":
109
+ case "0":
110
+ keepEmptyFiles = false;
111
+ break;
112
+ default:
113
+ throw new PluginOptionError(raw);
114
+ }
115
+ break;
116
+ }
117
+ default:
118
+ if (parseExtraOption === undefined) {
119
+ throw new PluginOptionError(raw);
120
+ }
121
+ try {
122
+ parseExtraOption(key, value);
123
+ }
124
+ catch (e) {
125
+ throw new PluginOptionError(raw, e);
126
+ }
127
+ break;
128
+ }
129
+ if (!sanitize) {
130
+ rawParameters.push(raw);
131
+ }
132
+ }
133
+ const sanitizedParameter = rawParameters.join(",");
134
+ return {
135
+ targets,
136
+ tsNocheck,
137
+ bootstrapWkt,
138
+ rewriteImports,
139
+ importExtension,
140
+ jsImportStyle,
141
+ keepEmptyFiles,
142
+ sanitizedParameter,
143
+ };
144
+ }
145
+ function splitParameter(parameter) {
146
+ if (parameter == undefined) {
147
+ return [];
148
+ }
149
+ return parameter.split(",").map((raw) => {
150
+ const i = raw.indexOf("=");
151
+ return {
152
+ key: i === -1 ? raw : raw.substring(0, i),
153
+ value: i === -1 ? "" : raw.substring(i + 1),
154
+ raw,
155
+ };
156
+ });
157
+ }
@@ -1,9 +1,8 @@
1
1
  import type { CodeGeneratorRequest, DescFile } from "@bufbuild/protobuf";
2
- import { CodeGeneratorResponse } from "@bufbuild/protobuf";
3
2
  import type { FileInfo, GeneratedFile } from "./generated-file.js";
4
3
  import { RuntimeImports } from "./runtime-imports.js";
5
4
  import type { Target } from "./target.js";
6
- import { RewriteImports } from "./import-path.js";
5
+ import { ParsedParameter } from "./parameter";
7
6
  /**
8
7
  * Schema describes the files and types that the plugin is requested to
9
8
  * generate.
@@ -34,10 +33,9 @@ export interface Schema {
34
33
  */
35
34
  readonly proto: CodeGeneratorRequest;
36
35
  }
37
- interface SchemaController {
38
- schema: Schema;
36
+ interface SchemaController extends Schema {
39
37
  getFileInfo: () => FileInfo[];
38
+ prepareGenerate(target: Target): void;
40
39
  }
41
- export declare function createSchema(request: CodeGeneratorRequest, targets: Target[], tsNocheck: boolean, bootstrapWkt: boolean, rewriteImports: RewriteImports, importExtension: string, keepEmptyFiles: boolean, pluginName: string, pluginVersion: string, pluginParameter: string): SchemaController;
42
- export declare function toResponse(files: FileInfo[]): CodeGeneratorResponse;
40
+ export declare function createSchema(request: CodeGeneratorRequest, parameter: ParsedParameter, pluginName: string, pluginVersion: string): SchemaController;
43
41
  export {};
@@ -11,63 +11,52 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { CodeGeneratorResponse, CodeGeneratorResponse_Feature, codegenInfo, createDescriptorSet, protoInt64, } from "@bufbuild/protobuf";
14
+ import { codegenInfo, createDescriptorSet } from "@bufbuild/protobuf";
15
15
  import { createGeneratedFile } from "./generated-file.js";
16
16
  import { createRuntimeImports } from "./runtime-imports.js";
17
17
  import { createImportSymbol } from "./import-symbol.js";
18
18
  import { deriveImportPath, makeImportPath, rewriteImportPath, } from "./import-path.js";
19
- export function createSchema(request, targets, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles, pluginName, pluginVersion, pluginParameter) {
19
+ export function createSchema(request, parameter, pluginName, pluginVersion) {
20
20
  const descriptorSet = createDescriptorSet(request.protoFile);
21
21
  const filesToGenerate = findFilesToGenerate(descriptorSet, request);
22
- const runtime = createRuntimeImports(bootstrapWkt);
22
+ const runtime = createRuntimeImports(parameter.bootstrapWkt);
23
23
  const createTypeImport = (desc) => {
24
24
  const name = codegenInfo.localName(desc);
25
- const from = makeImportPath(desc.file, bootstrapWkt, filesToGenerate);
25
+ const from = makeImportPath(desc.file, parameter.bootstrapWkt, filesToGenerate);
26
26
  return createImportSymbol(name, from);
27
27
  };
28
+ let target;
28
29
  const generatedFiles = [];
29
- const schema = {
30
- targets,
30
+ return {
31
+ targets: parameter.targets,
31
32
  runtime,
32
33
  proto: request,
33
34
  files: filesToGenerate,
34
35
  allFiles: descriptorSet.files,
35
36
  generateFile(name) {
36
- const genFile = createGeneratedFile(name, deriveImportPath(name), (importPath) => rewriteImportPath(importPath, rewriteImports, importExtension), createTypeImport, runtime, {
37
+ if (target === undefined) {
38
+ throw new Error("prepareGenerate() must be called before generateFile()");
39
+ }
40
+ const genFile = createGeneratedFile(name, deriveImportPath(name), target === "js" ? parameter.jsImportStyle : "module", // ts and dts always use import/export, only js may use commonjs
41
+ (importPath) => rewriteImportPath(importPath, parameter.rewriteImports, parameter.importExtension), createTypeImport, runtime, {
37
42
  pluginName,
38
43
  pluginVersion,
39
- pluginParameter,
40
- tsNocheck,
41
- }, keepEmptyFiles);
44
+ pluginParameter: parameter.sanitizedParameter,
45
+ tsNocheck: parameter.tsNocheck,
46
+ });
42
47
  generatedFiles.push(genFile);
43
48
  return genFile;
44
49
  },
45
- };
46
- return {
47
- schema,
48
50
  getFileInfo() {
49
- return generatedFiles.flatMap((file) => {
50
- const fileInfo = file.getFileInfo();
51
- // undefined is returned if the file has no content
52
- if (!fileInfo) {
53
- return [];
54
- }
55
- return [fileInfo];
56
- });
51
+ return generatedFiles
52
+ .map((f) => f.getFileInfo())
53
+ .filter((fi) => parameter.keepEmptyFiles || fi.content.length > 0);
54
+ },
55
+ prepareGenerate(newTarget) {
56
+ target = newTarget;
57
57
  },
58
58
  };
59
59
  }
60
- export function toResponse(files) {
61
- return new CodeGeneratorResponse({
62
- supportedFeatures: protoInt64.parse(CodeGeneratorResponse_Feature.PROTO3_OPTIONAL),
63
- file: files.map((f) => {
64
- if (f.preamble !== undefined) {
65
- f.content = f.preamble + "\n" + f.content;
66
- }
67
- return f;
68
- }),
69
- });
70
- }
71
60
  function findFilesToGenerate(descriptorSet, request) {
72
61
  const missing = request.fileToGenerate.filter((fileToGenerate) => descriptorSet.files.every((file) => fileToGenerate !== file.name + ".proto"));
73
62
  if (missing.length) {
@@ -1,2 +1,2 @@
1
1
  import type { FileInfo } from "./generated-file.js";
2
- export declare function transpile(files: FileInfo[], transpileJs: boolean, transpileDts: boolean): FileInfo[];
2
+ export declare function transpile(files: FileInfo[], transpileJs: boolean, transpileDts: boolean, jsImportStyle: "module" | "legacy_commonjs"): FileInfo[];
@@ -76,8 +76,11 @@ function createTranspiler(options, files) {
76
76
  host: host.compilerHost,
77
77
  });
78
78
  }
79
- export function transpile(files, transpileJs, transpileDts) {
79
+ export function transpile(files, transpileJs, transpileDts, jsImportStyle) {
80
80
  const options = Object.assign(Object.assign({}, defaultOptions), { declaration: transpileDts, emitDeclarationOnly: transpileDts && !transpileJs });
81
+ if (jsImportStyle == "legacy_commonjs") {
82
+ options.module = ts.ModuleKind.CommonJS;
83
+ }
81
84
  // Create the transpiler (a ts.Program object)
82
85
  const program = createTranspiler(options, files);
83
86
  const results = [];
@@ -1,4 +1,8 @@
1
+ import { Schema as SchemaInternal } from "./ecmascript/schema.js";
1
2
  export { Plugin } from "./plugin.js";
2
- export { Schema } from "./ecmascript/schema.js";
3
3
  export { runNodeJs } from "./run-node.js";
4
4
  export { createEcmaScriptPlugin } from "./create-es-plugin.js";
5
+ /**
6
+ * @deprecated Please use Schema from @bufbuild/protoplugin/ecmascript instead
7
+ */
8
+ export type Schema = SchemaInternal;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoplugin",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "Helps to create your own Protocol Buffers code generators.",
6
6
  "repository": {
@@ -47,7 +47,7 @@
47
47
  }
48
48
  },
49
49
  "dependencies": {
50
- "@bufbuild/protobuf": "1.5.1",
50
+ "@bufbuild/protobuf": "1.6.0",
51
51
  "@typescript/vfs": "^1.4.0",
52
52
  "typescript": "4.5.2"
53
53
  },