@bufbuild/protoplugin 0.2.1 → 0.4.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/dist/cjs/create-es-plugin.js +15 -3
- package/dist/cjs/ecmascript/gencommon.js +1 -1
- package/dist/cjs/ecmascript/generated-file.js +32 -9
- package/dist/cjs/ecmascript/import-path.js +15 -3
- package/dist/cjs/ecmascript/runtime-imports.js +1 -0
- package/dist/cjs/ecmascript/schema.js +3 -4
- package/dist/esm/create-es-plugin.js +15 -3
- package/dist/esm/ecmascript/gencommon.js +1 -1
- package/dist/esm/ecmascript/generated-file.js +32 -9
- package/dist/esm/ecmascript/import-path.js +15 -3
- package/dist/esm/ecmascript/runtime-imports.js +1 -0
- package/dist/esm/ecmascript/schema.js +4 -5
- package/dist/types/ecmascript/generated-file.d.ts +9 -2
- package/dist/types/ecmascript/import-path.d.ts +3 -2
- package/dist/types/ecmascript/runtime-imports.d.ts +1 -0
- package/dist/types/ecmascript/schema.d.ts +2 -2
- package/package.json +2 -2
|
@@ -30,8 +30,8 @@ function createEcmaScriptPlugin(init) {
|
|
|
30
30
|
version: init.version,
|
|
31
31
|
run(req) {
|
|
32
32
|
var _a;
|
|
33
|
-
const { targets, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles, } = parseParameter(req.parameter, init.parseOption);
|
|
34
|
-
const { schema, getFileInfo } = (0, schema_js_1.createSchema)(req, targets, init.name, init.version, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles);
|
|
33
|
+
const { targets, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles, } = parseParameter(req.parameter, init.parseOption);
|
|
34
|
+
const { schema, getFileInfo } = (0, schema_js_1.createSchema)(req, targets, init.name, init.version, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles);
|
|
35
35
|
const targetTs = schema.targets.includes("ts");
|
|
36
36
|
const targetJs = schema.targets.includes("js");
|
|
37
37
|
const targetDts = schema.targets.includes("dts");
|
|
@@ -103,6 +103,7 @@ function parseParameter(parameter, parseOption) {
|
|
|
103
103
|
let bootstrapWkt = false;
|
|
104
104
|
let keepEmptyFiles = false;
|
|
105
105
|
const rewriteImports = [];
|
|
106
|
+
let importExtension = ".js";
|
|
106
107
|
for (const { key, value } of splitParameter(parameter)) {
|
|
107
108
|
switch (key) {
|
|
108
109
|
case "target":
|
|
@@ -159,6 +160,10 @@ function parseParameter(parameter, parseOption) {
|
|
|
159
160
|
rewriteImports.push({ pattern, target });
|
|
160
161
|
break;
|
|
161
162
|
}
|
|
163
|
+
case "import_extension": {
|
|
164
|
+
importExtension = value === "none" ? "" : value;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
162
167
|
case "keep_empty_files": {
|
|
163
168
|
switch (value) {
|
|
164
169
|
case "true":
|
|
@@ -187,7 +192,14 @@ function parseParameter(parameter, parseOption) {
|
|
|
187
192
|
break;
|
|
188
193
|
}
|
|
189
194
|
}
|
|
190
|
-
return {
|
|
195
|
+
return {
|
|
196
|
+
targets,
|
|
197
|
+
tsNocheck,
|
|
198
|
+
bootstrapWkt,
|
|
199
|
+
rewriteImports,
|
|
200
|
+
importExtension,
|
|
201
|
+
keepEmptyFiles,
|
|
202
|
+
};
|
|
191
203
|
}
|
|
192
204
|
function splitParameter(parameter) {
|
|
193
205
|
if (parameter == undefined) {
|
|
@@ -48,7 +48,7 @@ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck)
|
|
|
48
48
|
builder.push(`syntax ${file.syntax})\n`);
|
|
49
49
|
builder.push("/* eslint-disable */\n");
|
|
50
50
|
if (tsNoCheck) {
|
|
51
|
-
builder.push("
|
|
51
|
+
builder.push("// @ts-nocheck\n");
|
|
52
52
|
}
|
|
53
53
|
builder.push("\n");
|
|
54
54
|
writeLeadingComments(file.getPackageComments());
|
|
@@ -17,15 +17,28 @@ exports.createGeneratedFile = void 0;
|
|
|
17
17
|
const import_symbol_js_1 = require("./import-symbol.js");
|
|
18
18
|
const gencommon_js_1 = require("./gencommon.js");
|
|
19
19
|
const import_path_js_1 = require("./import-path.js");
|
|
20
|
-
function createGeneratedFile(name, importPath, createTypeImport, runtimeImports, preambleSettings, keepEmpty) {
|
|
20
|
+
function createGeneratedFile(name, importPath, rewriteImportPath, createTypeImport, runtimeImports, preambleSettings, keepEmpty) {
|
|
21
21
|
let preamble;
|
|
22
22
|
const el = [];
|
|
23
23
|
return {
|
|
24
24
|
preamble(file) {
|
|
25
25
|
preamble = (0, gencommon_js_1.makeFilePreamble)(file, preambleSettings.pluginName, preambleSettings.pluginVersion, preambleSettings.parameter, preambleSettings.tsNocheck);
|
|
26
26
|
},
|
|
27
|
-
print(...
|
|
28
|
-
|
|
27
|
+
print(printableOrFragments, ...rest) {
|
|
28
|
+
let printables;
|
|
29
|
+
if (printableOrFragments != null &&
|
|
30
|
+
Object.prototype.hasOwnProperty.call(printableOrFragments, "raw")) {
|
|
31
|
+
// If called with a tagged template literal
|
|
32
|
+
printables = buildPrintablesFromFragments(printableOrFragments, rest);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
// If called with just an array of Printables
|
|
36
|
+
printables =
|
|
37
|
+
printableOrFragments != null
|
|
38
|
+
? [printableOrFragments, ...rest]
|
|
39
|
+
: rest;
|
|
40
|
+
}
|
|
41
|
+
printableToEl(printables, el, createTypeImport, runtimeImports);
|
|
29
42
|
el.push("\n");
|
|
30
43
|
},
|
|
31
44
|
export(name) {
|
|
@@ -39,7 +52,7 @@ function createGeneratedFile(name, importPath, createTypeImport, runtimeImports,
|
|
|
39
52
|
return createTypeImport(typeOrName);
|
|
40
53
|
},
|
|
41
54
|
getFileInfo() {
|
|
42
|
-
const content = elToContent(el, importPath);
|
|
55
|
+
const content = elToContent(el, importPath, rewriteImportPath);
|
|
43
56
|
if (!keepEmpty && content.length === 0) {
|
|
44
57
|
return;
|
|
45
58
|
}
|
|
@@ -52,11 +65,11 @@ function createGeneratedFile(name, importPath, createTypeImport, runtimeImports,
|
|
|
52
65
|
};
|
|
53
66
|
}
|
|
54
67
|
exports.createGeneratedFile = createGeneratedFile;
|
|
55
|
-
function elToContent(el, importerPath) {
|
|
68
|
+
function elToContent(el, importerPath, rewriteImportPath) {
|
|
56
69
|
const c = [];
|
|
57
|
-
const symbolToIdentifier = processImports(el, importerPath, (typeOnly, from, names) => {
|
|
70
|
+
const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
|
|
58
71
|
const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
|
|
59
|
-
const what = `{${p.join(", ")}}`;
|
|
72
|
+
const what = `{ ${p.join(", ")} }`;
|
|
60
73
|
if (typeOnly) {
|
|
61
74
|
c.push(`import type ${what} from ${(0, gencommon_js_1.literalString)(from)};\n`);
|
|
62
75
|
}
|
|
@@ -119,7 +132,17 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
|
|
|
119
132
|
}
|
|
120
133
|
}
|
|
121
134
|
}
|
|
122
|
-
function
|
|
135
|
+
function buildPrintablesFromFragments(fragments, values) {
|
|
136
|
+
const printables = [];
|
|
137
|
+
fragments.forEach((fragment, i) => {
|
|
138
|
+
printables.push(fragment);
|
|
139
|
+
if (fragments.length - 1 !== i) {
|
|
140
|
+
printables.push(values[i]);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return printables;
|
|
144
|
+
}
|
|
145
|
+
function processImports(el, importerPath, rewriteImportPath, makeImportStatement) {
|
|
123
146
|
// identifiers to use in the output
|
|
124
147
|
const symbolToIdentifier = new Map();
|
|
125
148
|
// symbols that need a value import (as opposed to a type-only import)
|
|
@@ -208,7 +231,7 @@ function processImports(el, importerPath, makeImportStatement) {
|
|
|
208
231
|
// should never happen
|
|
209
232
|
continue;
|
|
210
233
|
}
|
|
211
|
-
const from = (0, import_path_js_1.makeImportPathRelative)(importerPath, s.from);
|
|
234
|
+
const from = (0, import_path_js_1.makeImportPathRelative)(importerPath, rewriteImportPath(s.from));
|
|
212
235
|
if (i.types.size > 0) {
|
|
213
236
|
makeImportStatement(true, from, buildNames(i.types));
|
|
214
237
|
}
|
|
@@ -17,9 +17,10 @@ exports.makeImportPathRelative = exports.deriveImportPath = exports.relativePath
|
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
18
|
const cache = new WeakMap();
|
|
19
19
|
/**
|
|
20
|
-
* Apply import rewrites to the given path.
|
|
20
|
+
* Apply import rewrites to the given import path, and change all .js extensions
|
|
21
|
+
* to the given import extension.
|
|
21
22
|
*/
|
|
22
|
-
function rewriteImportPath(importPath, rewriteImports) {
|
|
23
|
+
function rewriteImportPath(importPath, rewriteImports, importExtension) {
|
|
23
24
|
let ri = cache.get(rewriteImports);
|
|
24
25
|
if (ri === undefined) {
|
|
25
26
|
ri = rewriteImports.map(({ pattern, target }) => {
|
|
@@ -32,9 +33,20 @@ function rewriteImportPath(importPath, rewriteImports) {
|
|
|
32
33
|
}
|
|
33
34
|
for (const { pattern, target } of ri) {
|
|
34
35
|
if (pattern.test(importPath)) {
|
|
35
|
-
|
|
36
|
+
if (exports.relativePathRE.test(importPath)) {
|
|
37
|
+
importPath =
|
|
38
|
+
target.replace(/\/$/, "") + importPath.replace(exports.relativePathRE, "/");
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
importPath = target;
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
36
44
|
}
|
|
37
45
|
}
|
|
46
|
+
if (importExtension != ".js" && importPath.endsWith(".js")) {
|
|
47
|
+
importPath =
|
|
48
|
+
importPath.substring(0, importPath.length - 3) + importExtension;
|
|
49
|
+
}
|
|
38
50
|
return importPath;
|
|
39
51
|
}
|
|
40
52
|
exports.rewriteImportPath = rewriteImportPath;
|
|
@@ -36,6 +36,7 @@ function createRuntimeImports(bootstrapWkt) {
|
|
|
36
36
|
ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
|
|
37
37
|
MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
|
|
38
38
|
MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
|
|
39
|
+
IMessageTypeRegistry: infoToSymbol("IMessageTypeRegistry", bootstrapWkt),
|
|
39
40
|
};
|
|
40
41
|
}
|
|
41
42
|
exports.createRuntimeImports = createRuntimeImports;
|
|
@@ -19,13 +19,13 @@ 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, targets, pluginName, pluginVersion, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles) {
|
|
22
|
+
function createSchema(request, targets, pluginName, pluginVersion, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles) {
|
|
23
23
|
const descriptorSet = (0, protobuf_1.createDescriptorSet)(request.protoFile);
|
|
24
24
|
const filesToGenerate = findFilesToGenerate(descriptorSet, request);
|
|
25
25
|
const runtime = (0, runtime_imports_js_1.createRuntimeImports)(bootstrapWkt);
|
|
26
26
|
const createTypeImport = (desc) => {
|
|
27
27
|
const name = protobuf_1.codegenInfo.localName(desc);
|
|
28
|
-
const from = (0, import_path_js_1.
|
|
28
|
+
const from = (0, import_path_js_1.makeImportPath)(desc.file, bootstrapWkt, filesToGenerate);
|
|
29
29
|
return (0, import_symbol_js_1.createImportSymbol)(name, from);
|
|
30
30
|
};
|
|
31
31
|
const generatedFiles = [];
|
|
@@ -36,8 +36,7 @@ function createSchema(request, targets, pluginName, pluginVersion, tsNocheck, bo
|
|
|
36
36
|
files: filesToGenerate,
|
|
37
37
|
allFiles: descriptorSet.files,
|
|
38
38
|
generateFile(name) {
|
|
39
|
-
const
|
|
40
|
-
const genFile = (0, generated_file_js_1.createGeneratedFile)(name, importPath, createTypeImport, runtime, {
|
|
39
|
+
const genFile = (0, generated_file_js_1.createGeneratedFile)(name, (0, import_path_js_1.deriveImportPath)(name), (importPath) => (0, import_path_js_1.rewriteImportPath)(importPath, rewriteImports, importExtension), createTypeImport, runtime, {
|
|
41
40
|
pluginName,
|
|
42
41
|
pluginVersion,
|
|
43
42
|
parameter: request.parameter,
|
|
@@ -27,8 +27,8 @@ export function createEcmaScriptPlugin(init) {
|
|
|
27
27
|
version: init.version,
|
|
28
28
|
run(req) {
|
|
29
29
|
var _a;
|
|
30
|
-
const { targets, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles, } = parseParameter(req.parameter, init.parseOption);
|
|
31
|
-
const { schema, getFileInfo } = createSchema(req, targets, init.name, init.version, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles);
|
|
30
|
+
const { targets, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles, } = parseParameter(req.parameter, init.parseOption);
|
|
31
|
+
const { schema, getFileInfo } = createSchema(req, targets, init.name, init.version, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles);
|
|
32
32
|
const targetTs = schema.targets.includes("ts");
|
|
33
33
|
const targetJs = schema.targets.includes("js");
|
|
34
34
|
const targetDts = schema.targets.includes("dts");
|
|
@@ -99,6 +99,7 @@ function parseParameter(parameter, parseOption) {
|
|
|
99
99
|
let bootstrapWkt = false;
|
|
100
100
|
let keepEmptyFiles = false;
|
|
101
101
|
const rewriteImports = [];
|
|
102
|
+
let importExtension = ".js";
|
|
102
103
|
for (const { key, value } of splitParameter(parameter)) {
|
|
103
104
|
switch (key) {
|
|
104
105
|
case "target":
|
|
@@ -155,6 +156,10 @@ function parseParameter(parameter, parseOption) {
|
|
|
155
156
|
rewriteImports.push({ pattern, target });
|
|
156
157
|
break;
|
|
157
158
|
}
|
|
159
|
+
case "import_extension": {
|
|
160
|
+
importExtension = value === "none" ? "" : value;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
158
163
|
case "keep_empty_files": {
|
|
159
164
|
switch (value) {
|
|
160
165
|
case "true":
|
|
@@ -183,7 +188,14 @@ function parseParameter(parameter, parseOption) {
|
|
|
183
188
|
break;
|
|
184
189
|
}
|
|
185
190
|
}
|
|
186
|
-
return {
|
|
191
|
+
return {
|
|
192
|
+
targets,
|
|
193
|
+
tsNocheck,
|
|
194
|
+
bootstrapWkt,
|
|
195
|
+
rewriteImports,
|
|
196
|
+
importExtension,
|
|
197
|
+
keepEmptyFiles,
|
|
198
|
+
};
|
|
187
199
|
}
|
|
188
200
|
function splitParameter(parameter) {
|
|
189
201
|
if (parameter == undefined) {
|
|
@@ -45,7 +45,7 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
|
|
|
45
45
|
builder.push(`syntax ${file.syntax})\n`);
|
|
46
46
|
builder.push("/* eslint-disable */\n");
|
|
47
47
|
if (tsNoCheck) {
|
|
48
|
-
builder.push("
|
|
48
|
+
builder.push("// @ts-nocheck\n");
|
|
49
49
|
}
|
|
50
50
|
builder.push("\n");
|
|
51
51
|
writeLeadingComments(file.getPackageComments());
|
|
@@ -14,15 +14,28 @@
|
|
|
14
14
|
import { createImportSymbol } from "./import-symbol.js";
|
|
15
15
|
import { literalString, makeFilePreamble } from "./gencommon.js";
|
|
16
16
|
import { makeImportPathRelative } from "./import-path.js";
|
|
17
|
-
export function createGeneratedFile(name, importPath, createTypeImport, runtimeImports, preambleSettings, keepEmpty) {
|
|
17
|
+
export function createGeneratedFile(name, importPath, rewriteImportPath, createTypeImport, runtimeImports, preambleSettings, keepEmpty) {
|
|
18
18
|
let preamble;
|
|
19
19
|
const el = [];
|
|
20
20
|
return {
|
|
21
21
|
preamble(file) {
|
|
22
22
|
preamble = makeFilePreamble(file, preambleSettings.pluginName, preambleSettings.pluginVersion, preambleSettings.parameter, preambleSettings.tsNocheck);
|
|
23
23
|
},
|
|
24
|
-
print(...
|
|
25
|
-
|
|
24
|
+
print(printableOrFragments, ...rest) {
|
|
25
|
+
let printables;
|
|
26
|
+
if (printableOrFragments != null &&
|
|
27
|
+
Object.prototype.hasOwnProperty.call(printableOrFragments, "raw")) {
|
|
28
|
+
// If called with a tagged template literal
|
|
29
|
+
printables = buildPrintablesFromFragments(printableOrFragments, rest);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
// If called with just an array of Printables
|
|
33
|
+
printables =
|
|
34
|
+
printableOrFragments != null
|
|
35
|
+
? [printableOrFragments, ...rest]
|
|
36
|
+
: rest;
|
|
37
|
+
}
|
|
38
|
+
printableToEl(printables, el, createTypeImport, runtimeImports);
|
|
26
39
|
el.push("\n");
|
|
27
40
|
},
|
|
28
41
|
export(name) {
|
|
@@ -36,7 +49,7 @@ export function createGeneratedFile(name, importPath, createTypeImport, runtimeI
|
|
|
36
49
|
return createTypeImport(typeOrName);
|
|
37
50
|
},
|
|
38
51
|
getFileInfo() {
|
|
39
|
-
const content = elToContent(el, importPath);
|
|
52
|
+
const content = elToContent(el, importPath, rewriteImportPath);
|
|
40
53
|
if (!keepEmpty && content.length === 0) {
|
|
41
54
|
return;
|
|
42
55
|
}
|
|
@@ -48,11 +61,11 @@ export function createGeneratedFile(name, importPath, createTypeImport, runtimeI
|
|
|
48
61
|
},
|
|
49
62
|
};
|
|
50
63
|
}
|
|
51
|
-
function elToContent(el, importerPath) {
|
|
64
|
+
function elToContent(el, importerPath, rewriteImportPath) {
|
|
52
65
|
const c = [];
|
|
53
|
-
const symbolToIdentifier = processImports(el, importerPath, (typeOnly, from, names) => {
|
|
66
|
+
const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
|
|
54
67
|
const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
|
|
55
|
-
const what = `{${p.join(", ")}}`;
|
|
68
|
+
const what = `{ ${p.join(", ")} }`;
|
|
56
69
|
if (typeOnly) {
|
|
57
70
|
c.push(`import type ${what} from ${literalString(from)};\n`);
|
|
58
71
|
}
|
|
@@ -115,7 +128,17 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
|
|
|
115
128
|
}
|
|
116
129
|
}
|
|
117
130
|
}
|
|
118
|
-
function
|
|
131
|
+
function buildPrintablesFromFragments(fragments, values) {
|
|
132
|
+
const printables = [];
|
|
133
|
+
fragments.forEach((fragment, i) => {
|
|
134
|
+
printables.push(fragment);
|
|
135
|
+
if (fragments.length - 1 !== i) {
|
|
136
|
+
printables.push(values[i]);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return printables;
|
|
140
|
+
}
|
|
141
|
+
function processImports(el, importerPath, rewriteImportPath, makeImportStatement) {
|
|
119
142
|
// identifiers to use in the output
|
|
120
143
|
const symbolToIdentifier = new Map();
|
|
121
144
|
// symbols that need a value import (as opposed to a type-only import)
|
|
@@ -204,7 +227,7 @@ function processImports(el, importerPath, makeImportStatement) {
|
|
|
204
227
|
// should never happen
|
|
205
228
|
continue;
|
|
206
229
|
}
|
|
207
|
-
const from = makeImportPathRelative(importerPath, s.from);
|
|
230
|
+
const from = makeImportPathRelative(importerPath, rewriteImportPath(s.from));
|
|
208
231
|
if (i.types.size > 0) {
|
|
209
232
|
makeImportStatement(true, from, buildNames(i.types));
|
|
210
233
|
}
|
|
@@ -14,9 +14,10 @@
|
|
|
14
14
|
import { codegenInfo } from "@bufbuild/protobuf";
|
|
15
15
|
const cache = new WeakMap();
|
|
16
16
|
/**
|
|
17
|
-
* Apply import rewrites to the given path.
|
|
17
|
+
* Apply import rewrites to the given import path, and change all .js extensions
|
|
18
|
+
* to the given import extension.
|
|
18
19
|
*/
|
|
19
|
-
export function rewriteImportPath(importPath, rewriteImports) {
|
|
20
|
+
export function rewriteImportPath(importPath, rewriteImports, importExtension) {
|
|
20
21
|
let ri = cache.get(rewriteImports);
|
|
21
22
|
if (ri === undefined) {
|
|
22
23
|
ri = rewriteImports.map(({ pattern, target }) => {
|
|
@@ -29,9 +30,20 @@ export function rewriteImportPath(importPath, rewriteImports) {
|
|
|
29
30
|
}
|
|
30
31
|
for (const { pattern, target } of ri) {
|
|
31
32
|
if (pattern.test(importPath)) {
|
|
32
|
-
|
|
33
|
+
if (relativePathRE.test(importPath)) {
|
|
34
|
+
importPath =
|
|
35
|
+
target.replace(/\/$/, "") + importPath.replace(relativePathRE, "/");
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
importPath = target;
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
33
41
|
}
|
|
34
42
|
}
|
|
43
|
+
if (importExtension != ".js" && importPath.endsWith(".js")) {
|
|
44
|
+
importPath =
|
|
45
|
+
importPath.substring(0, importPath.length - 3) + importExtension;
|
|
46
|
+
}
|
|
35
47
|
return importPath;
|
|
36
48
|
}
|
|
37
49
|
function starToRegExp(star) {
|
|
@@ -33,6 +33,7 @@ export function createRuntimeImports(bootstrapWkt) {
|
|
|
33
33
|
ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
|
|
34
34
|
MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
|
|
35
35
|
MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
|
|
36
|
+
IMessageTypeRegistry: infoToSymbol("IMessageTypeRegistry", bootstrapWkt),
|
|
36
37
|
};
|
|
37
38
|
}
|
|
38
39
|
function infoToSymbol(name, bootstrapWkt) {
|
|
@@ -11,18 +11,18 @@
|
|
|
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 {
|
|
14
|
+
import { CodeGeneratorResponse, CodeGeneratorResponse_Feature, codegenInfo, createDescriptorSet, protoInt64, } 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, pluginName, pluginVersion, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles) {
|
|
19
|
+
export function createSchema(request, targets, pluginName, pluginVersion, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles) {
|
|
20
20
|
const descriptorSet = createDescriptorSet(request.protoFile);
|
|
21
21
|
const filesToGenerate = findFilesToGenerate(descriptorSet, request);
|
|
22
22
|
const runtime = createRuntimeImports(bootstrapWkt);
|
|
23
23
|
const createTypeImport = (desc) => {
|
|
24
24
|
const name = codegenInfo.localName(desc);
|
|
25
|
-
const from =
|
|
25
|
+
const from = makeImportPath(desc.file, bootstrapWkt, filesToGenerate);
|
|
26
26
|
return createImportSymbol(name, from);
|
|
27
27
|
};
|
|
28
28
|
const generatedFiles = [];
|
|
@@ -33,8 +33,7 @@ export function createSchema(request, targets, pluginName, pluginVersion, tsNoch
|
|
|
33
33
|
files: filesToGenerate,
|
|
34
34
|
allFiles: descriptorSet.files,
|
|
35
35
|
generateFile(name) {
|
|
36
|
-
const
|
|
37
|
-
const genFile = createGeneratedFile(name, importPath, createTypeImport, runtime, {
|
|
36
|
+
const genFile = createGeneratedFile(name, deriveImportPath(name), (importPath) => rewriteImportPath(importPath, rewriteImports, importExtension), createTypeImport, runtime, {
|
|
38
37
|
pluginName,
|
|
39
38
|
pluginVersion,
|
|
40
39
|
parameter: request.parameter,
|
|
@@ -36,7 +36,13 @@ export interface GeneratedFile {
|
|
|
36
36
|
* - ImportSymbol: Adds an import statement and prints the name of the symbol.
|
|
37
37
|
* - DescMessage or DescEnum: Imports the type if necessary, and prints the name.
|
|
38
38
|
*/
|
|
39
|
-
print(...
|
|
39
|
+
print(...printables: Printable[]): void;
|
|
40
|
+
/**
|
|
41
|
+
* Add a line of code to the file with tagged template literal and
|
|
42
|
+
* an optional array of Printables.
|
|
43
|
+
* See print(Printable[]) for behavior when printing Printable items.
|
|
44
|
+
*/
|
|
45
|
+
print(fragments: TemplateStringsArray, ...printables: Printable[]): void;
|
|
40
46
|
/**
|
|
41
47
|
* Reserves an identifier in this file.
|
|
42
48
|
*/
|
|
@@ -61,7 +67,8 @@ export interface GenerateFileToFileInfo {
|
|
|
61
67
|
getFileInfo(): FileInfo | undefined;
|
|
62
68
|
}
|
|
63
69
|
declare type CreateTypeImportFn = (desc: DescMessage | DescEnum) => ImportSymbol;
|
|
64
|
-
|
|
70
|
+
declare type RewriteImportPathFn = (path: string) => string;
|
|
71
|
+
export declare function createGeneratedFile(name: string, importPath: string, rewriteImportPath: RewriteImportPathFn, createTypeImport: CreateTypeImportFn, runtimeImports: RuntimeImports, preambleSettings: {
|
|
65
72
|
pluginName: string;
|
|
66
73
|
pluginVersion: string;
|
|
67
74
|
parameter: string | undefined;
|
|
@@ -40,9 +40,10 @@ export declare type RewriteImports = {
|
|
|
40
40
|
target: string;
|
|
41
41
|
}[];
|
|
42
42
|
/**
|
|
43
|
-
* Apply import rewrites to the given path.
|
|
43
|
+
* Apply import rewrites to the given import path, and change all .js extensions
|
|
44
|
+
* to the given import extension.
|
|
44
45
|
*/
|
|
45
|
-
export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports): string;
|
|
46
|
+
export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: string): string;
|
|
46
47
|
/**
|
|
47
48
|
* Returns the import path for files generated by the base type generator
|
|
48
49
|
* protoc-gen-es.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CodeGeneratorRequest, DescFile } from "@bufbuild/protobuf";
|
|
2
2
|
import { CodeGeneratorResponse } from "@bufbuild/protobuf";
|
|
3
|
-
import type {
|
|
3
|
+
import type { FileInfo, GeneratedFile } from "./generated-file.js";
|
|
4
4
|
import { RuntimeImports } from "./runtime-imports.js";
|
|
5
5
|
import type { Target } from "./target.js";
|
|
6
6
|
import { RewriteImports } from "./import-path.js";
|
|
@@ -38,6 +38,6 @@ interface SchemaController {
|
|
|
38
38
|
schema: Schema;
|
|
39
39
|
getFileInfo: () => FileInfo[];
|
|
40
40
|
}
|
|
41
|
-
export declare function createSchema(request: CodeGeneratorRequest, targets: Target[], pluginName: string, pluginVersion: string, tsNocheck: boolean, bootstrapWkt: boolean, rewriteImports: RewriteImports, keepEmptyFiles: boolean): SchemaController;
|
|
41
|
+
export declare function createSchema(request: CodeGeneratorRequest, targets: Target[], pluginName: string, pluginVersion: string, tsNocheck: boolean, bootstrapWkt: boolean, rewriteImports: RewriteImports, importExtension: string, keepEmptyFiles: boolean): SchemaController;
|
|
42
42
|
export declare function toResponse(files: FileInfo[]): CodeGeneratorResponse;
|
|
43
43
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoplugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@bufbuild/protobuf": "0.
|
|
38
|
+
"@bufbuild/protobuf": "0.4.0",
|
|
39
39
|
"@typescript/vfs": "^1.4.0",
|
|
40
40
|
"typescript": "4.5.2"
|
|
41
41
|
},
|