From 8a6225a0deb10c22b899d1206750e3dd66c588b0 Mon Sep 17 00:00:00 2001 From: ZHAO JinXiang Date: Sun, 25 Jun 2023 14:32:29 +0800 Subject: [PATCH] feat: generate index.d.mts and index.d.cts --- README.md | 2 +- package.json | 5 ++--- src/auto.ts | 9 ++++++--- src/builder/rollup.ts | 34 +++++++++++++++++++++++----------- test/auto.test.ts | 11 ++++++++--- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d58b8e1a..960ffefa 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Update `package.json`: } }, "main": "./dist/index.cjs", - "types": "./dist/index.d.ts", + "types": "./dist/index.d.cts", "files": [ "dist" ] diff --git a/package.json b/package.json index f654b25d..1ef9fb46 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/auto.ts b/src/auto.ts index 9bc2809a..74badfe5 100644 --- a/src/auto.ts +++ b/src/auto.ts @@ -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 @@ -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) { @@ -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; } diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index ec09a28b..4d955b88 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -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 ({ input: Object.fromEntries( ctx.options.entries @@ -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", @@ -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 }, diff --git a/test/auto.test.ts b/test/auto.test.ts index 76a7e3e3..1fa912d8 100644 --- a/test/auto.test.ts +++ b/test/auto.test.ts @@ -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"]