Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that module preceeds import and require in exports definitions #49

Merged
merged 7 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export type Message =
}
>
| BaseMessage<'EXPORTS_TYPES_SHOULD_BE_FIRST'>
| BaseMessage<'EXPORTS_MODULE_SHOULD_PRECEED_IMPORT'>
| BaseMessage<'EXPORTS_MODULE_SHOULD_PRECEED_REQUIRE'>
bluwy marked this conversation as resolved.
Show resolved Hide resolved
| BaseMessage<'EXPORTS_DEFAULT_SHOULD_BE_LAST'>
| BaseMessage<'EXPORTS_MODULE_SHOULD_BE_ESM'>
| BaseMessage<'EXPORTS_VALUE_INVALID', { suggestValue: string }>
Expand Down
28 changes: 28 additions & 0 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,34 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
}
}

// a 'module' export should always preceed 'require'
if (
'module' in exports &&
'require' in exports &&
exportsKeys.indexOf('module') > exportsKeys.indexOf('require')
) {
messages.push({
code: 'EXPORTS_MODULE_SHOULD_PRECEED_REQUIRE',
args: {},
path: currentPath.concat('module'),
type: 'error'
})
}

// a 'module' export should always preceed 'import'
if (
'module' in exports &&
'import' in exports &&
exportsKeys.indexOf('module') > exportsKeys.indexOf('import')
) {
messages.push({
code: 'EXPORTS_MODULE_SHOULD_PRECEED_IMPORT',
args: {},
path: currentPath.concat('module'),
type: 'error'
})
}

// the default export should be the last condition
if (
'default' in exports &&
Expand Down
6 changes: 6 additions & 0 deletions pkg/src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export function printMessage(m, pkg) {
case 'EXPORTS_TYPES_SHOULD_BE_FIRST':
// prettier-ignore
return `${c.bold(fp(m.path))} should be the first in the object as required by TypeScript.`
case 'EXPORTS_MODULE_SHOULD_PRECEED_REQUIRE':
// prettier-ignore
return `${c.bold(fp(m.path))} should come before 'require' so it can take precedence when used by a bundler.`
case 'EXPORTS_MODULE_SHOULD_PRECEED_IMPORT':
// prettier-ignore
return `${c.bold(fp(m.path))} should come before 'import' so it can take precedence when used by a bundler.`
bluwy marked this conversation as resolved.
Show resolved Hide resolved
case 'EXPORTS_DEFAULT_SHOULD_BE_LAST':
// prettier-ignore
return `${c.bold(fp(m.path))} should be the last in the object so it doesn't take precedence over the keys following it.`
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/exports-module/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'foo'
1 change: 1 addition & 0 deletions pkg/tests/fixtures/exports-module/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar'
12 changes: 12 additions & 0 deletions pkg/tests/fixtures/exports-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "publint-test-exports-module",
"version": "0.0.1",
"private": true,
"exports": {
".": {
"import": "./main.mjs",
"require": "./main.js",
"module": "./main.mjs"
}
}
}
5 changes: 5 additions & 0 deletions pkg/tests/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ testFixture('missing-files', [

testFixture('no-exports-module', [])

testFixture('exports-module', [
'EXPORTS_MODULE_SHOULD_PRECEED_REQUIRE',
'EXPORTS_MODULE_SHOULD_PRECEED_IMPORT'
])

testFixture('publish-config', ['USE_EXPORTS_BROWSER', 'FILE_DOES_NOT_EXIST'])

testFixture('test-1', ['TYPES_NOT_EXPORTED', 'FILE_INVALID_FORMAT'])
Expand Down
8 changes: 8 additions & 0 deletions site/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ If the `exports` field contains glob paths, but it doesn't match any files, repo

The `exports` field should not have globs defined with trailing slashes. It is [deprecated](https://nodejs.org/docs/latest-v16.x/api/packages.html#subpath-folder-mappings) and should use [subpath patterns](https://nodejs.org/api/packages.html#subpath-patterns), e.g. a trailing `/*` instead.

## `EXPORTS_MODULE_SHOULD_PRECEED_IMPORT`

Ensure `module` condition to come before `import` condition. Due to the way conditions are matched top-to-bottom, the `module` condition (used in bundler contexts only) must come before an `import` definition, so it has the opportunity to take precedence.

## `EXPORTS_MODULE_SHOULD_PRECEED_REQUIRE`

Ensure `module` condition to come before `require` condition. Due to the way conditions are matched top-to-bottom, the `module` condition (used in bundler contexts only) must come before a `require` definition, so it has the opportunity to take precedence.

## `EXPORTS_TYPES_SHOULD_BE_FIRST`

Ensure `types` condition to be the first. The [TypeScript docs](https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing) recommends so, but it's also because the `exports` field is order-based.
Expand Down