Skip to content

Commit 1684161

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

File tree

6 files changed

+47
-35
lines changed

6 files changed

+47
-35
lines changed

dts.snapshot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"CopyOptionsFn": "type CopyOptionsFn = (_: ResolvedConfig) => Awaitable<CopyOptions>",
2828
"CssOptions": "interface CssOptions {\n splitting?: boolean\n fileName?: string\n}",
2929
"DevtoolsOptions": "interface DevtoolsOptions extends NonNullable<InputOptions['devtools']> {\n ui?: boolean | Partial<StartOptions>\n clean?: boolean\n}",
30-
"ExportsOptions": "interface ExportsOptions {\n devExports?: boolean | string\n packageJson?: boolean\n all?: boolean\n exclude?: (RegExp | string)[]\n customExports?: (_: Record<string, any>, _: { pkg: PackageJson; chunks: ChunksByFormat; isPublish: boolean }) => Awaitable<Record<string, any>>\n}",
30+
"ExportsOptions": "interface ExportsOptions {\n devExports?: boolean | string\n packageJson?: boolean\n all?: boolean\n exclude?: (RegExp | string)[]\n legacy?: boolean\n customExports?: (_: Record<string, any>, _: { pkg: PackageJson; chunks: ChunksByFormat; isPublish: boolean }) => Awaitable<Record<string, any>>\n}",
3131
"Format": "type Format = ModuleFormat",
3232
"globalLogger": "Logger",
3333
"InlineConfig": "interface InlineConfig extends UserConfig {\n config?: boolean | string\n configLoader?: 'auto' | 'native' | 'unrun'\n filter?: RegExp | Arrayable<string>\n}",

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.test.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ describe.concurrent('generateExports', () => {
5252
".": "./main.js",
5353
"./package.json": "./package.json",
5454
},
55-
"main": "./main.js",
56-
"module": "./main.js",
55+
"main": undefined,
56+
"module": undefined,
5757
"publishExports": undefined,
5858
"types": undefined,
5959
}
@@ -77,8 +77,8 @@ describe.concurrent('generateExports', () => {
7777
"./foo": "./foo.js",
7878
"./package.json": "./package.json",
7979
},
80-
"main": "./index.js",
81-
"module": "./index.js",
80+
"main": undefined,
81+
"module": undefined,
8282
"publishExports": undefined,
8383
"types": undefined,
8484
}
@@ -102,8 +102,8 @@ describe.concurrent('generateExports', () => {
102102
"./foo": "./foo/index.js",
103103
"./package.json": "./package.json",
104104
},
105-
"main": "./index.js",
106-
"module": "./index.js",
105+
"main": undefined,
106+
"module": undefined,
107107
"publishExports": undefined,
108108
"types": undefined,
109109
}
@@ -308,8 +308,6 @@ describe.concurrent('generateExports', () => {
308308
// key order matters
309309
expect(JSON.stringify(results, undefined, 2)).toMatchInlineSnapshot(`
310310
"{
311-
"main": "./index.js",
312-
"module": "./index.js",
313311
"exports": {
314312
".": {
315313
"dev": "./SRC/index.js",
@@ -345,8 +343,8 @@ describe.concurrent('generateExports', () => {
345343
"./foo": "./foo.js",
346344
"./package.json": "./package.json",
347345
},
348-
"main": "./index.js",
349-
"module": "./index.js",
346+
"main": undefined,
347+
"module": undefined,
350348
"publishExports": undefined,
351349
"types": undefined,
352350
}
@@ -373,8 +371,8 @@ describe.concurrent('generateExports', () => {
373371
"./foo": "./foo.js",
374372
"./package.json": "./package.json",
375373
},
376-
"main": "./index.js",
377-
"module": "./index.js",
374+
"main": undefined,
375+
"module": undefined,
378376
"publishExports": undefined,
379377
"types": undefined,
380378
}
@@ -459,8 +457,8 @@ describe.concurrent('generateExports', () => {
459457
"./foo": "./foo/index.js",
460458
"./package.json": "./package.json",
461459
},
462-
"main": "./index.js",
463-
"module": "./index.js",
460+
"main": undefined,
461+
"module": undefined,
464462
"publishExports": undefined,
465463
"types": "./index.d.ts",
466464
}
@@ -538,8 +536,8 @@ describe.concurrent('generateExports', () => {
538536
"./package.json": "./package.json",
539537
"./style.css": "./style.css",
540538
},
541-
"main": "./index.js",
542-
"module": "./index.js",
539+
"main": undefined,
540+
"module": undefined,
543541
"publishExports": undefined,
544542
"types": undefined,
545543
}
@@ -564,8 +562,8 @@ describe.concurrent('generateExports', () => {
564562
".": "./index.js",
565563
"./package.json": "./package.json",
566564
},
567-
"main": "./index.js",
568-
"module": "./index.js",
565+
"main": undefined,
566+
"module": undefined,
569567
"publishExports": undefined,
570568
"types": undefined,
571569
}
@@ -589,8 +587,8 @@ describe.concurrent('generateExports', () => {
589587
"./custom.css": "./custom.css",
590588
"./package.json": "./package.json",
591589
},
592-
"main": "./index.js",
593-
"module": "./index.js",
590+
"main": undefined,
591+
"module": undefined,
594592
"publishExports": undefined,
595593
"types": undefined,
596594
}
@@ -614,8 +612,8 @@ describe.concurrent('generateExports', () => {
614612
"./package.json": "./package.json",
615613
"./style.css": "./dist/style.css",
616614
},
617-
"main": "./index.js",
618-
"module": "./index.js",
615+
"main": undefined,
616+
"module": undefined,
619617
"publishExports": undefined,
620618
"types": undefined,
621619
}
@@ -644,8 +642,8 @@ describe.concurrent('generateExports', () => {
644642
"./package.json": "./package.json",
645643
"./style.css": "./style.css",
646644
},
647-
"main": "./index.js",
648-
"module": "./index.js",
645+
"main": undefined,
646+
"module": undefined,
649647
"publishExports": {
650648
".": "./index.js",
651649
"./package.json": "./package.json",

src/features/pkg/exports.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ export interface ExportsOptions {
4848
*/
4949
exclude?: (RegExp | string)[]
5050

51+
/**
52+
* Generate legacy fields (`main` and `module`) for older Node.js and bundlers
53+
* that do not support package `exports` field.
54+
*
55+
* Defaults to false, if only ESM builds are included, true otherwise.
56+
*
57+
* @see {@link https://github.com/publint/publint/issues/24}
58+
*/
59+
legacy?: boolean
60+
5161
customExports?: (
5262
exports: Record<string, any>,
5363
context: {
@@ -112,8 +122,15 @@ export async function generateExports(
112122
publishExports?: Record<string, any>
113123
}> {
114124
typeAssert(options.exports)
115-
const {
116-
exports: { devExports, all, packageJson = true, exclude, customExports },
125+
let {
126+
exports: {
127+
devExports,
128+
all,
129+
packageJson = true,
130+
exclude,
131+
customExports,
132+
legacy,
133+
},
117134
css,
118135
logger,
119136
} = options
@@ -131,6 +148,9 @@ export async function generateExports(
131148
logger.warn(`No CJS or ESM formats found in chunks for package ${pkg.name}`)
132149
}
133150

151+
const isPureESM = formats.length === 1 && formats[0] === 'es'
152+
legacy ??= !isPureESM
153+
134154
for (const [format, chunksByFormat] of Object.entries(chunks) as [
135155
NormalizedFormat,
136156
RolldownChunk[],
@@ -239,8 +259,8 @@ export async function generateExports(
239259
}
240260

241261
return {
242-
main: main || module || pkg.main,
243-
module: module || pkg.module,
262+
main: legacy ? main || module || pkg.main : undefined,
263+
module: legacy ? module || pkg.module : undefined,
244264
types: cjsTypes || esmTypes || pkg.types,
245265
exports,
246266
publishExports,

0 commit comments

Comments
 (0)