Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate index.d.mts and index.d.cts #273

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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",
xiaoxiangmoe marked this conversation as resolved.
Show resolved Hide resolved
"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": {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
".": {
"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({
xiaoxiangmoe marked this conversation as resolved.
Show resolved Hide resolved
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
xiaoxiangmoe marked this conversation as resolved.
Show resolved Hide resolved
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 = (
xiaoxiangmoe marked this conversation as resolved.
Show resolved Hide resolved
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