@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.
- package/README.md +2 -2
- package/dist/cjs/create-es-plugin.d.ts +8 -3
- package/dist/cjs/create-es-plugin.js +19 -133
- package/dist/cjs/ecmascript/export-declaration.d.ts +7 -0
- package/dist/cjs/ecmascript/export-declaration.js +24 -0
- package/dist/cjs/ecmascript/gencommon.d.ts +1 -3
- package/dist/cjs/ecmascript/gencommon.js +1 -75
- package/dist/cjs/ecmascript/generated-file.d.ts +46 -7
- package/dist/cjs/ecmascript/generated-file.js +79 -17
- package/dist/cjs/ecmascript/index.d.ts +21 -6
- package/dist/cjs/ecmascript/index.js +30 -8
- package/dist/cjs/ecmascript/jsdoc.d.ts +8 -0
- package/dist/cjs/ecmascript/jsdoc.js +90 -0
- package/dist/cjs/ecmascript/parameter.d.ts +13 -0
- package/dist/cjs/ecmascript/parameter.js +161 -0
- package/dist/cjs/ecmascript/schema.d.ts +4 -6
- package/dist/cjs/ecmascript/schema.js +21 -33
- package/dist/cjs/ecmascript/transpile.d.ts +1 -1
- package/dist/cjs/ecmascript/transpile.js +4 -1
- package/dist/cjs/index.d.ts +5 -1
- package/dist/esm/create-es-plugin.d.ts +8 -3
- package/dist/esm/create-es-plugin.js +19 -133
- package/dist/esm/ecmascript/export-declaration.d.ts +7 -0
- package/dist/esm/ecmascript/export-declaration.js +20 -0
- package/dist/esm/ecmascript/gencommon.d.ts +1 -3
- package/dist/esm/ecmascript/gencommon.js +0 -72
- package/dist/esm/ecmascript/generated-file.d.ts +46 -7
- package/dist/esm/ecmascript/generated-file.js +79 -17
- package/dist/esm/ecmascript/index.d.ts +21 -6
- package/dist/esm/ecmascript/index.js +23 -2
- package/dist/esm/ecmascript/jsdoc.d.ts +8 -0
- package/dist/esm/ecmascript/jsdoc.js +86 -0
- package/dist/esm/ecmascript/parameter.d.ts +13 -0
- package/dist/esm/ecmascript/parameter.js +157 -0
- package/dist/esm/ecmascript/schema.d.ts +4 -6
- package/dist/esm/ecmascript/schema.js +21 -32
- package/dist/esm/ecmascript/transpile.d.ts +1 -1
- package/dist/esm/ecmascript/transpile.js +4 -1
- package/dist/esm/index.d.ts +5 -1
- package/package.json +2 -2
|
@@ -13,19 +13,41 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.
|
|
16
|
+
exports.createJsDocBlock = exports.makeJsDoc = exports.literalString = exports.findCustomEnumOption = exports.findCustomMessageOption = exports.findCustomScalarOption = exports.getFieldTyping = exports.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.localName = exports.createImportSymbol = exports.reifyWkt = void 0;
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
+
const jsdoc_js_1 = require("./jsdoc.js");
|
|
19
|
+
const gencommon_js_1 = require("./gencommon.js");
|
|
18
20
|
var reify_wkt_js_1 = require("./reify-wkt.js");
|
|
19
21
|
Object.defineProperty(exports, "reifyWkt", { enumerable: true, get: function () { return reify_wkt_js_1.reifyWkt; } });
|
|
22
|
+
var import_symbol_js_1 = require("./import-symbol.js");
|
|
23
|
+
Object.defineProperty(exports, "createImportSymbol", { enumerable: true, get: function () { return import_symbol_js_1.createImportSymbol; } });
|
|
20
24
|
exports.localName = protobuf_1.codegenInfo.localName;
|
|
21
|
-
var
|
|
22
|
-
Object.defineProperty(exports, "
|
|
23
|
-
Object.defineProperty(exports, "
|
|
24
|
-
Object.defineProperty(exports, "
|
|
25
|
-
Object.defineProperty(exports, "getFieldTyping", { enumerable: true, get: function () { return gencommon_js_1.getFieldTyping; } });
|
|
26
|
-
Object.defineProperty(exports, "makeJsDoc", { enumerable: true, get: function () { return gencommon_js_1.makeJsDoc; } });
|
|
27
|
-
Object.defineProperty(exports, "literalString", { enumerable: true, get: function () { return gencommon_js_1.literalString; } });
|
|
25
|
+
var gencommon_js_2 = require("./gencommon.js");
|
|
26
|
+
Object.defineProperty(exports, "getFieldExplicitDefaultValue", { enumerable: true, get: function () { return gencommon_js_2.getFieldExplicitDefaultValue; } });
|
|
27
|
+
Object.defineProperty(exports, "getFieldIntrinsicDefaultValue", { enumerable: true, get: function () { return gencommon_js_2.getFieldIntrinsicDefaultValue; } });
|
|
28
|
+
Object.defineProperty(exports, "getFieldTyping", { enumerable: true, get: function () { return gencommon_js_2.getFieldTyping; } });
|
|
28
29
|
var custom_options_js_1 = require("./custom-options.js");
|
|
29
30
|
Object.defineProperty(exports, "findCustomScalarOption", { enumerable: true, get: function () { return custom_options_js_1.findCustomScalarOption; } });
|
|
30
31
|
Object.defineProperty(exports, "findCustomMessageOption", { enumerable: true, get: function () { return custom_options_js_1.findCustomMessageOption; } });
|
|
31
32
|
Object.defineProperty(exports, "findCustomEnumOption", { enumerable: true, get: function () { return custom_options_js_1.findCustomEnumOption; } });
|
|
33
|
+
/**
|
|
34
|
+
* @deprecated Please use GeneratedFile.string() instead
|
|
35
|
+
*/
|
|
36
|
+
function literalString(value) {
|
|
37
|
+
return (0, gencommon_js_1.literalString)(value);
|
|
38
|
+
}
|
|
39
|
+
exports.literalString = literalString;
|
|
40
|
+
/**
|
|
41
|
+
* @deprecated Please use GeneratedFile.jsDoc() instead
|
|
42
|
+
*/
|
|
43
|
+
function makeJsDoc(desc, indentation = "") {
|
|
44
|
+
return (0, jsdoc_js_1.createJsDocBlock)(desc, indentation).toString();
|
|
45
|
+
}
|
|
46
|
+
exports.makeJsDoc = makeJsDoc;
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated Please use GeneratedFile.jsDoc() instead
|
|
49
|
+
*/
|
|
50
|
+
function createJsDocBlock(text, indentation = "") {
|
|
51
|
+
return (0, jsdoc_js_1.createJsDocBlock)(text, indentation).toString();
|
|
52
|
+
}
|
|
53
|
+
exports.createJsDocBlock = createJsDocBlock;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnyDesc, DescExtension, DescFile } from "@bufbuild/protobuf";
|
|
2
|
+
export type JSDocBlock = {
|
|
3
|
+
readonly kind: "es_jsdoc";
|
|
4
|
+
text: string;
|
|
5
|
+
indentation?: string;
|
|
6
|
+
toString(): string;
|
|
7
|
+
};
|
|
8
|
+
export declare function createJsDocBlock(textOrDesc: string | Exclude<AnyDesc, DescFile | DescExtension>, indentation?: string): JSDocBlock;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021-2023 Buf Technologies, Inc.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.createJsDocBlock = void 0;
|
|
17
|
+
function createJsDocBlock(textOrDesc, indentation) {
|
|
18
|
+
const text = typeof textOrDesc == "string" ? textOrDesc : createTextForDesc(textOrDesc);
|
|
19
|
+
return {
|
|
20
|
+
kind: "es_jsdoc",
|
|
21
|
+
text,
|
|
22
|
+
indentation,
|
|
23
|
+
toString() {
|
|
24
|
+
if (text.trim().length == 0) {
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
let lines = text.split("\n");
|
|
28
|
+
if (lines.length === 0) {
|
|
29
|
+
return "";
|
|
30
|
+
}
|
|
31
|
+
lines = lines.map((l) => l.split("*/").join("*\\/"));
|
|
32
|
+
lines = lines.map((l) => (l.length > 0 ? " " + l : l));
|
|
33
|
+
const i = indentation !== null && indentation !== void 0 ? indentation : "";
|
|
34
|
+
return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
exports.createJsDocBlock = createJsDocBlock;
|
|
39
|
+
function createTextForDesc(desc) {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
const comments = desc.getComments();
|
|
42
|
+
let text = "";
|
|
43
|
+
if (comments.leading !== undefined) {
|
|
44
|
+
text += comments.leading;
|
|
45
|
+
if (text.endsWith("\n")) {
|
|
46
|
+
text = text.substring(0, text.length - 1);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (comments.trailing !== undefined) {
|
|
50
|
+
if (text.length > 0) {
|
|
51
|
+
text += "\n\n";
|
|
52
|
+
}
|
|
53
|
+
text += comments.trailing;
|
|
54
|
+
if (text.endsWith("\n")) {
|
|
55
|
+
text = text.substring(0, text.length - 1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (text.length > 0) {
|
|
59
|
+
text += "\n\n";
|
|
60
|
+
}
|
|
61
|
+
text = text
|
|
62
|
+
.split("\n")
|
|
63
|
+
.map((line) => (line.startsWith(" ") ? line.substring(1) : line))
|
|
64
|
+
.join("\n");
|
|
65
|
+
switch (desc.kind) {
|
|
66
|
+
case "enum_value":
|
|
67
|
+
text += `@generated from enum value: ${desc.declarationString()};`;
|
|
68
|
+
break;
|
|
69
|
+
case "field":
|
|
70
|
+
text += `@generated from field: ${desc.declarationString()};`;
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
text += `@generated from ${desc.toString()}`;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
let deprecated = desc.deprecated;
|
|
77
|
+
switch (desc.kind) {
|
|
78
|
+
case "enum":
|
|
79
|
+
case "message":
|
|
80
|
+
case "service":
|
|
81
|
+
deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
if (deprecated) {
|
|
87
|
+
text += "\n@deprecated";
|
|
88
|
+
}
|
|
89
|
+
return text;
|
|
90
|
+
}
|
|
@@ -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,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021-2023 Buf Technologies, Inc.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.parseParameter = void 0;
|
|
17
|
+
const error_js_1 = require("../error.js");
|
|
18
|
+
function parseParameter(parameter, parseExtraOption) {
|
|
19
|
+
let targets = ["js", "dts"];
|
|
20
|
+
let tsNocheck = true;
|
|
21
|
+
let bootstrapWkt = false;
|
|
22
|
+
let keepEmptyFiles = false;
|
|
23
|
+
const rewriteImports = [];
|
|
24
|
+
let importExtension = ".js";
|
|
25
|
+
let jsImportStyle = "module";
|
|
26
|
+
const rawParameters = [];
|
|
27
|
+
for (const { key, value, raw } of splitParameter(parameter)) {
|
|
28
|
+
// Whether this key/value plugin parameter pair should be
|
|
29
|
+
// printed to the generated file preamble
|
|
30
|
+
let sanitize = false;
|
|
31
|
+
switch (key) {
|
|
32
|
+
case "target":
|
|
33
|
+
targets = [];
|
|
34
|
+
for (const rawTarget of value.split("+")) {
|
|
35
|
+
switch (rawTarget) {
|
|
36
|
+
case "js":
|
|
37
|
+
case "ts":
|
|
38
|
+
case "dts":
|
|
39
|
+
if (targets.indexOf(rawTarget) < 0) {
|
|
40
|
+
targets.push(rawTarget);
|
|
41
|
+
}
|
|
42
|
+
break;
|
|
43
|
+
default:
|
|
44
|
+
throw new error_js_1.PluginOptionError(raw);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
value.split("+");
|
|
48
|
+
break;
|
|
49
|
+
case "ts_nocheck":
|
|
50
|
+
switch (value) {
|
|
51
|
+
case "true":
|
|
52
|
+
case "1":
|
|
53
|
+
tsNocheck = true;
|
|
54
|
+
break;
|
|
55
|
+
case "false":
|
|
56
|
+
case "0":
|
|
57
|
+
tsNocheck = false;
|
|
58
|
+
break;
|
|
59
|
+
default:
|
|
60
|
+
throw new error_js_1.PluginOptionError(raw);
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
case "bootstrap_wkt":
|
|
64
|
+
switch (value) {
|
|
65
|
+
case "true":
|
|
66
|
+
case "1":
|
|
67
|
+
bootstrapWkt = true;
|
|
68
|
+
break;
|
|
69
|
+
case "false":
|
|
70
|
+
case "0":
|
|
71
|
+
bootstrapWkt = false;
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
throw new error_js_1.PluginOptionError(raw);
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case "rewrite_imports": {
|
|
78
|
+
const parts = value.split(":");
|
|
79
|
+
if (parts.length !== 2) {
|
|
80
|
+
throw new error_js_1.PluginOptionError(raw, "must be in the form of <pattern>:<target>");
|
|
81
|
+
}
|
|
82
|
+
const [pattern, target] = parts;
|
|
83
|
+
rewriteImports.push({ pattern, target });
|
|
84
|
+
// rewrite_imports can be noisy and is more of an implementation detail
|
|
85
|
+
// so we strip it out of the preamble
|
|
86
|
+
sanitize = true;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case "import_extension": {
|
|
90
|
+
importExtension = value === "none" ? "" : value;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "js_import_style":
|
|
94
|
+
switch (value) {
|
|
95
|
+
case "module":
|
|
96
|
+
jsImportStyle = value;
|
|
97
|
+
break;
|
|
98
|
+
case "legacy_commonjs":
|
|
99
|
+
jsImportStyle = value;
|
|
100
|
+
break;
|
|
101
|
+
default:
|
|
102
|
+
throw new error_js_1.PluginOptionError(raw);
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
case "keep_empty_files": {
|
|
106
|
+
switch (value) {
|
|
107
|
+
case "true":
|
|
108
|
+
case "1":
|
|
109
|
+
keepEmptyFiles = true;
|
|
110
|
+
break;
|
|
111
|
+
case "false":
|
|
112
|
+
case "0":
|
|
113
|
+
keepEmptyFiles = false;
|
|
114
|
+
break;
|
|
115
|
+
default:
|
|
116
|
+
throw new error_js_1.PluginOptionError(raw);
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
default:
|
|
121
|
+
if (parseExtraOption === undefined) {
|
|
122
|
+
throw new error_js_1.PluginOptionError(raw);
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
parseExtraOption(key, value);
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
throw new error_js_1.PluginOptionError(raw, e);
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
if (!sanitize) {
|
|
133
|
+
rawParameters.push(raw);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const sanitizedParameter = rawParameters.join(",");
|
|
137
|
+
return {
|
|
138
|
+
targets,
|
|
139
|
+
tsNocheck,
|
|
140
|
+
bootstrapWkt,
|
|
141
|
+
rewriteImports,
|
|
142
|
+
importExtension,
|
|
143
|
+
jsImportStyle,
|
|
144
|
+
keepEmptyFiles,
|
|
145
|
+
sanitizedParameter,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
exports.parseParameter = parseParameter;
|
|
149
|
+
function splitParameter(parameter) {
|
|
150
|
+
if (parameter == undefined) {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
return parameter.split(",").map((raw) => {
|
|
154
|
+
const i = raw.indexOf("=");
|
|
155
|
+
return {
|
|
156
|
+
key: i === -1 ? raw : raw.substring(0, i),
|
|
157
|
+
value: i === -1 ? "" : raw.substring(i + 1),
|
|
158
|
+
raw,
|
|
159
|
+
};
|
|
160
|
+
});
|
|
161
|
+
}
|
|
@@ -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 {
|
|
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,
|
|
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 {};
|
|
@@ -13,66 +13,54 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.
|
|
16
|
+
exports.createSchema = void 0;
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
18
|
const generated_file_js_1 = require("./generated-file.js");
|
|
19
19
|
const runtime_imports_js_1 = require("./runtime-imports.js");
|
|
20
20
|
const import_symbol_js_1 = require("./import-symbol.js");
|
|
21
21
|
const import_path_js_1 = require("./import-path.js");
|
|
22
|
-
function createSchema(request,
|
|
22
|
+
function createSchema(request, parameter, pluginName, pluginVersion) {
|
|
23
23
|
const descriptorSet = (0, protobuf_1.createDescriptorSet)(request.protoFile);
|
|
24
24
|
const filesToGenerate = findFilesToGenerate(descriptorSet, request);
|
|
25
|
-
const runtime = (0, runtime_imports_js_1.createRuntimeImports)(bootstrapWkt);
|
|
25
|
+
const runtime = (0, runtime_imports_js_1.createRuntimeImports)(parameter.bootstrapWkt);
|
|
26
26
|
const createTypeImport = (desc) => {
|
|
27
27
|
const name = protobuf_1.codegenInfo.localName(desc);
|
|
28
|
-
const from = (0, import_path_js_1.makeImportPath)(desc.file, bootstrapWkt, filesToGenerate);
|
|
28
|
+
const from = (0, import_path_js_1.makeImportPath)(desc.file, parameter.bootstrapWkt, filesToGenerate);
|
|
29
29
|
return (0, import_symbol_js_1.createImportSymbol)(name, from);
|
|
30
30
|
};
|
|
31
|
+
let target;
|
|
31
32
|
const generatedFiles = [];
|
|
32
|
-
|
|
33
|
-
targets,
|
|
33
|
+
return {
|
|
34
|
+
targets: parameter.targets,
|
|
34
35
|
runtime,
|
|
35
36
|
proto: request,
|
|
36
37
|
files: filesToGenerate,
|
|
37
38
|
allFiles: descriptorSet.files,
|
|
38
39
|
generateFile(name) {
|
|
39
|
-
|
|
40
|
+
if (target === undefined) {
|
|
41
|
+
throw new Error("prepareGenerate() must be called before generateFile()");
|
|
42
|
+
}
|
|
43
|
+
const genFile = (0, generated_file_js_1.createGeneratedFile)(name, (0, import_path_js_1.deriveImportPath)(name), target === "js" ? parameter.jsImportStyle : "module", // ts and dts always use import/export, only js may use commonjs
|
|
44
|
+
(importPath) => (0, import_path_js_1.rewriteImportPath)(importPath, parameter.rewriteImports, parameter.importExtension), createTypeImport, runtime, {
|
|
40
45
|
pluginName,
|
|
41
46
|
pluginVersion,
|
|
42
|
-
pluginParameter,
|
|
43
|
-
tsNocheck,
|
|
44
|
-
}
|
|
47
|
+
pluginParameter: parameter.sanitizedParameter,
|
|
48
|
+
tsNocheck: parameter.tsNocheck,
|
|
49
|
+
});
|
|
45
50
|
generatedFiles.push(genFile);
|
|
46
51
|
return genFile;
|
|
47
52
|
},
|
|
48
|
-
};
|
|
49
|
-
return {
|
|
50
|
-
schema,
|
|
51
53
|
getFileInfo() {
|
|
52
|
-
return generatedFiles
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return [fileInfo];
|
|
59
|
-
});
|
|
54
|
+
return generatedFiles
|
|
55
|
+
.map((f) => f.getFileInfo())
|
|
56
|
+
.filter((fi) => parameter.keepEmptyFiles || fi.content.length > 0);
|
|
57
|
+
},
|
|
58
|
+
prepareGenerate(newTarget) {
|
|
59
|
+
target = newTarget;
|
|
60
60
|
},
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
exports.createSchema = createSchema;
|
|
64
|
-
function toResponse(files) {
|
|
65
|
-
return new protobuf_1.CodeGeneratorResponse({
|
|
66
|
-
supportedFeatures: protobuf_1.protoInt64.parse(protobuf_1.CodeGeneratorResponse_Feature.PROTO3_OPTIONAL),
|
|
67
|
-
file: files.map((f) => {
|
|
68
|
-
if (f.preamble !== undefined) {
|
|
69
|
-
f.content = f.preamble + "\n" + f.content;
|
|
70
|
-
}
|
|
71
|
-
return f;
|
|
72
|
-
}),
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
exports.toResponse = toResponse;
|
|
76
64
|
function findFilesToGenerate(descriptorSet, request) {
|
|
77
65
|
const missing = request.fileToGenerate.filter((fileToGenerate) => descriptorSet.files.every((file) => fileToGenerate !== file.name + ".proto"));
|
|
78
66
|
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[];
|
|
@@ -82,8 +82,11 @@ function createTranspiler(options, files) {
|
|
|
82
82
|
host: host.compilerHost,
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
|
-
function transpile(files, transpileJs, transpileDts) {
|
|
85
|
+
function transpile(files, transpileJs, transpileDts, jsImportStyle) {
|
|
86
86
|
const options = Object.assign(Object.assign({}, defaultOptions), { declaration: transpileDts, emitDeclarationOnly: transpileDts && !transpileJs });
|
|
87
|
+
if (jsImportStyle == "legacy_commonjs") {
|
|
88
|
+
options.module = typescript_1.default.ModuleKind.CommonJS;
|
|
89
|
+
}
|
|
87
90
|
// Create the transpiler (a ts.Program object)
|
|
88
91
|
const program = createTranspiler(options, files);
|
|
89
92
|
const results = [];
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -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;
|
|
@@ -45,14 +45,19 @@ interface PluginInit {
|
|
|
45
45
|
*/
|
|
46
46
|
generateDts?: (schema: Schema, target: "dts") => void;
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* An optional function which will transpile a given set of files.
|
|
49
49
|
*
|
|
50
|
-
* This
|
|
50
|
+
* This function is meant to be used in place of either generateJs,
|
|
51
51
|
* generateDts, or both. However, those functions will take precedence.
|
|
52
52
|
* This means that if generateJs, generateDts, and this transpile function
|
|
53
53
|
* are all provided, this transpile function will be ignored.
|
|
54
|
+
*
|
|
55
|
+
* If jsImportStyle is "module" (the standard behavior), the function is
|
|
56
|
+
* expected to use ECMAScript module import and export statements when
|
|
57
|
+
* transpiling to JS. If jsImportStyle is "legacy_commonjs", the function is
|
|
58
|
+
* expected to use CommonJs require() and exports when transpiling to JS.
|
|
54
59
|
*/
|
|
55
|
-
transpile?: (files: FileInfo[], transpileJs: boolean, transpileDts: boolean) => FileInfo[];
|
|
60
|
+
transpile?: (files: FileInfo[], transpileJs: boolean, transpileDts: boolean, jsImportStyle: "module" | "legacy_commonjs") => FileInfo[];
|
|
56
61
|
}
|
|
57
62
|
/**
|
|
58
63
|
* Create a new code generator plugin for ECMAScript.
|