Skip to content

Commit 9de4c89

Browse files
committed
fix: apply restoreExtension on bundle
1 parent 92f1ea2 commit 9de4c89

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

packages/telescope/__tests__/bundler.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as t from "@babel/types";
22
import { bundlePackages, getPackagesBundled } from "../src/bundle";
33
import generate from "@babel/generator";
4-
import { recursiveModuleBundle } from "@cosmology/ast";
4+
import { exportAllFromRelPath, exportTypesWithAlias, recursiveModuleBundle } from "@cosmology/ast";
5+
import { restoreExtension } from "@cosmology/utils";
56
import { getTestProtoStore } from "../test-utils";
67
import { getImportStatements } from "../src/imports";
78

@@ -124,3 +125,47 @@ it("get import statements with ext without .", () => {
124125

125126
expect(importStat).toMatchSnapshot();
126127
});
128+
129+
describe("module bundle type with restoreImportExtension", () => {
130+
it("exportAllFromRelPath applies restoreExtension with .js", () => {
131+
const relPath = "./auth/v1beta1/auth";
132+
const restored = restoreExtension(relPath, ".js");
133+
const node = exportAllFromRelPath(restored);
134+
const code = generate(t.program([node])).code;
135+
expect(code).toBe('export * from "./auth/v1beta1/auth.js";');
136+
});
137+
138+
it("exportAllFromRelPath applies restoreExtension with .mjs", () => {
139+
const relPath = "./bank/v1beta1/tx";
140+
const restored = restoreExtension(relPath, ".mjs");
141+
const node = exportAllFromRelPath(restored);
142+
const code = generate(t.program([node])).code;
143+
expect(code).toBe('export * from "./bank/v1beta1/tx.mjs";');
144+
});
145+
146+
it("exportTypesWithAlias applies restoreExtension", () => {
147+
const relPath = "./auth/v1beta1/auth";
148+
const restored = restoreExtension(relPath, ".js");
149+
const types = [
150+
{ name: "BaseAccount", alias: "BaseAccount" },
151+
{ name: "ModuleAccount", alias: "ModuleAccount" },
152+
];
153+
const node = exportTypesWithAlias(types, restored);
154+
const code = generate(t.program([node])).code;
155+
expect(code).toBe(
156+
'export { BaseAccount, ModuleAccount } from "./auth/v1beta1/auth.js";'
157+
);
158+
});
159+
160+
it("restoreExtension does not double-add extension", () => {
161+
const relPath = "./auth/v1beta1/auth.js";
162+
const restored = restoreExtension(relPath, ".js");
163+
expect(restored).toBe("./auth/v1beta1/auth.js");
164+
});
165+
166+
it("restoreExtension without ext returns original path", () => {
167+
const relPath = "./auth/v1beta1/auth";
168+
const restored = restoreExtension(relPath, undefined);
169+
expect(restored).toBe("./auth/v1beta1/auth");
170+
});
171+
});

packages/telescope/src/generators/create-bundle.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
duplicateImportPathsWithExt,
1010
makeAliasName,
1111
makeAliasNameWithPackageAtEnd,
12+
restoreExtension,
1213
} from "@cosmology/utils";
1314

1415
/**
@@ -72,6 +73,8 @@ export const plugin = (builder: TelescopeBuilder, bundler: Bundler) => {
7273
return;
7374
}
7475

76+
const restoreImportExt = builder.options.restoreImportExtension;
77+
7578
// check each of exported identifiers is duplicated
7679
exportObjs.forEach((exportObj) => {
7780
const duplicatedTypeNames = exportObj.exportedIdentifiers.filter(
@@ -125,10 +128,11 @@ export const plugin = (builder: TelescopeBuilder, bundler: Bundler) => {
125128
return { name: identifier, alias: identifier };
126129
}
127130
);
128-
prog.push(exportTypesWithAlias(typesWithAlias, exportObj.relativePath));
131+
const relPath = restoreExtension(exportObj.relativePath, restoreImportExt);
132+
prog.push(exportTypesWithAlias(typesWithAlias, relPath));
129133
} else {
130134
// export *
131-
prog.push(exportAllFromRelPath(exportObj.relativePath));
135+
prog.push(exportAllFromRelPath(restoreExtension(exportObj.relativePath, restoreImportExt)));
132136
}
133137
});
134138
}

0 commit comments

Comments
 (0)