Skip to content

Commit 9ba1413

Browse files
committed
feat(exports)!: add legacy option, remove main & module fields if pure ESM
1 parent e38b0e9 commit 9ba1413

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
"./package.json": "./package.json",
2424
"./client": "./client.d.ts"
2525
},
26-
"main": "./dist/index.mjs",
27-
"module": "./dist/index.mjs",
2826
"types": "./dist/index.d.mts",
2927
"typesVersions": {
3028
"*": {

packages/create-tsdown/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
"./run": "./dist/run.mjs",
2121
"./package.json": "./package.json"
2222
},
23-
"main": "./dist/index.mjs",
24-
"module": "./dist/index.mjs",
2523
"types": "./dist/index.d.mts",
2624
"typesVersions": {
2725
"*": {

packages/migrate/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
"./run": "./dist/run.mjs",
2121
"./package.json": "./package.json"
2222
},
23-
"main": "./dist/index.mjs",
24-
"module": "./dist/index.mjs",
2523
"types": "./dist/index.d.mts",
2624
"typesVersions": {
2725
"*": {

src/features/pkg/exports.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ export interface ExportsOptions {
2121
*/
2222
all?: boolean
2323

24+
/**
25+
* Generate legacy fields (`main` and `module`) for older Node.js and bundlers
26+
* that do not support package `exports` field.
27+
*
28+
* - Defaults to false, if only ESM builds are included.
29+
* - Defaults to true, if CJS builds are included.
30+
*
31+
* @see {@link https://github.com/publint/publint/issues/24}
32+
*/
33+
legacy?: boolean
34+
2435
customExports?: (
2536
exports: Record<string, any>,
2637
context: {
@@ -68,7 +79,7 @@ type SubExport = Partial<Record<'cjs' | 'es' | 'src', string>>
6879
export async function generateExports(
6980
pkg: PackageJson,
7081
chunks: ChunksByFormat,
71-
{ devExports, all, customExports }: ExportsOptions,
82+
{ devExports, all, customExports, legacy }: ExportsOptions,
7283
): Promise<{
7384
main: string | undefined
7485
module: string | undefined
@@ -84,6 +95,10 @@ export async function generateExports(
8495
esmTypes: string | undefined
8596
const exportsMap: Map<string, SubExport> = new Map()
8697

98+
const formats = Object.keys(chunks)
99+
const isPureESM = formats.length === 1 && formats[0] === 'es'
100+
legacy ??= !isPureESM
101+
87102
for (const [format, chunksByFormat] of Object.entries(chunks) as [
88103
NormalizedFormat,
89104
RolldownChunk[],
@@ -189,8 +204,8 @@ export async function generateExports(
189204
}
190205

191206
return {
192-
main: main || module || pkg.main,
193-
module: module || pkg.module,
207+
main: legacy ? main || module || pkg.main : undefined,
208+
module: legacy ? module || pkg.module : undefined,
194209
types: cjsTypes || esmTypes || pkg.types,
195210
exports,
196211
publishExports,

0 commit comments

Comments
 (0)