Skip to content

Commit

Permalink
chore: include exporting as default logic
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Jan 23, 2025
1 parent fe2cbeb commit ced3c25
Show file tree
Hide file tree
Showing 15 changed files with 1,095 additions and 89 deletions.
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
129 changes: 129 additions & 0 deletions src/builders/rollup/plugins/cjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# 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 (index.ts in the fixture)

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 `asdefault.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

0 comments on commit ced3c25

Please sign in to comment.