@@ -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>>
6879export 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