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

chore: switch to esm #22

Merged
merged 17 commits into from
Aug 16, 2021

Conversation

vasco-santos
Copy link
Collaborator

@vasco-santos vasco-santos commented Jul 22, 2021

BREAKING CHANGE: built content includes ESM and CJS

Needs:

@vasco-santos
Copy link
Collaborator Author

This should be ready, just needs a new aegir. It would be helpful to have a review @achingbrain

package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@achingbrain
Copy link
Owner

I think all the files need to switch over to using named exports, otherwise this innocuous looking sort of thing:

/**
 * @typedef {import('foo').Bar} Bar
 */

module.exports = function baz () {}

Generates:

export type Bar = import('foo').Bar;
export default function baz(): void;

and ipjs says to not mix default and named exports.

package.json Outdated Show resolved Hide resolved
@achingbrain
Copy link
Owner

I think all the files need to switch over to using named exports

Actually maybe not, we can just remove the @typedef and import the type directly in the function's @param annotation, this solves that problem.

Still getting some weird typeof import('path/to/uint8arrays/types/src/from-string') has no call signatures errors though.

@achingbrain
Copy link
Owner

No, we have to switch to named exports.

has no call signatures - this is caused by ts wanting:

const fromString = require('uint8arrays/from-string')

to be:

const fromString = require('uint8arrays/from-string').default

which compiles types but then fails at runtime in node due to "require" in the exports map resolving to the .cjs file which doesn't have a .default property:

"exports": {
  "browser": "./esm/src/from-string.js",
  "require": "./cjs/src/from-string.js",
  "import": "./esm/src/from-string.js"
}

Ignoring that for a second, if we run bundled cjs code in the browser, "exports" means the browser gets supplied the esm version, which does have a .default property so breaks - fixing this would require a PR to ipjs to change this line to supply the cjs version instead or at least make it an option. Do we want that? We want to live in the bundler-free future so probably not.

Ingoring that for a second, if we follow my suggestion above:

we can just remove the @typedef and import the type directly in the function's @param annotation

we're breaking compatibility because we previously exported a SupportedEncodings type from to-string and from-string which would be removed, so we'd need a major.

We could ditch cjs, which would require all cjs users to change all instances of require('uint8arrays/from-string') to require('uint8arrays/from-string').default so we'd need a major.

Instead we can switch to named exports, which will need a major, but then people don't need to use .default and we can retain the SupportedEncodings type.

As ever, this springs to mind:

image

@vasco-santos
Copy link
Collaborator Author

Thanks for the detailed feedback. I agree that the best solution is to make named exports and I will change it

vasco-santos and others added 5 commits July 28, 2021 13:00
BREAKING CHANGE: built content includes ESM and CJS
Co-authored-by: Alex Potsides <alex@achingbrain.net>
Co-authored-by: Alex Potsides <alex@achingbrain.net>
@vasco-santos
Copy link
Collaborator Author

types tested on ipfs/js-ipfs#3774

@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2021

Codecov Report

Merging #22 (fa58e00) into master (c7a0601) will decrease coverage by 0.19%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #22      +/-   ##
==========================================
- Coverage   98.35%   98.15%   -0.20%     
==========================================
  Files           7        7              
  Lines         243      217      -26     
  Branches       44       44              
==========================================
- Hits          239      213      -26     
  Misses          4        4              
Impacted Files Coverage Δ
src/compare.js 100.00% <100.00%> (ø)
src/concat.js 100.00% <100.00%> (ø)
src/equals.js 91.30% <100.00%> (ø)
src/from-string.js 100.00% <100.00%> (ø)
src/to-string.js 100.00% <100.00%> (ø)
src/util/bases.js 100.00% <100.00%> (ø)
src/xor.js 89.47% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c7a0601...fa58e00. Read the comment docs.

@@ -17,9 +17,6 @@ jobs:
- uses: gozala/typescript-error-reporter-action@v1.0.8
- run: npx aegir build
- run: npx aegir dep-check
- uses: ipfs/aegir/actions/bundle-size@master
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this needs to be added again once aegir ships it as I could not make it use a branch

Copy link
Owner

Choose a reason for hiding this comment

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

I've restored this but now CI is failing on this step with:

Error: Cannot find module '/github/workspace/index.js'

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh damn, I will look into it

achingbrain added a commit to ipfs/aegir that referenced this pull request Aug 14, 2021
This builds up on @achingbrain 's work on #863 with build improvements and full support

This adds:
- `ipjs` for ESM modules
  - auto-detected
- `types` property transformation, allowing real time ts check in dev and path update for dist folder (TLDR no dist in the path)
- `release` for ESM modules will navigate to the dist to publish its content
- Dockerfile to bundlesize action per actions/runner#772 (comment) as we need node14+ for ESM

One of the problematic modules in skypack using aegir is `uint8arrays`. It is a CJS module that depends on a ESM first module (multiformats), which makes skypack to get bad dependency paths.

I tested this out shipping `uint8arrays` achingbrain/uint8arrays#22 PR and everything working smoothly 🎉 

Original release: https://codepen.io/vascosantos/pen/KKmXoPV?editors=0011
Scoped release using `aegir`: https://codepen.io/vascosantos/pen/bGWoONq?editors=0011

(see browser built in console for errors)

Co-authored-by: achingbrain <alex@achingbrain.net>
package.json Outdated
Comment on lines 70 to 90
"./index.js": {
"import": "./src/index.js"
},
"./compare.js": {
"import": "./src/compare.js"
},
"./concat.js": {
"import": "./src/concat.js"
},
"./equals.js": {
"import": "./src/equals.js"
},
"./from-string.js": {
"import": "./src/from-string.js"
},
"./to-string.js": {
"import": "./src/to-string.js"
},
"./xor.js": {
"import": "./src/xor.js"
}
Copy link
Owner

Choose a reason for hiding this comment

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

Why do we need to double up these exports? Could be quite error prone over time, maybe we could get ipjs to do this for us?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We talked about this offline a couple of weeks. Anyway, we do not need this anymore. Removed now

@vasco-santos vasco-santos marked this pull request as ready for review August 16, 2021 11:12
@achingbrain achingbrain merged commit ce698bc into achingbrain:master Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants