@@ -13,7 +13,7 @@ import { getRootPackageJSON } from '../utils/get-root-package-json.js';
13
13
import { getWorkspacePackagePaths } from '../utils/get-workspace-package-paths.js' ;
14
14
import { getWorkspaces } from '../utils/get-workspaces.js' ;
15
15
import { rewriteExports } from '../utils/rewrite-exports.js' ;
16
- import { presetFields , presetFieldsESM } from './bootstrap.js' ;
16
+ import { presetFieldsDual , presetFieldsOnlyESM } from './bootstrap.js' ;
17
17
18
18
export const DIST_DIR = 'dist' ;
19
19
@@ -243,9 +243,9 @@ async function build({
243
243
return ;
244
244
}
245
245
246
- validatePackageJson ( pkg , {
247
- includesCommonJS : config ?. commonjs ?? true ,
248
- } ) ;
246
+ const dual = config ?. commonjs ?? true ;
247
+
248
+ validatePackageJson ( pkg , { dual } ) ;
249
249
250
250
const declarations = await globby ( '**/*.d.ts' , {
251
251
cwd : getBuildPath ( 'esm' ) ,
@@ -285,7 +285,7 @@ async function build({
285
285
) ,
286
286
) ;
287
287
288
- if ( config ?. commonjs === undefined ) {
288
+ if ( dual ) {
289
289
// Transpile ESM to CJS and move CJS to dist/cjs only if there's something to transpile
290
290
await fse . ensureDir ( join ( distPath , 'cjs' ) ) ;
291
291
@@ -385,9 +385,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
385
385
'engines' ,
386
386
'name' ,
387
387
'main' ,
388
- 'module' ,
389
388
'typings' ,
390
- 'typescript' ,
391
389
'type' ,
392
390
] ;
393
391
@@ -409,14 +407,10 @@ function rewritePackageJson(pkg: Record<string, any>) {
409
407
const distDirStr = `${ DIST_DIR } /` ;
410
408
411
409
newPkg . main = newPkg . main . replace ( distDirStr , '' ) ;
412
- newPkg . module = newPkg . module . replace ( distDirStr , '' ) ;
413
410
newPkg . typings = newPkg . typings . replace ( distDirStr , '' ) ;
414
- newPkg . typescript = {
415
- definition : newPkg . typescript . definition . replace ( distDirStr , '' ) ,
416
- } ;
417
411
418
412
if ( ! pkg . exports ) {
419
- newPkg . exports = presetFields . exports ;
413
+ newPkg . exports = presetFieldsDual . exports ;
420
414
}
421
415
newPkg . exports = rewriteExports ( pkg . exports , DIST_DIR ) ;
422
416
@@ -434,7 +428,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
434
428
export function validatePackageJson (
435
429
pkg : any ,
436
430
opts : {
437
- includesCommonJS : boolean ;
431
+ dual : boolean ;
438
432
} ,
439
433
) {
440
434
function expect ( key : string , expected : unknown ) {
@@ -454,43 +448,29 @@ export function validatePackageJson(
454
448
// 2. have an exports property
455
449
// 3. have an exports and bin property
456
450
if ( Object . keys ( pkg . bin ?? { } ) . length > 0 ) {
457
- if ( opts . includesCommonJS === true ) {
458
- expect ( 'main' , presetFields . main ) ;
459
- expect ( 'module' , presetFields . module ) ;
460
- expect ( 'typings' , presetFields . typings ) ;
461
- expect ( 'typescript.definition' , presetFields . typescript . definition ) ;
451
+ if ( opts . dual === true ) {
452
+ expect ( 'main' , presetFieldsDual . main ) ;
453
+ expect ( 'typings' , presetFieldsDual . typings ) ;
462
454
} else {
463
- expect ( 'main' , presetFieldsESM . main ) ;
464
- expect ( 'module' , presetFieldsESM . module ) ;
465
- expect ( 'typings' , presetFieldsESM . typings ) ;
466
- expect ( 'typescript.definition' , presetFieldsESM . typescript . definition ) ;
455
+ expect ( 'main' , presetFieldsOnlyESM . main ) ;
456
+ expect ( 'typings' , presetFieldsOnlyESM . typings ) ;
467
457
}
468
- } else if (
469
- pkg . main !== undefined ||
470
- pkg . module !== undefined ||
471
- pkg . exports !== undefined ||
472
- pkg . typings !== undefined ||
473
- pkg . typescript !== undefined
474
- ) {
475
- if ( opts . includesCommonJS === true ) {
458
+ } else if ( pkg . main !== undefined || pkg . exports !== undefined || pkg . typings !== undefined ) {
459
+ if ( opts . dual === true ) {
476
460
// if there is no bin property, we NEED to check the exports.
477
- expect ( 'main' , presetFields . main ) ;
478
- expect ( 'module' , presetFields . module ) ;
479
- expect ( 'typings' , presetFields . typings ) ;
480
- expect ( 'typescript.definition' , presetFields . typescript . definition ) ;
461
+ expect ( 'main' , presetFieldsDual . main ) ;
462
+ expect ( 'typings' , presetFieldsDual . typings ) ;
481
463
482
464
// For now we enforce a top level exports property
483
- expect ( "exports['.'].require" , presetFields . exports [ '.' ] . require ) ;
484
- expect ( "exports['.'].import" , presetFields . exports [ '.' ] . import ) ;
485
- expect ( "exports['.'].default" , presetFields . exports [ '.' ] . default ) ;
465
+ expect ( "exports['.'].require" , presetFieldsDual . exports [ '.' ] . require ) ;
466
+ expect ( "exports['.'].import" , presetFieldsDual . exports [ '.' ] . import ) ;
467
+ expect ( "exports['.'].default" , presetFieldsDual . exports [ '.' ] . default ) ;
486
468
} else {
487
- expect ( 'main' , presetFieldsESM . main ) ;
488
- expect ( 'module' , presetFieldsESM . module ) ;
489
- expect ( 'typings' , presetFieldsESM . typings ) ;
490
- expect ( 'typescript.definition' , presetFieldsESM . typescript . definition ) ;
469
+ expect ( 'main' , presetFieldsOnlyESM . main ) ;
470
+ expect ( 'typings' , presetFieldsOnlyESM . typings ) ;
491
471
492
472
// For now, we enforce a top level exports property
493
- expect ( "exports['.']" , presetFieldsESM . exports [ '.' ] ) ;
473
+ expect ( "exports['.']" , presetFieldsOnlyESM . exports [ '.' ] ) ;
494
474
}
495
475
}
496
476
}
0 commit comments