Skip to content

Commit fc250ac

Browse files
authored
fix: Only set type when there's 1 build. (#128)
* Change type. * Add tests.
1 parent 08f5995 commit fc250ac

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

packages/packemon/src/Package.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,14 @@ export class Package {
421421
let mainEntry = '';
422422
let moduleEntry = '';
423423
let browserEntry = '';
424+
let buildCount = 0;
424425

425426
// eslint-disable-next-line complexity
426427
this.artifacts.forEach((artifact) => {
427428
// Build files
428429
if (artifact instanceof CodeArtifact) {
430+
buildCount += artifact.builds.length;
431+
429432
// Generate `main`, `module`, and `browser` fields
430433
if (artifact.inputs.index) {
431434
if (!mainEntry || artifact.platform === 'node') {
@@ -464,10 +467,13 @@ export class Package {
464467
if (mainEntry) {
465468
this.packageJson.main = mainEntry;
466469

467-
if (mainEntry.includes('mjs/') || mainEntry.includes('esm/')) {
468-
this.packageJson.type = 'module';
469-
} else if (mainEntry.includes('cjs/')) {
470-
this.packageJson.type = 'commonjs';
470+
// Only set when we have 1 build, otherwise its confusing
471+
if (buildCount === 1) {
472+
if (mainEntry.includes('mjs/') || mainEntry.includes('esm/')) {
473+
this.packageJson.type = 'module';
474+
} else if (mainEntry.includes('cjs/')) {
475+
this.packageJson.type = 'commonjs';
476+
}
471477
}
472478
}
473479

packages/packemon/tests/Package.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,6 @@ describe('Package', () => {
862862

863863
expect(pkg.packageJson).toEqual(
864864
expect.objectContaining({
865-
type: 'commonjs',
866865
main: './cjs/index.cjs',
867866
bin: './lib/bin.js',
868867
exports: {
@@ -912,7 +911,6 @@ describe('Package', () => {
912911

913912
expect(pkg.packageJson).toEqual(
914913
expect.objectContaining({
915-
type: 'commonjs',
916914
main: './cjs/node.cjs',
917915
bin: './lib/cli.js',
918916
exports: {
@@ -959,7 +957,6 @@ describe('Package', () => {
959957

960958
expect(pkg.packageJson).toEqual(
961959
expect.objectContaining({
962-
type: 'commonjs',
963960
main: './cjs/node.cjs',
964961
bin: './lib/cli.js',
965962
exports: {

0 commit comments

Comments
 (0)