Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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