unjs / unbuild

📦 A unified JavaScript build system
MIT License
2.21k stars 86 forks source link

cannot build to cjs using mkdist #311

Closed Barbapapazes closed 1 month ago

Barbapapazes commented 10 months ago

Hello,

I was experimenting with unbuild and I encounter an issue.

Is it possible (if recommanded) to build to esm and cjs at the same time using mkdist like with rollup?

Barbapapazes commented 10 months ago

I expect to have

    ├── vite.cjs
    ├── vite.d.cts
    ├── vite.d.mts
    ├── vite.d.ts
    ├── vite.mjs
    ├── webpack.cjs
    ├── webpack.d.cts
    ├── webpack.d.mts
    ├── webpack.d.ts
    └── webpack.mjs

but I only got

├── vite.d.ts
├── vite.mjs
├── webpack.d.ts
└── webpack.mjs
joshmeads commented 10 months ago

Here was my work around for now, just a snippet I use in most the configs:

function dualOutput(
  config: Omit<MkdistBuildEntry, "builder" | "format">
): BuildEntry[] {
  return [
    {
      builder: "mkdist",
      format: "esm",
      ...config,
      pattern: "**/!(*.stories).{js,jsx,ts,tsx}",
    },
    {
      builder: "mkdist",
      format: "cjs",
      ...config,
      pattern: "**/!(*.stories).{js,jsx,ts,tsx}",
    },
  ];
}
export default defineBuildConfig({
  entries: [
    "./src/index",
    ...dualOutput({
      input: "./src/components/Bits/",
      outDir: "./dist/bits",
    }),
Barbapapazes commented 10 months ago

Here was my work around for now, just a snippet I use in most the configs:

function dualOutput(
  config: Omit<MkdistBuildEntry, "builder" | "format">
): BuildEntry[] {
  return [
    {
      builder: "mkdist",
      format: "esm",
      ...config,
      pattern: "**/!(*.stories).{js,jsx,ts,tsx}",
    },
    {
      builder: "mkdist",
      format: "cjs",
      ...config,
      pattern: "**/!(*.stories).{js,jsx,ts,tsx}",
    },
  ];
}
export default defineBuildConfig({
  entries: [
    "./src/index",
    ...dualOutput({
      input: "./src/components/Bits/",
      outDir: "./dist/bits",
    }),

Yeah, I'm also using this way but, since there is a way to generate both with the rollup preset, I was hoping to do the same way for mkdist!

pi0 commented 1 month ago

Moving docs to https://github.com/unjs/unbuild/issues/398