From 183cf039895184edd365e6cb7bb9751426ac56f4 Mon Sep 17 00:00:00 2001 From: Andrew <5755685+arnidan@users.noreply.github.com> Date: Wed, 17 Jan 2024 20:49:40 +0100 Subject: [PATCH] fix: add support of importSuffix=.js for index files (#986) --- src/utils.ts | 4 ++-- tests/utils-test.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 885e897f0..b17abb608 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -35,7 +35,7 @@ export function generateIndexFiles(files: FileDescriptorProto[], options: Option if (!(part in branch.leaves)) { const prePkgParts = pkgParts.slice(0, i + 1); const index = `index.${prePkgParts.join(".")}.ts`; - branch.chunks.push(code`export * as ${part} from "./${path.basename(index, ".ts")}";`); + branch.chunks.push(code`export * as ${part} from "./${path.basename(index, ".ts") + options.importSuffix}";`); branch.leaves[part] = { index, leaves: {}, @@ -44,7 +44,7 @@ export function generateIndexFiles(files: FileDescriptorProto[], options: Option } return branch.leaves[part]; }, packageTree); - branch.chunks.push(code`export * from "./${moduleName}";`); + branch.chunks.push(code`export * from "./${moduleName + options.importSuffix}";`); } const indexFiles: [string, Code][] = []; diff --git a/tests/utils-test.ts b/tests/utils-test.ts index 389b940e7..7de14ef8a 100644 --- a/tests/utils-test.ts +++ b/tests/utils-test.ts @@ -1,5 +1,7 @@ -import { maybeAddComment } from "../src/utils"; +import { maybeAddComment, generateIndexFiles } from "../src/utils"; +import { defaultOptions } from "../src/options"; import { Code, joinCode } from "ts-poet"; +import { FileDescriptorProto } from "ts-proto-descriptors"; describe("utils", () => { describe("maybeAddComment", () => { @@ -65,4 +67,46 @@ describe("utils", () => { `); }); }); + + describe("generateIndexFiles", () => { + const options = defaultOptions(); + const files: FileDescriptorProto[] = [ + FileDescriptorProto.fromJSON({ name: "Test.proto" }), + FileDescriptorProto.fromJSON({ name: "package/TestPackage.proto", package: "package" }), + ]; + + it("handles files", () => { + const indexFiles = generateIndexFiles(files, options); + + expect(indexFiles[0][0]).toMatch("index.ts"); + expect(indexFiles[0][1].toString()).toMatchInlineSnapshot(` + "export * from "./Test"; + export * as package from "./index.package"; + " + `); + + expect(indexFiles[1][0]).toMatch("index.package.ts"); + expect(indexFiles[1][1].toString()).toMatchInlineSnapshot(` + "export * from "./package/TestPackage"; + " + `); + }); + + it("handles files with importSuffix=.js", () => { + const indexFiles = generateIndexFiles(files, { ...options, importSuffix: ".js" }); + + expect(indexFiles[0][0]).toMatch("index.ts"); + expect(indexFiles[0][1].toString()).toMatchInlineSnapshot(` + "export * from "./Test.js"; + export * as package from "./index.package.js"; + " + `); + + expect(indexFiles[1][0]).toMatch("index.package.ts"); + expect(indexFiles[1][1].toString()).toMatchInlineSnapshot(` + "export * from "./package/TestPackage.js"; + " + `); + }); + }); });