@bufbuild/protoplugin 0.3.0 → 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.
@@ -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 { targets, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles };
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) {
@@ -17,7 +17,7 @@ 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 {
@@ -52,7 +52,7 @@ function createGeneratedFile(name, importPath, createTypeImport, runtimeImports,
52
52
  return createTypeImport(typeOrName);
53
53
  },
54
54
  getFileInfo() {
55
- const content = elToContent(el, importPath);
55
+ const content = elToContent(el, importPath, rewriteImportPath);
56
56
  if (!keepEmpty && content.length === 0) {
57
57
  return;
58
58
  }
@@ -65,9 +65,9 @@ function createGeneratedFile(name, importPath, createTypeImport, runtimeImports,
65
65
  };
66
66
  }
67
67
  exports.createGeneratedFile = createGeneratedFile;
68
- function elToContent(el, importerPath) {
68
+ function elToContent(el, importerPath, rewriteImportPath) {
69
69
  const c = [];
70
- const symbolToIdentifier = processImports(el, importerPath, (typeOnly, from, names) => {
70
+ const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
71
71
  const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
72
72
  const what = `{ ${p.join(", ")} }`;
73
73
  if (typeOnly) {
@@ -142,7 +142,7 @@ function buildPrintablesFromFragments(fragments, values) {
142
142
  });
143
143
  return printables;
144
144
  }
145
- function processImports(el, importerPath, makeImportStatement) {
145
+ function processImports(el, importerPath, rewriteImportPath, makeImportStatement) {
146
146
  // identifiers to use in the output
147
147
  const symbolToIdentifier = new Map();
148
148
  // symbols that need a value import (as opposed to a type-only import)
@@ -231,7 +231,7 @@ function processImports(el, importerPath, makeImportStatement) {
231
231
  // should never happen
232
232
  continue;
233
233
  }
234
- const from = (0, import_path_js_1.makeImportPathRelative)(importerPath, s.from);
234
+ const from = (0, import_path_js_1.makeImportPathRelative)(importerPath, rewriteImportPath(s.from));
235
235
  if (i.types.size > 0) {
236
236
  makeImportStatement(true, from, buildNames(i.types));
237
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
- return (target.replace(/\/$/, "") + importPath.replace(exports.relativePathRE, "/"));
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;
@@ -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.rewriteImportPath)((0, import_path_js_1.makeImportPath)(desc.file, bootstrapWkt, filesToGenerate), rewriteImports);
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 importPath = (0, import_path_js_1.rewriteImportPath)((0, import_path_js_1.deriveImportPath)(name), rewriteImports);
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 { targets, tsNocheck, bootstrapWkt, rewriteImports, keepEmptyFiles };
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) {
@@ -14,7 +14,7 @@
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 {
@@ -49,7 +49,7 @@ export function createGeneratedFile(name, importPath, createTypeImport, runtimeI
49
49
  return createTypeImport(typeOrName);
50
50
  },
51
51
  getFileInfo() {
52
- const content = elToContent(el, importPath);
52
+ const content = elToContent(el, importPath, rewriteImportPath);
53
53
  if (!keepEmpty && content.length === 0) {
54
54
  return;
55
55
  }
@@ -61,9 +61,9 @@ export function createGeneratedFile(name, importPath, createTypeImport, runtimeI
61
61
  },
62
62
  };
63
63
  }
64
- function elToContent(el, importerPath) {
64
+ function elToContent(el, importerPath, rewriteImportPath) {
65
65
  const c = [];
66
- const symbolToIdentifier = processImports(el, importerPath, (typeOnly, from, names) => {
66
+ const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
67
67
  const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
68
68
  const what = `{ ${p.join(", ")} }`;
69
69
  if (typeOnly) {
@@ -138,7 +138,7 @@ function buildPrintablesFromFragments(fragments, values) {
138
138
  });
139
139
  return printables;
140
140
  }
141
- function processImports(el, importerPath, makeImportStatement) {
141
+ function processImports(el, importerPath, rewriteImportPath, makeImportStatement) {
142
142
  // identifiers to use in the output
143
143
  const symbolToIdentifier = new Map();
144
144
  // symbols that need a value import (as opposed to a type-only import)
@@ -227,7 +227,7 @@ function processImports(el, importerPath, makeImportStatement) {
227
227
  // should never happen
228
228
  continue;
229
229
  }
230
- const from = makeImportPathRelative(importerPath, s.from);
230
+ const from = makeImportPathRelative(importerPath, rewriteImportPath(s.from));
231
231
  if (i.types.size > 0) {
232
232
  makeImportStatement(true, from, buildNames(i.types));
233
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
- return (target.replace(/\/$/, "") + importPath.replace(relativePathRE, "/"));
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) {
@@ -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 { codegenInfo, CodeGeneratorResponse, CodeGeneratorResponse_Feature, createDescriptorSet, protoInt64, } from "@bufbuild/protobuf";
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 = rewriteImportPath(makeImportPath(desc.file, bootstrapWkt, filesToGenerate), rewriteImports);
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 importPath = rewriteImportPath(deriveImportPath(name), rewriteImports);
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,
@@ -67,7 +67,8 @@ export interface GenerateFileToFileInfo {
67
67
  getFileInfo(): FileInfo | undefined;
68
68
  }
69
69
  declare type CreateTypeImportFn = (desc: DescMessage | DescEnum) => ImportSymbol;
70
- export declare function createGeneratedFile(name: string, importPath: string, createTypeImport: CreateTypeImportFn, runtimeImports: RuntimeImports, preambleSettings: {
70
+ declare type RewriteImportPathFn = (path: string) => string;
71
+ export declare function createGeneratedFile(name: string, importPath: string, rewriteImportPath: RewriteImportPathFn, createTypeImport: CreateTypeImportFn, runtimeImports: RuntimeImports, preambleSettings: {
71
72
  pluginName: string;
72
73
  pluginVersion: string;
73
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 { GeneratedFile, FileInfo } from "./generated-file.js";
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.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.3.0",
38
+ "@bufbuild/protobuf": "0.4.0",
39
39
  "@typescript/vfs": "^1.4.0",
40
40
  "typescript": "4.5.2"
41
41
  },