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

Use package.json#exports #1185

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions .changeset/healthy-cobras-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@changesets/get-version-range-type": minor
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of those are in the 0.x range so I could consider using patch for them - it doesn't quite matter for them though anyway so I'm just using minor for every pkg here

"@changesets/assemble-release-plan": minor
"@changesets/get-dependents-graph": minor
"@changesets/apply-release-plan": minor
"@changesets/changelog-github": minor
"@changesets/get-release-plan": minor
"@changesets/get-github-info": minor
"@changesets/changelog-git": minor
"@changesets/release-utils": minor
"@changesets/test-utils": minor
"@changesets/config": minor
"@changesets/errors": minor
"@changesets/logger": minor
"@changesets/parse": minor
"@changesets/types": minor
"@changesets/write": minor
"@changesets/read": minor
"@changesets/cli": minor
"@changesets/git": minor
"@changesets/pre": minor
---

`package.json#exports` have been added to limit what (and how) code might be imported from the package.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@babel/preset-typescript": "^7.18.6",
"@babel/runtime": "^7.20.1",
"@manypkg/cli": "^0.19.1",
"@preconstruct/cli": "^1.1.26",
"@preconstruct/cli": "^2.8.1",
"@types/fs-extra": "^5.1.0",
"@types/jest": "^24.0.12",
"@types/jest-in-case": "^1.0.6",
Expand All @@ -65,13 +65,19 @@
"jest-junit": "^15.0.0",
"jest-watch-typeahead": "^2.2.1",
"prettier": "^2.7.1",
"typescript": "^4.8.4"
"typescript": "^5.1.6"
},
"preconstruct": {
"packages": [
"packages/*",
"scripts/*"
]
],
"exports": {
"importConditionDefaultExport": "default"
},
"___experimentalFlags_WILL_CHANGE_IN_PATCH": {
"importsConditions": true
}
},
"prettier": {}
}
16 changes: 14 additions & 2 deletions packages/apply-release-plan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/apply-release-plan",
"version": "6.1.4",
"description": "Takes a release plan and applies it to packages",
"main": "dist/apply-release-plan.cjs.js",
"module": "dist/apply-release-plan.esm.js",
"main": "dist/changesets-apply-release-plan.cjs.js",
"module": "dist/changesets-apply-release-plan.esm.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this field will have no effect. it will be ignored in favor of exports and can be removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's here to support older bundlers that don't support exports

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair, I guess. Though it seems hard to imagine that people are both bundling changeset packages to create their own changeset tools and doing so with a bundler that's that old

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but this is auto-generated by a somewhat universal library builder ( https://github.com/preconstruct/preconstruct/ ) and for a lot of npm packages it might still make sense to have proper compat with webpack 4 etc.

"exports": {
".": {
"types": {
"import": "./dist/changesets-apply-release-plan.cjs.mjs",
"default": "./dist/changesets-apply-release-plan.cjs.js"
},
"module": "./dist/changesets-apply-release-plan.esm.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is totally non-standard. is it really necessary? it would be much nicer not to have it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is totally standard :p it's just not described by node - the exports "standard" is meant to be extensible. This condition was born out of the collaboration between webpack and Rollup maintainers when it became clear that the dual package hazard is a thing. To mitigate the problem in bundlers that would like to apply node semantics closely they invented this condition that kinda works like the "old" package.json#module. Both require and import loaders in bundlers are able to load this, even though this file is authored in ESM. This is also adopted by other bundlers like Vite, bun, Parcel, esbuild and more - so I'd call it pretty standard :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you for educating me! I had no idea Rollup supported this, but it looks like it's been in @rollup/plugin-node since 11.0.0. Very much appreciate the informative responses!

"import": "./dist/changesets-apply-release-plan.cjs.mjs",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this in the nicest possible way, but it is absolutely crazy to use .cjs.mjs as a file extension since a file cannot be both CJS and ESM. Assuming this file is actually ESM, can the extension be just .mjs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an MJS wrapper - it's an ESM file that reexports CJS file. I know it looks funny but it kinda makes perfect sense to us. It's also kinda irrelevant rly - the extension is .mjs here. The other dotted part is not considered to be an extension, we just use it to denote some additional meaning that is separate from the package name (hence the need to use a different delimiter than a dash)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's helpful to know. While I agree it's irrelevant to consumers of the package, it isn't necessarily intuitive to people who might want to contribute to it. Something like -wrapper.mjs might be a little clearer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, usually you just don't touch those files at all. When contributing you just interact directly with sources and all of this here is auto-generated for us. I appreciate the feedback but at the end of the day, I consider this particular thing a very minor detail that is kinda irrelevant. I can see how it might be surprising at first glance though.

"default": "./dist/changesets-apply-release-plan.cjs.js"
},
"./package.json": "./package.json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid we started this convention because of some ill-behaved versions of rollup-plugin-svelte that would tell users to inform package authors to add it. It's really not necessary though and rollup-plugin-svelte no longer has that behavior

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to support require('pkg/package.json') in node. We think that there is no big cost associated with supporting this and some node_modules crawlers (and similar tools) are using this to inspect package metadata.

},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/apply-release-plan",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
/*
BAD CODE ALERT!

You should never reach out of one package and into another in a multi-package repository.
(doing so is a leading cause of 'works on my machine' but then failure when the packages are published)

We are doing it here to avoide adding a circular dependency and as this is only used in testing.

This is wicked, and please don't copy us.
*/

export { default } from "../../../cli/changelog";
// We are doing it here to avoide adding a circular dependency and as this is only used in testing.
// This is wicked, and please don't copy us.
// eslint-disable-next-line import/no-extraneous-dependencies
export { default } from "@changesets/cli/changelog";
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
/*
BAD CODE ALERT!

You should never reach out of one package and into another in a multi-package repository.
(doing so is a leading cause of 'works on my machine' but then failure when the packages are published)

We are doing it here to avoide adding a circular dependency and as this is only used in testing.

This is wicked, and please don't copy us.
*/

export { default } from "../../../cli/commit";
// We are doing it here to avoide adding a circular dependency and as this is only used in testing.
// This is wicked, and please don't copy us.
// eslint-disable-next-line import/no-extraneous-dependencies
export { default } from "@changesets/cli/commit";
16 changes: 14 additions & 2 deletions packages/assemble-release-plan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/assemble-release-plan",
"version": "5.2.4",
"description": "Reads changesets and adds information on dependents that need bumping",
"main": "dist/assemble-release-plan.cjs.js",
"module": "dist/assemble-release-plan.esm.js",
"main": "dist/changesets-assemble-release-plan.cjs.js",
"module": "dist/changesets-assemble-release-plan.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-assemble-release-plan.cjs.mjs",
"default": "./dist/changesets-assemble-release-plan.cjs.js"
},
"module": "./dist/changesets-assemble-release-plan.esm.js",
"import": "./dist/changesets-assemble-release-plan.cjs.mjs",
"default": "./dist/changesets-assemble-release-plan.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/assemble-release-plan",
"dependencies": {
Expand Down
16 changes: 14 additions & 2 deletions packages/changelog-git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/changelog-git",
"version": "0.1.14",
"description": "A changelog entry generator for git that writes hashes",
"main": "dist/changelog-git.cjs.js",
"module": "dist/changelog-git.esm.js",
"main": "dist/changesets-changelog-git.cjs.js",
"module": "dist/changesets-changelog-git.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-changelog-git.cjs.mjs",
"default": "./dist/changesets-changelog-git.cjs.js"
},
"module": "./dist/changesets-changelog-git.esm.js",
"import": "./dist/changesets-changelog-git.cjs.mjs",
"default": "./dist/changesets-changelog-git.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/changelog-git",
"dependencies": {
Expand Down
16 changes: 14 additions & 2 deletions packages/changelog-github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/changelog-github",
"version": "0.4.8",
"description": "A changelog entry generator for GitHub that links to commits, PRs and users",
"main": "dist/changelog-github.cjs.js",
"module": "dist/changelog-github.esm.js",
"main": "dist/changesets-changelog-github.cjs.js",
"module": "dist/changesets-changelog-github.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-changelog-github.cjs.mjs",
"default": "./dist/changesets-changelog-github.cjs.js"
},
"module": "./dist/changesets-changelog-github.esm.js",
"import": "./dist/changesets-changelog-github.cjs.mjs",
"default": "./dist/changesets-changelog-github.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/changelog-github",
"dependencies": {
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/changelog/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"main": "dist/cli.cjs.js",
"module": "dist/cli.esm.js",
"preconstruct": {
"source": "../src/changelog"
}
"main": "dist/changesets-cli-changelog.cjs.js",
"module": "dist/changesets-cli-changelog.esm.js"
}
7 changes: 2 additions & 5 deletions packages/cli/commit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"main": "dist/cli.cjs.js",
"module": "dist/cli.esm.js",
"preconstruct": {
"source": "../src/commit"
}
"main": "dist/changesets-cli-commit.cjs.js",
"module": "dist/changesets-cli-commit.esm.js"
}
40 changes: 35 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,38 @@
"changelog",
"commit"
],
"main": "dist/cli.cjs.js",
"module": "dist/cli.esm.js",
"main": "dist/changesets-cli.cjs.js",
"module": "dist/changesets-cli.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-cli.cjs.mjs",
"default": "./dist/changesets-cli.cjs.js"
},
"module": "./dist/changesets-cli.esm.js",
"import": "./dist/changesets-cli.cjs.mjs",
"default": "./dist/changesets-cli.cjs.js"
},
"./changelog": {
"types": {
"import": "./changelog/dist/changesets-cli-changelog.cjs.mjs",
"default": "./changelog/dist/changesets-cli-changelog.cjs.js"
},
"module": "./changelog/dist/changesets-cli-changelog.esm.js",
"import": "./changelog/dist/changesets-cli-changelog.cjs.mjs",
"default": "./changelog/dist/changesets-cli-changelog.cjs.js"
},
"./commit": {
"types": {
"import": "./commit/dist/changesets-cli-commit.cjs.mjs",
"default": "./commit/dist/changesets-cli-commit.cjs.js"
},
"module": "./commit/dist/changesets-cli-commit.esm.js",
"import": "./commit/dist/changesets-cli-commit.cjs.mjs",
"default": "./commit/dist/changesets-cli-commit.cjs.js"
},
"./package.json": "./package.json"
},
"author": "Changesets Contributors",
"contributors": [
"Ben Conolly",
Expand All @@ -23,9 +53,9 @@
],
"preconstruct": {
"entrypoints": [
".",
"changelog",
"commit"
"./index.ts",
"./changelog.ts",
"./commit/index.ts"
]
},
"license": "MIT",
Expand Down
16 changes: 14 additions & 2 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/config",
"version": "2.3.1",
"description": "Utilities for reading and parsing Changeset's config",
"main": "dist/config.cjs.js",
"module": "dist/config.esm.js",
"main": "dist/changesets-config.cjs.js",
"module": "dist/changesets-config.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-config.cjs.mjs",
"default": "./dist/changesets-config.cjs.js"
},
"module": "./dist/changesets-config.esm.js",
"import": "./dist/changesets-config.cjs.mjs",
"default": "./dist/changesets-config.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/config",
"files": [
Expand Down
16 changes: 14 additions & 2 deletions packages/errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/errors",
"version": "0.1.4",
"description": "Error classes for @changesets",
"main": "dist/errors.cjs.js",
"module": "dist/errors.esm.js",
"main": "dist/changesets-errors.cjs.js",
"module": "dist/changesets-errors.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-errors.cjs.mjs",
"default": "./dist/changesets-errors.cjs.js"
},
"module": "./dist/changesets-errors.esm.js",
"import": "./dist/changesets-errors.cjs.mjs",
"default": "./dist/changesets-errors.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/errors",
"dependencies": {
Expand Down
16 changes: 14 additions & 2 deletions packages/get-dependents-graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/get-dependents-graph",
"version": "1.3.6",
"description": "Get the graph of dependents in a monorepo",
"main": "dist/get-dependents-graph.cjs.js",
"module": "dist/get-dependents-graph.esm.js",
"main": "dist/changesets-get-dependents-graph.cjs.js",
"module": "dist/changesets-get-dependents-graph.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-get-dependents-graph.cjs.mjs",
"default": "./dist/changesets-get-dependents-graph.cjs.js"
},
"module": "./dist/changesets-get-dependents-graph.esm.js",
"import": "./dist/changesets-get-dependents-graph.cjs.mjs",
"default": "./dist/changesets-get-dependents-graph.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/get-dependents-graph",
"dependencies": {
Expand Down
16 changes: 14 additions & 2 deletions packages/get-github-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/get-github-info",
"version": "0.5.2",
"description": "Get the GitHub username and PR number from a commit. Intended for use with changesets.",
"main": "dist/get-github-info.cjs.js",
"module": "dist/get-github-info.esm.js",
"main": "dist/changesets-get-github-info.cjs.js",
"module": "dist/changesets-get-github-info.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-get-github-info.cjs.mjs",
"default": "./dist/changesets-get-github-info.cjs.js"
},
"module": "./dist/changesets-get-github-info.esm.js",
"import": "./dist/changesets-get-github-info.cjs.mjs",
"default": "./dist/changesets-get-github-info.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/get-github-info",
"dependencies": {
Expand Down
16 changes: 14 additions & 2 deletions packages/get-release-plan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/get-release-plan",
"version": "3.0.17",
"description": "Reads changesets and adds information on dependents that need bumping",
"main": "dist/get-release-plan.cjs.js",
"module": "dist/get-release-plan.esm.js",
"main": "dist/changesets-get-release-plan.cjs.js",
"module": "dist/changesets-get-release-plan.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-get-release-plan.cjs.mjs",
"default": "./dist/changesets-get-release-plan.cjs.js"
},
"module": "./dist/changesets-get-release-plan.esm.js",
"import": "./dist/changesets-get-release-plan.cjs.mjs",
"default": "./dist/changesets-get-release-plan.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/get-release-plan",
"dependencies": {
Expand Down
16 changes: 14 additions & 2 deletions packages/get-version-range-type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
"name": "@changesets/get-version-range-type",
"version": "0.3.2",
"description": "Common get-version-range-type shared between changeset packages",
"main": "dist/get-version-range-type.cjs.js",
"module": "dist/get-version-range-type.esm.js",
"main": "dist/changesets-get-version-range-type.cjs.js",
"module": "dist/changesets-get-version-range-type.esm.js",
"exports": {
".": {
"types": {
"import": "./dist/changesets-get-version-range-type.cjs.mjs",
"default": "./dist/changesets-get-version-range-type.cjs.js"
},
"module": "./dist/changesets-get-version-range-type.esm.js",
"import": "./dist/changesets-get-version-range-type.cjs.mjs",
"default": "./dist/changesets-get-version-range-type.cjs.js"
},
"./package.json": "./package.json"
},
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/main/packages/get-version-range-type"
}