Skip to content

Commit

Permalink
set type:module in package.json only if main is not cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Sep 26, 2023
1 parent d460568 commit 56e018c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ export const setMain = (
}
const types = resolveExport(pkg.exports['.'], [m, 'types'])
pkg.main = mod
if (c.main === 'esm') pkg.type = 'module'
else delete pkg.type
if (types && types !== mod) pkg.types = types
else delete pkg.types
} else {
pkg.type = 'module'
delete pkg.main
delete pkg.types
}
Expand Down
9 changes: 6 additions & 3 deletions test/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ t.test('setting top level main', async t => {
exports: Record<string, Export>
main?: string
types?: string
type?: string
},
{
main?: string
types?: string
type?: string
},
boolean
][] = [
Expand All @@ -91,7 +93,7 @@ t.test('setting top level main', async t => {
},
},
},
{},
{ type: 'module' },
true,
],
[
Expand Down Expand Up @@ -119,7 +121,7 @@ t.test('setting top level main', async t => {
},
},
},
{ main: './i.js', types: './i.d.ts' },
{ type: 'module', main: './i.js', types: './i.d.ts' },
true,
],
[
Expand Down Expand Up @@ -147,7 +149,7 @@ t.test('setting top level main', async t => {
},
},
},
{ main: './i.js' },
{ main: './i.js', type: 'module' },
true,
],
[
Expand Down Expand Up @@ -241,6 +243,7 @@ t.test('setting top level main', async t => {
if (ok) {
t.equal(pkg.main, expect.main)
t.equal(pkg.types, expect.types)
t.equal(pkg.type, expect.type)
} else {
t.strictSame(exits(), [[1]])
t.matchSnapshot(fails)
Expand Down

0 comments on commit 56e018c

Please sign in to comment.