Skip to content

Commit

Permalink
Fix ESM main no exports check
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jan 14, 2023
1 parent 2cb06d9 commit 57fdc43
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export async function publint({ pkgDir, vfs, level, _include }) {
type: 'warning'
})
}
if (expectFormat === 'ESM') {
if (expectFormat === 'ESM' && !exports) {
messages.push({
code: 'HAS_ESM_MAIN_BUT_NO_EXPORTS',
args: {},
Expand Down
2 changes: 1 addition & 1 deletion pkg/src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function printMessage(m, pkg) {
return `${c.bold(fp(m.path))} is ${pv(m.path)} but file does not exist.`
case 'HAS_ESM_MAIN_BUT_NO_EXPORTS':
// prettier-ignore
return `${c.bold('pkg.main')} is an ESM file, but it is usually better to use ${c.bold('pkg.exports')} instead, and remove ${c.bold('pkg.main')} alongside, as compatible NodeJS versions support it as well. (This will be a breaking change)`
return `${c.bold('pkg.main')} is an ESM file, but it is usually better to use ${c.bold('pkg.exports')} instead. If you don't support NodeJS 12.6 and below, you can also remove ${c.bold('pkg.main')}. (This will be a breaking change)`
case 'HAS_MODULE_BUT_NO_EXPORTS':
// prettier-ignore
return `${c.bold('pkg.module')} is used to output ESM, but ${c.bold('pkg.exports')} is not defined. As NodeJS doesn't read ${c.bold('pkg.module')}, the ESM output may be skipped. Consider adding ${c.bold('pkg.exports')} to export the ESM output. ${c.bold('pkg.module')} can usually be removed alongside too. (This will be a breaking change)`
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/test-2/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
1 change: 1 addition & 0 deletions pkg/tests/fixtures/test-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-test-2",
"version": "0.0.1",
"private": true,
"main": "./main.mjs",
"browser": "./lib/bar.js",
"exports": {
"./*.js": "./lib/*.js",
Expand Down
12 changes: 8 additions & 4 deletions site/rules.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ <h2><code>HAS_MODULE_BUT_NO_EXPORTS</code></h2>
</p>
<h2><code>HAS_ESM_MAIN_BUT_NO_EXPORTS</code></h2>
<p>
If the <code>main</code> entrypoint is an ES module, you can likely
remove it as environments that supports so also supports
<code>exports</code>, which should be used instead, and the
<code>main</code> can then be removed alongside too.
If the <code>main</code> entrypoint is an ES module, it's recommended to
use the <code>exports</code> field instead as it's initially introduced
for better ESM compatibility.
</p>
<p>
If you're not supporting NodeJS 12.6 and below, you can also remove the
<code>main</code> field as all tooling would read from
<code>exports</code> only and skip <code>main</code>.
</p>
<h2><code>EXPORTS_GLOB_NO_MATCHED_FILES</code></h2>
<p>
Expand Down
2 changes: 1 addition & 1 deletion site/src/utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function printMessage(m, pkg) {
return `File does not exist`
case 'HAS_ESM_MAIN_BUT_NO_EXPORTS':
// prettier-ignore
return `${bold('pkg.main')} is an ESM file, but it is usually better to use ${bold('pkg.exports')} instead, and remove ${bold('pkg.main')} alongside, as compatible NodeJS versions support it as well. (This will be a breaking change)`
return `${bold('pkg.main')} is an ESM file, but it is usually better to use ${bold('pkg.exports')} instead. If you don't support NodeJS 12.6 and below, you can also remove ${bold('pkg.main')}. (This will be a breaking change)`
case 'HAS_MODULE_BUT_NO_EXPORTS':
// prettier-ignore
return `${bold('pkg.module')} is used to output ESM, but ${bold('pkg.exports')} is not defined. As NodeJS doesn't read ${bold('pkg.module')}, the ESM output may be skipped. Consider adding ${bold('pkg.exports')} to export the ESM output. ${bold('pkg.module')} can usually be removed alongside too. (This will be a breaking change)`
Expand Down

0 comments on commit 57fdc43

Please sign in to comment.