diff --git a/packages/packemon/src/Package.ts b/packages/packemon/src/Package.ts index 7c98839fb..e478e68c5 100644 --- a/packages/packemon/src/Package.ts +++ b/packages/packemon/src/Package.ts @@ -438,25 +438,25 @@ export class Package { this.artifacts.forEach((artifact) => { // Build files if (artifact instanceof CodeArtifact) { + const mainEntryName = artifact.inputs.index ? 'index' : Object.keys(artifact.inputs)[0]; + buildCount += artifact.builds.length; // Generate `main`, `module`, and `browser` fields - if (artifact.inputs.index) { - if (!mainEntry || artifact.platform === 'node') { - mainEntry = artifact.findEntryPoint(['lib', 'cjs', 'mjs', 'esm'], 'index'); - } - - if (!moduleEntry) { - moduleEntry = artifact.findEntryPoint(['esm'], 'index'); - } - - // Only include when we share a lib with another platform - if (!browserEntry && artifact.platform === 'browser') { - browserEntry = artifact.findEntryPoint( - artifact.sharedLib ? ['lib', 'umd'] : ['umd'], - 'index', - ); - } + if (!mainEntry || (artifact.platform === 'node' && mainEntryName === 'index')) { + mainEntry = artifact.findEntryPoint(['lib', 'cjs', 'mjs', 'esm'], mainEntryName); + } + + if (!moduleEntry || (artifact.platform === 'browser' && mainEntryName === 'index')) { + moduleEntry = artifact.findEntryPoint(['esm'], mainEntryName); + } + + // Only include when we share a lib with another platform + if (!browserEntry && artifact.platform === 'browser') { + browserEntry = artifact.findEntryPoint( + artifact.sharedLib ? ['lib', 'umd'] : ['umd'], + mainEntryName, + ); } // Generate `bin` field @@ -471,7 +471,11 @@ export class Package { // Type declarations if (artifact instanceof TypesArtifact) { - this.packageJson.types = artifact.findEntryPoint('index'); + const mainEntryName = artifact.builds.some((build) => build.outputName === 'index') + ? 'index' + : artifact.builds[0].outputName; + + this.packageJson.types = artifact.findEntryPoint(mainEntryName); } }); diff --git a/packages/packemon/tests/Package.test.ts b/packages/packemon/tests/Package.test.ts index 74c29dfcb..83d6c3df2 100644 --- a/packages/packemon/tests/Package.test.ts +++ b/packages/packemon/tests/Package.test.ts @@ -320,14 +320,14 @@ describe('Package', () => { ); }); - it('doesnt add "main" if output name is not "index"', async () => { + it('adds "main" if output name is not "index"', async () => { const a = createCodeArtifact([{ format: 'lib' }]); a.inputs = { server: 'src/index.ts' }; pkg.addArtifact(a); await pkg.build({}, config); - expect(pkg.packageJson.main).toBeUndefined(); + expect(pkg.packageJson.main).toBe('./lib/server.js'); }); it('adds "main" when using shared `lib` format', async () => { diff --git a/packages/packemon/tests/__snapshots__/outputs.test.ts.snap b/packages/packemon/tests/__snapshots__/outputs.test.ts.snap index a0780e34c..d93fc8794 100644 --- a/packages/packemon/tests/__snapshots__/outputs.test.ts.snap +++ b/packages/packemon/tests/__snapshots__/outputs.test.ts.snap @@ -192,10 +192,12 @@ exports[`Outputs (babel) artifacts builds all the artifacts with rollup 6`] = ` Array [ "package.json", Object { + "browser": "./umd/client.js", "dependencies": Object { "typescript": "*", }, "main": "./lib/index.js", + "module": "./esm/client.js", "name": "project-rollup", "packemon": Object { "inputs": Object { @@ -859,10 +861,12 @@ exports[`Outputs (swc) artifacts builds all the artifacts with rollup 6`] = ` Array [ "package.json", Object { + "browser": "./umd/client.js", "dependencies": Object { "typescript": "*", }, "main": "./lib/index.js", + "module": "./esm/client.js", "name": "project-rollup", "packemon": Object { "inputs": Object {