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

fix: correct default CJS exports #475

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions examples/1.zero-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
}
}
},
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"typesVersions": {
"*": {
"utils": [
"dist/utils.d.ts"
]
}
},
"files": [
"dist"
],
Expand Down
3 changes: 3 additions & 0 deletions examples/1.zero-config/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export interface SomeType {
name: string;
}
export default function sum(a: number, b: number): number {
return a + b;
}
4 changes: 2 additions & 2 deletions src/builders/rollup/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getRollupOptions } from "./config";
import { getChunkFilename } from "./utils";
import { rollupStub } from "./stub";
import { rollupWatch } from "./watch";
import { fixCJSExportTypePlugin } from "./plugins/cjs";
import { fixDefaultCJSExportsPlugin } from "./plugins/cjs";

export async function rollupBuild(ctx: BuildContext): Promise<void> {
// Stub mode
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function rollupBuild(ctx: BuildContext): Promise<void> {
...rollupOptions.plugins,
dts(ctx.options.rollup.dts),
removeShebangPlugin(),
ctx.options.rollup.emitCJS && fixCJSExportTypePlugin(),
ctx.options.rollup.emitCJS && fixDefaultCJSExportsPlugin(ctx),
].filter(Boolean);

await ctx.hooks.callHook("rollup:dts:options", ctx, rollupOptions);
Expand Down
144 changes: 144 additions & 0 deletions src/builders/rollup/plugins/cjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Default CJS exports

This document will cover all the types of default exports that can be generated by [rollup-plugin-dts](https://github.com/Swatinem/rollup-plugin-dts) plugin.

### Export default without specifier from the module itself

The following example can be found in the [reexport-types](../../../../test/cjs-types-fixture/reexport-types) fixture, check `index.ts` module.

Given the following code:

```ts
// module.ts
export default function foo() {}
```

`rollup-plugin-dts` will generate:

```ts
// module.d.[c]ts
declare function foo(): void;

export { foo as default };
```

we need to convert it to:

```ts
// module.d.[c]ts
declare function foo(): void;

export = foo;
```

### Exporting default from default import

The following example can be found in the [reexport-default](../../../../test/cjs-types-fixture/reexport-default) fixture, check `index.ts` module.

Given the following code:

```ts
// module.ts
import MagicString from "magic-string";
export default MagicString;
```

`rollup-plugin-dts` will generate:

```ts
// module.d.[c]ts
import MagicString from "magic-string";
export { default } from "magic-string";
```

we need to convert it to (doesn't require adding the import):

```ts
// module.d.[c]ts
import MagicString from "magic-string";
export = MagicString;
```

### Exporting default with specifier

The following example can be found in the [reexport-types](../../../../test/cjs-types-fixture/reexport-types) fixture, check `all.ts` module.

Given the following code:

```ts
// module.ts
export type * from "./index.ts";
export { default } from "./index.ts";
```

`rollup-plugin-dts` will generate:

```ts
// module.d.[c]ts
// otherexports and otherexporttypes when present
export { otherexports, type otherexporttypes, default } from "./index.js";
```

we need to convert it to (requires adding the import):

```ts
// module.d.[c]ts
import _default from "./index.js";
export = _default;
// otherexports and otherexporttypes when present
export { otherexports, type otherexporttypes } from "./index.js";
```

### Exporting default from named import as default export with specifier

The following example can be found in the [reexport-default](../../../../test/cjs-types-fixture/reexport-default) fixture, check `asdefault.ts` module.

Given the following code:

```ts
// module.ts
import { resolve } from "pathe";
export default resolve;
```

`rollup-plugin-dts` will generate:

```ts
// module.d.[c]ts
import { resolve } from "pathe";
export { resolve as default } from "pathe";
```

we need to convert it to (doesn't require adding the import):

```ts
// module.d.[c]ts
import { resolve } from "pathe";
export = resolve;
```

### Exporting named export as default with specifier

The following example can be found in the [reexport-default](../../../../test/cjs-types-fixture/reexport-default) fixture, check `resolveasdefault.ts` module.

Given the following code:

```ts
// module.ts
export { resolve as default } from "pathe";
```

`rollup-plugin-dts` will generate:

```ts
// module.d.[c]ts
export { resolve as default } from "pathe";
```

we need to convert it to (requires adding the import):

```ts
// module.d.[c]ts
import { resolve } from "pathe";
export = resolve;
```
Loading
Loading