@@ -13,7 +13,7 @@ import { getRootPackageJSON } from '../utils/get-root-package-json.js';
1313import { getWorkspacePackagePaths } from '../utils/get-workspace-package-paths.js' ;
1414import { getWorkspaces } from '../utils/get-workspaces.js' ;
1515import { rewriteExports } from '../utils/rewrite-exports.js' ;
16- import { presetFields , presetFieldsESM } from './bootstrap.js' ;
16+ import { presetFieldsDual , presetFieldsOnlyESM } from './bootstrap.js' ;
1717
1818export const DIST_DIR = 'dist' ;
1919
@@ -243,9 +243,9 @@ async function build({
243243 return ;
244244 }
245245
246- validatePackageJson ( pkg , {
247- includesCommonJS : config ?. commonjs ?? true ,
248- } ) ;
246+ const dual = config ?. commonjs ?? true ;
247+
248+ validatePackageJson ( pkg , { dual } ) ;
249249
250250 const declarations = await globby ( '**/*.d.ts' , {
251251 cwd : getBuildPath ( 'esm' ) ,
@@ -285,7 +285,7 @@ async function build({
285285 ) ,
286286 ) ;
287287
288- if ( config ?. commonjs === undefined ) {
288+ if ( dual ) {
289289 // Transpile ESM to CJS and move CJS to dist/cjs only if there's something to transpile
290290 await fse . ensureDir ( join ( distPath , 'cjs' ) ) ;
291291
@@ -385,9 +385,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
385385 'engines' ,
386386 'name' ,
387387 'main' ,
388- 'module' ,
389388 'typings' ,
390- 'typescript' ,
391389 'type' ,
392390 ] ;
393391
@@ -409,14 +407,10 @@ function rewritePackageJson(pkg: Record<string, any>) {
409407 const distDirStr = `${ DIST_DIR } /` ;
410408
411409 newPkg . main = newPkg . main . replace ( distDirStr , '' ) ;
412- newPkg . module = newPkg . module . replace ( distDirStr , '' ) ;
413410 newPkg . typings = newPkg . typings . replace ( distDirStr , '' ) ;
414- newPkg . typescript = {
415- definition : newPkg . typescript . definition . replace ( distDirStr , '' ) ,
416- } ;
417411
418412 if ( ! pkg . exports ) {
419- newPkg . exports = presetFields . exports ;
413+ newPkg . exports = presetFieldsDual . exports ;
420414 }
421415 newPkg . exports = rewriteExports ( pkg . exports , DIST_DIR ) ;
422416
@@ -434,7 +428,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
434428export function validatePackageJson (
435429 pkg : any ,
436430 opts : {
437- includesCommonJS : boolean ;
431+ dual : boolean ;
438432 } ,
439433) {
440434 function expect ( key : string , expected : unknown ) {
@@ -454,43 +448,29 @@ export function validatePackageJson(
454448 // 2. have an exports property
455449 // 3. have an exports and bin property
456450 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 ) ;
462454 } 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 ) ;
467457 }
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 ) {
476460 // 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 ) ;
481463
482464 // 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 ) ;
486468 } 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 ) ;
491471
492472 // For now, we enforce a top level exports property
493- expect ( "exports['.']" , presetFieldsESM . exports [ '.' ] ) ;
473+ expect ( "exports['.']" , presetFieldsOnlyESM . exports [ '.' ] ) ;
494474 }
495475 }
496476}
0 commit comments