-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Fix types and exports of the package #197
Conversation
🦋 Changeset detectedLatest commit: 4b6fabe The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested on our code base and it seems to work well. 👍🏽
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a risky change. Changing the exports
field is almost always a major breaking change, due to how different versions of Node, bundlers, etc handle the exports field. What version of typescript started supporting the exports
field rather than types
in package.json?
NOTE:
Due to rollup not inserting a compatibility layer when transpiling, in rare instances, consumers might have to access the main export with ".default". This is caused by a mix of named, and default exports.
The above sounds like it might be a breaking change.
.changeset/strong-otters-brush.md
Outdated
@@ -0,0 +1,5 @@ | |||
--- | |||
"single-spa-react": patch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever the exports
field changes in the package.json, it might need to be a major change because of how different bundlers and NodeJS versions handle it.
"single-spa-react": patch | |
"single-spa-react": major |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix should handle all resolution strategies (all typescript strategies), along with all popular bundlers. Still, if someone is using an obscure tool, it might behave differently, so we could treat it as a breaking change. Problem is not only with the typescript version, but a combination of typescript "moduleResolution", and bundlers. Currently, create-single-spa uses the "node" (which is actually node10) resolution strategy. What do you think?
I have inserted a compatibility layer that fixes the issue with certain bundlers requiring access to default export with ".default". Now, all of the resolution strategies are correctly handled:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Being totally honest, I would prefer calling it a major version just in case there's a weird edge case where it changes how things work. I don't know all the edge cases, but have had lots of github issues after changing my exports
fields before.
I prefer single-spa-react users face no risk when doing patch upgrades.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it to Major.
scripts/build-types.js
Outdated
@@ -0,0 +1,10 @@ | |||
import fs from "fs"; | |||
|
|||
function copyFile(sourcePath, destinationPath) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: calling fs.copyFileSync
directly is simpler than extracting out a reusable copyFile function. Please remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the script, as it is no longer needed.
package.json
Outdated
@@ -2,7 +2,7 @@ | |||
"name": "single-spa-react", | |||
"version": "5.1.4", | |||
"description": "Single-spa lifecycles helper for React apps", | |||
"main": "lib/umd/single-spa-react.js", | |||
"main": "lib/cjs/single-spa-react.cjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the main
field from UMD to CJS is a breaking change. Please update the changeset to be a major
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change this back - we can discuss the reasons for changing to CJS in Slack and possibly in another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted to UMD.
src/single-spa-react.js
Outdated
@@ -375,3 +375,15 @@ function createSingleSpaRoot(opts) { | |||
|
|||
return SingleSpaRoot; | |||
} | |||
|
|||
/** | |||
* If module is running in a CommonJS environment, a compatibility layer for node16 resolution strategy is inserted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't add extra changes in subsequent commits
Parcel as SingleSpaParcel, | ||
} from "single-spa"; | ||
|
||
interface ParcelCompProps<ExtraProps = {}> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be moved to a shared declarations file, and then imported into both the .d.ts and .d.cts files?
import { ParcelCompProps } from './shared.d.ts';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you agree, we can leave this as-is for now, since these files will not be needed when we migrate to typescript.
parcel/package.json
Outdated
@@ -0,0 +1,4 @@ | |||
{ | |||
"main": "../lib/cjs/parcel.cjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module format for this main field should match the format for the single-spa-react package.json (UMD)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/single-spa-react.js
Outdated
* During transpilation, Rollup correctly inserts the "exports.default", and "exports.__esModule" properties, but they are not respected by some bundlers. | ||
* For more information see https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSOnlyExportsDefault.md | ||
*/ | ||
if (typeof module !== "undefined" && module.exports) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this, as discussed in the call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
Fixes #170.
This fix employs several strategies to achieve compatibility with all resolution strategies (node10, node16 - cjs, node16 - esm, bundler).
Here is the analysis by https://arethetypeswrong.github.io/.
NOTE:
Due to rollup not inserting a compatibility layer when transpiling, in rare instances, consumers might have to access the main export with ".default". This is caused by a mix of named, and default exports.