Skip to content

Commit

Permalink
fix: generate stub types of with explicit extension import if pkg typ…
Browse files Browse the repository at this point in the history
…e is `module` (#371)
  • Loading branch information
yshrsmz authored Jun 4, 2024
1 parent 3ae2de1 commit e1e0935
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ export async function rollupBuild(ctx: BuildContext) {
entry.name!,
);

const isESM = ctx.pkg.type === "module";
const resolvedEntry = normalize(
tryResolve(entry.input, ctx.options.rootDir) || entry.input,
);
const resolvedEntryWithoutExt = resolvedEntry.slice(
0,
Math.max(0, resolvedEntry.length - extname(resolvedEntry).length),
);
const resolvedEntryForTypeImport = isESM
? `${resolvedEntry.replace(/(\.m?)(ts)$/, "$1js")}`
: resolvedEntryWithoutExt;
const code = await fsp.readFile(resolvedEntry, "utf8");
const shebang = getShebang(code);

Expand All @@ -85,7 +89,7 @@ export async function rollupBuild(ctx: BuildContext) {
`const _jiti = jiti(null, ${serializedJitiOptions})`,
"",
`/** @type {import(${JSON.stringify(
resolvedEntryWithoutExt,
resolvedEntryForTypeImport,
)})} */`,
`module.exports = _jiti(${JSON.stringify(resolvedEntry)})`,
].join("\n"),
Expand Down Expand Up @@ -114,7 +118,7 @@ export async function rollupBuild(ctx: BuildContext) {
"",
`const _jiti = jiti(null, ${serializedJitiOptions})`,
"",
`/** @type {import(${JSON.stringify(resolvedEntryWithoutExt)})} */`,
`/** @type {import(${JSON.stringify(resolvedEntryForTypeImport)})} */`,
`const _module = await _jiti.import(${JSON.stringify(
resolvedEntry,
)});`,
Expand All @@ -129,10 +133,10 @@ export async function rollupBuild(ctx: BuildContext) {
await writeFile(
output + ".d.ts",
[
`export * from ${JSON.stringify(resolvedEntryWithoutExt)};`,
`export * from ${JSON.stringify(resolvedEntryForTypeImport)};`,
hasDefaultExport
? `export { default } from ${JSON.stringify(
resolvedEntryWithoutExt,
resolvedEntryForTypeImport,
)};`
: "",
].join("\n"),
Expand Down

0 comments on commit e1e0935

Please sign in to comment.