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

fix: Enable generation of ESM so that it can be used with native imports #123

Closed
wants to merge 4 commits into from

Conversation

ElMassimo
Copy link

@ElMassimo ElMassimo commented Feb 26, 2021

Description 📖

This pull request modifies the build command to also output ESM, so that this library can be used with native ESM imports.

Background 📜

Currently, the library is specifying a "dist/superjson.esm.js", that is not being generated, so native imports fail.

Notes ✏️

Using tsup instead of tsc, which has better defaults.

Unfortunately, tsup does not support older node versions, so we use tsc when doing CI with Node 10.

Moved .eslintrc.js to .eslintrc.json because some the older ESLint versions tsdx depends on would fail to require it now that the closest package.json is of "type": "module".

const Benchmark = require('benchmark');
const SuperJSON = require('./dist/').default;
import Benchmark from 'benchmark';
import SuperJSON from './dist/index.js';
Copy link
Collaborator

Choose a reason for hiding this comment

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

The reason to use require here was that it enabled running node benchmark.js, which is quite helpful in dev. Is this change neccessary?

Copy link
Author

@ElMassimo ElMassimo Mar 3, 2021

Choose a reason for hiding this comment

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

Running node benchmark.js fails in the latest node versions after adding "type": "module" if using require.

Copy link
Author

Choose a reason for hiding this comment

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

Added a script to ensure the benchmark can be run in Node 10 as well, by enabling modules with the experimental flag. You can now run yarn benchmark.

@@ -0,0 +1,23 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't spot a difference to the previous config. What's the idea behind this change?

Copy link
Author

@ElMassimo ElMassimo Mar 3, 2021

Choose a reason for hiding this comment

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

Mentioned it in the description:

Moved .eslintrc.js to .eslintrc.json because some the older ESLint versions tsdx depends on would fail to require it now that the closest package.json is of "type": "module".

"typings": "dist/index.d.ts",
"type": "module",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like the addition of ESM exports!

package.json Outdated
@@ -11,14 +13,15 @@
"node": ">=10"
},
"scripts": {
"build": "tsc",
"clean": "rm -rf ./dist",
"build": "tsup src/index.ts --dts --sourcemap --target es5 --format cjs,esm",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we make this work with TSC itself, too? We can't drop the Node 10 CI.

Copy link
Author

Choose a reason for hiding this comment

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

I'll take a look and see if I can achieve the same output using tsc alone.

Have in mind that it's only building in Node 10 that fails, using the library in Node 10 should work.

Copy link
Author

Choose a reason for hiding this comment

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

Added a fallback to tsc in Node 10 since tsup uses the TextEncoder global which was added in Node 12.

Copy link

Choose a reason for hiding this comment

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

Wonder why Node.js 10 support is still required, it's reaching the end of life next month

Copy link

Choose a reason for hiding this comment

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

(just removed TextDecoder from tsup since it's not necessary)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Node.js is pre-ES6 and thus a good proxy for testing legacy browser support.

@Skn0tt
Copy link
Collaborator

Skn0tt commented Mar 17, 2021

Hey @ElMassimo, this PR makes quite some big changes to our build process. If it's okay for you, I'd like to close it in favour of #127, which takes a leaner approach.

@ElMassimo
Copy link
Author

@Skn0tt Sounds good! I don't mind as long as I can import this library as a module 😃

@Skn0tt Skn0tt closed this Mar 17, 2021
@Skn0tt
Copy link
Collaborator

Skn0tt commented Mar 17, 2021

@allcontributors add ElMassimo for code

@allcontributors
Copy link
Contributor

@Skn0tt

I've put up a pull request to add @ElMassimo! 🎉

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.

3 participants