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 Jul 4, 2023
1 parent 825df15 commit 0191789
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 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
9 changes: 6 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,7 @@ export function inferEntries(
entries.find((i) => i.input === input) ||
entries[entries.push({ input }) - 1];

if (output.file.endsWith(".d.ts")) {
if (/\.d\.(m|c)?ts$/.test(output.file)) {
dts = true;
}

Expand Down
45 changes: 34 additions & 11 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,47 @@ 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);
// #region cjs
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"),
});
}
// #endregion
// #region mjs
await typesBuild.write({
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
entryFileNames: "[name].d.mts",
chunkFileNames: (chunk) => getChunkFilename(ctx, chunk, "d.mts"),
});
// #endregion
// #region .d.ts for node10 compatibility
await typesBuild.write({
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
format: "esm",
entryFileNames: "[name].d.ts",
chunkFileNames: (chunk) => getChunkFilename(ctx, chunk, "d.ts"),
});
// #endregion
}

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 +232,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 +244,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
11 changes: 8 additions & 3 deletions test/auto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ describe("inferEntries", () => {
const result = inferEntries(
{
exports: {
types: "dist/test.d.ts",
import: "dist/test.mjs",
require: "dist/test.cjs",
import: {
types: "dist/test.d.mts",
default: "dist/test.mjs",
},
require: {
types: "dist/test.d.cts",
default: "dist/test.cjs",
},
},
},
["src/", "src/test.ts"]
Expand Down

0 comments on commit 0191789

Please sign in to comment.