@@ -12,7 +12,7 @@ import { getRootPackageJSON } from '../utils/get-root-package-json.js';
1212import { getWorkspacePackagePaths } from '../utils/get-workspace-package-paths.js' ;
1313import { getWorkspaces } from '../utils/get-workspaces.js' ;
1414import { rewriteExports } from '../utils/rewrite-exports.js' ;
15- import { presetFields , presetFieldsESM } from './bootstrap.js' ;
15+ import { presetFieldsDual , presetFieldsOnlyESM } from './bootstrap.js' ;
1616
1717export const DIST_DIR = 'dist' ;
1818
@@ -227,9 +227,9 @@ async function build({
227227 return ;
228228 }
229229
230- validatePackageJson ( pkg , {
231- includesCommonJS : config ?. commonjs ?? true ,
232- } ) ;
230+ const dual = config ?. commonjs ?? true ;
231+
232+ validatePackageJson ( pkg , { dual } ) ;
233233
234234 const declarations = await globby ( '**/*.d.ts' , {
235235 cwd : getBuildPath ( 'esm' ) ,
@@ -269,7 +269,7 @@ async function build({
269269 ) ,
270270 ) ;
271271
272- if ( config ?. commonjs === undefined ) {
272+ if ( dual ) {
273273 // Transpile ESM to CJS and move CJS to dist/cjs only if there's something to transpile
274274 await fse . ensureDir ( join ( distPath , 'cjs' ) ) ;
275275
@@ -369,9 +369,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
369369 'engines' ,
370370 'name' ,
371371 'main' ,
372- 'module' ,
373372 'typings' ,
374- 'typescript' ,
375373 'type' ,
376374 ] ;
377375
@@ -393,14 +391,10 @@ function rewritePackageJson(pkg: Record<string, any>) {
393391 const distDirStr = `${ DIST_DIR } /` ;
394392
395393 newPkg . main = newPkg . main . replace ( distDirStr , '' ) ;
396- newPkg . module = newPkg . module . replace ( distDirStr , '' ) ;
397394 newPkg . typings = newPkg . typings . replace ( distDirStr , '' ) ;
398- newPkg . typescript = {
399- definition : newPkg . typescript . definition . replace ( distDirStr , '' ) ,
400- } ;
401395
402396 if ( ! pkg . exports ) {
403- newPkg . exports = presetFields . exports ;
397+ newPkg . exports = presetFieldsDual . exports ;
404398 }
405399 newPkg . exports = rewriteExports ( pkg . exports , DIST_DIR ) ;
406400
@@ -418,7 +412,7 @@ function rewritePackageJson(pkg: Record<string, any>) {
418412export function validatePackageJson (
419413 pkg : any ,
420414 opts : {
421- includesCommonJS : boolean ;
415+ dual : boolean ;
422416 } ,
423417) {
424418 function expect ( key : string , expected : unknown ) {
@@ -438,35 +432,29 @@ export function validatePackageJson(
438432 // 2. have an exports property
439433 // 3. have an exports and bin property
440434 if ( Object . keys ( pkg . bin ?? { } ) . length > 0 ) {
441- if ( opts . includesCommonJS === true ) {
442- expect ( 'main' , presetFields . main ) ;
443- expect ( 'typings' , presetFields . typings ) ;
435+ if ( opts . dual === true ) {
436+ expect ( 'main' , presetFieldsDual . main ) ;
437+ expect ( 'typings' , presetFieldsDual . typings ) ;
444438 } else {
445- expect ( 'main' , presetFieldsESM . main ) ;
446- expect ( 'typings' , presetFieldsESM . typings ) ;
439+ expect ( 'main' , presetFieldsOnlyESM . main ) ;
440+ expect ( 'typings' , presetFieldsOnlyESM . typings ) ;
447441 }
448- } else if (
449- pkg . main !== undefined ||
450- pkg . module !== undefined ||
451- pkg . exports !== undefined ||
452- pkg . typings !== undefined ||
453- pkg . typescript !== undefined
454- ) {
455- if ( opts . includesCommonJS === true ) {
442+ } else if ( pkg . main !== undefined || pkg . exports !== undefined || pkg . typings !== undefined ) {
443+ if ( opts . dual === true ) {
456444 // if there is no bin property, we NEED to check the exports.
457- expect ( 'main' , presetFields . main ) ;
458- expect ( 'typings' , presetFields . typings ) ;
445+ expect ( 'main' , presetFieldsDual . main ) ;
446+ expect ( 'typings' , presetFieldsDual . typings ) ;
459447
460448 // For now we enforce a top level exports property
461- expect ( "exports['.'].require" , presetFields . exports [ '.' ] . require ) ;
462- expect ( "exports['.'].import" , presetFields . exports [ '.' ] . import ) ;
463- expect ( "exports['.'].default" , presetFields . exports [ '.' ] . default ) ;
449+ expect ( "exports['.'].require" , presetFieldsDual . exports [ '.' ] . require ) ;
450+ expect ( "exports['.'].import" , presetFieldsDual . exports [ '.' ] . import ) ;
451+ expect ( "exports['.'].default" , presetFieldsDual . exports [ '.' ] . default ) ;
464452 } else {
465- expect ( 'main' , presetFieldsESM . main ) ;
466- expect ( 'typings' , presetFieldsESM . typings ) ;
453+ expect ( 'main' , presetFieldsOnlyESM . main ) ;
454+ expect ( 'typings' , presetFieldsOnlyESM . typings ) ;
467455
468456 // For now, we enforce a top level exports property
469- expect ( "exports['.']" , presetFieldsESM . exports [ '.' ] ) ;
457+ expect ( "exports['.']" , presetFieldsOnlyESM . exports [ '.' ] ) ;
470458 }
471459 }
472460}
0 commit comments