Skip to content

Commit

Permalink
feat: generate index.d.mts and index.d.cts
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Jun 25, 2023
1 parent 825df15 commit 883e68d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Update `package.json`:
}
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"types": "./dist/index.d.cts",
"files": [
"dist"
]
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
"license": "MIT",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
"import": "./dist/index.mjs"
}
},
"types": "./dist/index.d.ts",
"types": "./dist/index.d.mts",
"bin": {
"unbuild": "./dist/cli.mjs"
},
Expand Down
13 changes: 10 additions & 3 deletions src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ export function inferEntries(
for (const output of outputs) {
// Supported output file extensions are `.d.ts`, `.cjs` and `.mjs`
// But we support any file extension here in case user has extended rollup options
const outputSlug = output.file.replace(/(\*[^/\\]*|\.d\.ts|\.\w+)$/, "");
const outputSlug = output.file.replace(
/(\*[^/\\]*|\.d\.(m|c)?ts|\.\w+)$/,
""
);
const isDir = outputSlug.endsWith("/");

// Skip top level directory
Expand All @@ -128,7 +131,7 @@ export function inferEntries(
const SOURCE_RE = new RegExp(`(?<=/|$)${d}${isDir ? "" : "\\.\\w+"}$`);
return sourceFiles
.find((i) => SOURCE_RE.test(i))
?.replace(/(\.d\.ts|\.\w+)$/, "");
?.replace(/(\.d\.(m|c)?ts|\.\w+)$/, "");
}, undefined as any);

if (!input) {
Expand All @@ -144,7 +147,11 @@ export function inferEntries(
entries.find((i) => i.input === input) ||
entries[entries.push({ input }) - 1];

if (output.file.endsWith(".d.ts")) {
if (
output.file.endsWith(".d.ts") ||
output.file.endsWith(".d.cts") ||
output.file.endsWith(".d.mts")
) {
dts = true;
}

Expand Down
34 changes: 23 additions & 11 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,36 @@ export async function rollupBuild(ctx: BuildContext) {
await ctx.hooks.callHook("rollup:dts:options", ctx, rollupOptions);
const typesBuild = await rollup(rollupOptions);
await ctx.hooks.callHook("rollup:dts:build", ctx, typesBuild);
if (ctx.options.rollup.emitCJS) {
await typesBuild.write({
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
entryFileNames: "[name].d.cts",
chunkFileNames: (chunk) => getChunkFilename(ctx, chunk, "d.cts"),
});
}
await typesBuild.write({
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
format: "esm",
entryFileNames: "[name].d.mts",
chunkFileNames: (chunk) => getChunkFilename(ctx, chunk, "d.mts"),
});
}

await ctx.hooks.callHook("rollup:done", ctx);
}

export function getRollupOptions(ctx: BuildContext): RollupOptions {
const getChunkFilename = (chunk: PreRenderedChunk, ext: string) => {
if (chunk.isDynamicEntry) {
return `chunks/[name].${ext}`;
}
// TODO: Find a way to generate human friendly hash for short groups
return `shared/${ctx.options.name}.[hash].${ext}`;
};
const getChunkFilename = (
ctx: BuildContext,
chunk: PreRenderedChunk,
ext: string
) => {
if (chunk.isDynamicEntry) {
return `chunks/[name].${ext}`;
}
// TODO: Find a way to generate human friendly hash for short groups
return `shared/${ctx.options.name}.[hash].${ext}`;
};

export function getRollupOptions(ctx: BuildContext): RollupOptions {
return (<RollupOptions>{
input: Object.fromEntries(
ctx.options.entries
Expand All @@ -209,7 +221,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
entryFileNames: "[name].cjs",
chunkFileNames: (chunk: PreRenderedChunk) =>
getChunkFilename(chunk, "cjs"),
getChunkFilename(ctx, chunk, "cjs"),
format: "cjs",
exports: "auto",
interop: "compat",
Expand All @@ -221,7 +233,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
entryFileNames: "[name].mjs",
chunkFileNames: (chunk: PreRenderedChunk) =>
getChunkFilename(chunk, "mjs"),
getChunkFilename(ctx, chunk, "mjs"),
format: "esm",
exports: "auto",
generatedCode: { constBindings: true },
Expand Down

0 comments on commit 883e68d

Please sign in to comment.