Skip to content

Commit

Permalink
fix: add support of importSuffix=.js for index files (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnidan authored and stephenh committed Jan 18, 2024
1 parent 57a6626 commit 183cf03
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -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][] = [];
Expand Down
46 changes: 45 additions & 1 deletion tests/utils-test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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";
"
`);
});
});
});

0 comments on commit 183cf03

Please sign in to comment.