diff --git a/src/exports.ts b/src/exports.ts index c40849d9..c4239c1d 100644 --- a/src/exports.ts +++ b/src/exports.ts @@ -171,8 +171,10 @@ function parseExport( export function getExportPaths(pkg: PackageMetadata) { const pathsMap: Record = {} const packageType = getPackageType(pkg) - + const isCjsPackage = packageType === 'commonjs' + const { exports: exportsConditions } = pkg + if (exportsConditions) { const paths = parseExport(exportsConditions, packageType) Object.assign(pathsMap, paths) @@ -181,12 +183,21 @@ export function getExportPaths(pkg: PackageMetadata) { // main export '.' from main/module/typings const defaultMainExport = constructFullExportCondition( { - [packageType === 'commonjs' ? 'require' : 'import']: pkg.main, + [isCjsPackage ? 'require' : 'import']: pkg.main, module: pkg.module, types: getTypings(pkg), }, packageType, ) + + if (isCjsPackage && pathsMap['.']?.['require']) { + // pathsMap's exports.require are prioritized. + defaultMainExport['require'] = pathsMap['.']['require'] + + console.warn( + `(warning) "exports.require" has overwritten "main" since they are duplicated.`, + ) + } // Merge the main export into '.' paths const mainExport = Object.assign({}, pathsMap['.'], defaultMainExport) diff --git a/test/lib-unit/exports.test.ts b/test/lib-unit/exports.test.ts index e3342c93..55759278 100644 --- a/test/lib-unit/exports.test.ts +++ b/test/lib-unit/exports.test.ts @@ -151,6 +151,29 @@ describe('lib exports', () => { }, }) }) + + it('should warn the duplicated export conditions', () => { + const logSpy = jest.spyOn(console, 'warn') + + expect( + getExportPaths({ + main: './dist/index.js', + exports: { + import: './dist/index.mjs', + require: './dist/index.cjs', + }, + }), + ).toEqual({ + '.': { + import: './dist/index.mjs', + require: './dist/index.cjs', + }, + }) + + expect(logSpy).toHaveBeenCalledWith( + '(warning) "exports.require" has overwritten "main" since they are duplicated.', + ) + }) }) describe('getExportConditionDist', () => {