From 179fd8f419325fea1003716c22e6e0ae9786e9b9 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 27 Jul 2022 15:39:39 -0700 Subject: [PATCH] fix: Properly handle package exports for dual formats. --- packages/packemon/src/Package.ts | 15 +++++++++++---- packages/packemon/tests/Package.test.ts | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/packemon/src/Package.ts b/packages/packemon/src/Package.ts index 74c95fff7..fe2e593c3 100644 --- a/packages/packemon/src/Package.ts +++ b/packages/packemon/src/Package.ts @@ -3,7 +3,7 @@ import glob from 'fast-glob'; import fs from 'fs-extra'; import semver from 'semver'; -import { isObject, Memoize, PackageStructure, Path, toArray } from '@boost/common'; +import { deepMerge, isObject, Memoize, PackageStructure, Path, toArray } from '@boost/common'; import { optimal } from '@boost/common/optimal'; import { createDebugger, Debugger } from '@boost/debug'; import { Artifact } from './Artifact'; @@ -29,6 +29,7 @@ import { FeatureFlags, InputMap, PackageConfig, + PackageExportPaths, PackageExports, PackemonPackage, PackemonPackageConfig, @@ -503,12 +504,18 @@ export class Package { } if (!exportMap[path]) { - exportMap[path] = {}; - } else if (typeof exportMap[path] === 'string') { + exportMap[path] = conditions; + return; + } + + if (typeof exportMap[path] === 'string') { exportMap[path] = { default: exportMap[path] }; } - Object.assign(exportMap[path]!, conditions); + exportMap[path] = deepMerge( + exportMap[path] as PackageExportPaths, + typeof conditions === 'string' ? { default: conditions } : conditions, + ); }); }); diff --git a/packages/packemon/tests/Package.test.ts b/packages/packemon/tests/Package.test.ts index f6071a64a..7ed0e31be 100644 --- a/packages/packemon/tests/Package.test.ts +++ b/packages/packemon/tests/Package.test.ts @@ -700,6 +700,23 @@ describe('Package', () => { './package.json': './package.json', }); }); + + it('supports dual cjs/mjs exports', async () => { + pkg.addArtifact(createCodeArtifact([{ format: 'cjs' }])); + pkg.addArtifact(createCodeArtifact([{ format: 'mjs' }], 'node', 'experimental')); + + await pkg.build({ addExports: true }, config); + + expect(pkg.packageJson.exports).toEqual({ + '.': { + node: { + import: './mjs/index.mjs', + require: './cjs/index.cjs', + }, + }, + './package.json': './package.json', + }); + }); }); describe('files', () => {