From 1b884227727f9e6ec684913acc608e6521f5d54b Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 18 Mar 2024 10:19:20 +0100 Subject: [PATCH] Add option to choose import extension --- packages/proto-loader/README.md | 2 ++ packages/proto-loader/bin/proto-loader-gen-types.ts | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/proto-loader/README.md b/packages/proto-loader/README.md index 5abf4d1dd..4b942c9f6 100644 --- a/packages/proto-loader/README.md +++ b/packages/proto-loader/README.md @@ -84,6 +84,8 @@ Options: [boolean] [default: false] --includeComments Generate doc comments from comments in the original files [boolean] [default: false] + --importExtension Specify which extension is used to import files with. + [string] [choices: "", ".ts", ".js", ".mjs"] [default: ""] -I, --includeDirs Directories to search for included files [array] -O, --outDir Directory in which to output files [string] [required] --grpcLib The gRPC implementation library that these types will diff --git a/packages/proto-loader/bin/proto-loader-gen-types.ts b/packages/proto-loader/bin/proto-loader-gen-types.ts index 6db109904..789296368 100644 --- a/packages/proto-loader/bin/proto-loader-gen-types.ts +++ b/packages/proto-loader/bin/proto-loader-gen-types.ts @@ -43,6 +43,7 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & { outDir: string; verbose?: boolean; includeComments?: boolean; + importExtension: string, inputTemplate: string; outputTemplate: string; inputBranded: boolean; @@ -153,7 +154,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv throw new Error('Invalid object passed to getImportLine'); } } - return `import type { ${importedTypes} } from '${filePath}';` + return `import type { ${importedTypes} } from '${filePath}${options.importExtension}';` } function getChildMessagesAndEnums(namespace: Protobuf.NamespaceBase): (Protobuf.Type | Protobuf.Enum)[] { @@ -863,6 +864,7 @@ async function runScript() { .option('json', boolDefaultFalseOption) .boolean('verbose') .option('includeComments', boolDefaultFalseOption) + .option('importExtension', { string: true, choices: ["", ".ts", ".js", ".mjs"], default: "" }) .option('includeDirs', { normalize: true, array: true, @@ -909,6 +911,7 @@ async function runScript() { oneofs: 'Output virtual oneof fields set to the present field\'s name', json: 'Represent Infinity and NaN as strings in float fields. Also decode google.protobuf.Any automatically', includeComments: 'Generate doc comments from comments in the original files', + importExtension: 'Specify which extension is used to import files with.', includeDirs: 'Directories to search for included files', outDir: 'Directory in which to output files', grpcLib: 'The gRPC implementation library that these types will be used with. If not provided, some types will not be generated',