Skip to content

Commit

Permalink
Suggest adding type field
Browse files Browse the repository at this point in the history
closes #83
  • Loading branch information
bluwy committed Dec 23, 2023
1 parent 16facaa commit a0e16e8
Show file tree
Hide file tree
Showing 24 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type Message =
| BaseMessage<'USE_EXPORTS_BROWSER'>
| BaseMessage<'USE_EXPORTS_OR_IMPORTS_BROWSER'>
| BaseMessage<'USE_FILES'>
| BaseMessage<'USE_TYPE'>
| BaseMessage<
'TYPES_NOT_EXPORTED',
{
Expand Down
11 changes: 11 additions & 0 deletions pkg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ export async function publint({ pkgDir, vfs, level, strict, _packedFiles }) {
})
}

// Check if "type" field is specified, help Node.js push towards an ESM default future:
// https://nodejs.org/en/blog/release/v20.10.0
if (rootPkg.type == null) {
messages.push({
code: 'USE_TYPE',
args: {},
path: ['name'],
type: 'suggestion'
})
}

// Relies on default node resolution
// https://nodejs.org/api/modules.html#all-together
// LOAD_INDEX(X)
Expand Down
3 changes: 3 additions & 0 deletions pkg/src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export function formatMessage(m, pkg) {
case 'USE_FILES':
// prettier-ignore
return `The package ${c.bold('publishes internal tests or config files')}. You can use ${c.bold('pkg.files')} to only publish certain files and save user bandwidth.`
case 'USE_TYPE':
// prettier-ignore
return `The package does not specify the ${c.bold('type')} field. Environments may incorrectly identify a CJS file as ESM in the future. Consider adding ${c.bold('"type"')}: "${c.bold('commonjs')}".`
case 'TYPES_NOT_EXPORTED': {
const typesFilePath = exportsRel(m.args.typesFilePath)
if (m.args.actualExtension && m.args.expectExtension) {
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/exports-browser-conflict/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-exports-browser-conflict",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"browser": {
"./lib.server.js": "./lib.browser.js"
},
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/exports-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-exports-module",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"exports": {
".": {
"import": "./main.mjs",
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/exports-styles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-exports-styles",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"exports": {
"./array": {
"import": ["./array.js", "./non-existent.js", "std:lib"]
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/glob-deprecated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-glob-deprecated",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"exports": {
"./": "./src/"
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/tests/fixtures/glob/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "publint-glob",
"version": "0.0.1",
"private": true
"private": true,
"type": "commonjs"
}
1 change: 1 addition & 0 deletions pkg/tests/fixtures/invalid-field-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-invalid-field-types",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"main": 0,
"module": true,
"jsnext:main": false
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/invalid-jsx-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-invalid-jsx-extensions",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"main": "./main.cjsx",
"module": "./main.mjsx"
}
1 change: 1 addition & 0 deletions pkg/tests/fixtures/missing-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-missing-files",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"main": "missing.js",
"jsnext:main": "./missing.js",
"jsnext": "./missing.js",
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/not-missing-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "publint-not-missing-files",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"main": "index"
}
1 change: 1 addition & 0 deletions pkg/tests/fixtures/publish-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-publish-config",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"main": "missing.js",
"browser": "missing.js",
"exports": {
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/test-1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "publint-test-1",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"exports": "./main.js"
}
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,
"type": "commonjs",
"main": "./main.mjs",
"browser": "./lib/bar.js",
"typings": "./doesn't-exist.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-types-exports-resolution-cjs",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"types": "./main.d.ts",
"exports": {
".": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-types-exports-resolution-dual-explicit",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"types": "./main-special.d.ts",
"exports": {
".": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-types-exports-resolution-dual",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"types": "./main-special.d.ts",
"exports": {
".": {
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/types-exports-resolution/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-types-exports-resolution",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"types": "./main.d.ts",
"exports": {
".": {
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/fixtures/types-versions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-types-versions",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"types": "./main.d.ts",
"typesVersions": {}
}
1 change: 1 addition & 0 deletions pkg/tests/fixtures/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "publint-types",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"types": "./main.d.ts",
"exports": {
".": {
Expand Down
3 changes: 2 additions & 1 deletion pkg/tests/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ testFixture('umd', ['FILE_INVALID_FORMAT', 'FILE_INVALID_FORMAT'])

testFixture('deprecated-fields', [
'DEPRECATED_FIELD_JSNEXT',
'DEPRECATED_FIELD_JSNEXT'
'DEPRECATED_FIELD_JSNEXT',
'USE_TYPE'
])

/**
Expand Down
4 changes: 4 additions & 0 deletions site/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ Internal tests or config files are published, which are usually not needed and u
}
```

## `USE_TYPE`

Since [Node.js v20.10.0](https://nodejs.org/en/blog/release/v20.10.0), it introduces a new `--experimental-default-type` flag to flip the default module system from "CJS-as-default" to "ESM-as-default". If enabled, `package.json` without the `"type"` field will mean its descendant JS files to be interpreted as ESM instead of CJS, which may not work correctly. Hence, if you've not set the `"type"` field, it's recommended to set as `"type": "commonjs"` (default value) to be future-proof.

## `FIELD_INVALID_VALUE_TYPE`

Some `package.json` fields has a set of allowed types, e.g. `string` or `object` only. If an invalid type is passed, this error message will be showed.
Expand Down
3 changes: 3 additions & 0 deletions site/src/utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ function messageToString(m, pkg) {
case 'USE_FILES':
// prettier-ignore
return `The package ${bold('publishes internal tests or config files')}. You can use ${bold('pkg.files')} to only publish certain files and save user bandwidth.`
case 'USE_TYPE':
// prettier-ignore
return `The package does not specify the ${bold('"type"')} field. Environments may incorrectly identify a CJS file as ESM in the future. Consider adding ${bold('"type"')}: "${bold('commonjs')}".`
case 'TYPES_NOT_EXPORTED': {
const typesFilePath = exportsRel(m.args.typesFilePath)
if (m.args.actualExtension && m.args.expectExtension) {
Expand Down

0 comments on commit a0e16e8

Please sign in to comment.