Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

feat: improve compatibility with native ESM in Node and the browser #21373

Merged

Conversation

vovacodes
Copy link
Contributor

@vovacodes vovacodes commented Nov 19, 2021

Problem

This package is failing in the browser ESM + Import Map environment because of an incorrect use of the js-sha3 package, which is a CJS package and doesn't provide any named exports.

Summary of Changes

This PR also adds a test that ensures ESM compatibility in the future.

@mergify mergify bot added the community Community contribution label Nov 19, 2021
@mergify mergify bot requested a review from a team November 19, 2021 23:17
@vovacodes
Copy link
Contributor Author

@guybedford could you please take a look and make sure the changes I propose are indeed correct from the Node's and browser ESM behaviour's standpoint

@vovacodes vovacodes force-pushed the feature/improve-esm-compatibility branch 2 times, most recently from ecb129d to 372c0df Compare November 19, 2021 23:33
Copy link

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

This is the correct interop pattern per the Node.js CJS / ESM interop semantics - https://nodejs.org/dist/latest-v17.x/docs/api/esm.html#commonjs-namespaces.

@@ -55,6 +55,7 @@
"re": "semantic-release --repository-url git@github.com:solana-labs/solana-web3.js.git",
"test": "mocha -r ts-node/register './test/**/*.test.ts'",
"test:cover": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' nyc --reporter=lcov mocha -r ts-node/register './test/**/*.test.ts'",
"test:esm-compat": "node test/esm-compat.mjs",

Choose a reason for hiding this comment

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

Perhaps add this invocation to the CI definitions as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, added

Copy link

Choose a reason for hiding this comment

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

that's so good !

// This tests if the package is configured correctly and can be imported in Node versions that support ESM.
import * as web3 from '@solana/web3.js';

console.log(web3);
Copy link
Contributor

Choose a reason for hiding this comment

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

While this is a good test when looking at it, I don't think it will fail the CI if it fails. Try using mocha framework describe it like the other tests, so that it fails when we test through mocha.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should fail CI because Node will just throw an exception if it cannot import the module, I can make it a mocha test but it should fail in exactly the same way, no describe or it blocks will ever be reached if that happens

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using a dynamic import might work, ok let me try that...

Copy link
Contributor

@jacobcreech jacobcreech Nov 23, 2021

Choose a reason for hiding this comment

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

I see you added a different step in the CI. I think that should cover it for failure. Just using mocha would keep it standardized across other tests vs just testing if you can console.log web3. Looks like something needs to be updated unrelated to the PR to fix the build issue. Will wait for another reviewer to get their eyes on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I'll update

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to remove the test as it turns out there's no way to test ESM compatibility without either changing the build files to .mjs or switching "type" to "module" in package.json. I'm happy to do it if there's willingness from the authors to accept a change that potentially might break compatibility with some legacy bundlers

@vovacodes vovacodes requested review from jacobcreech and removed request for a team November 23, 2021 15:24
@guybedford
Copy link

To be honest, the fact that this PR doesn't alter the existing tests from passing should be enough since interop is already tested in the current browser bundling, it's just a slight alteration to the pattern.

@jacobcreech jacobcreech requested a review from a team November 23, 2021 22:54
@jstarry jstarry added the automerge Merge this Pull Request automatically once CI passes label Nov 24, 2021
@jstarry jstarry changed the title Improve compatibility with native ESM in Node and the browser fix: improve compatibility with native ESM in Node and the browser Nov 24, 2021
@mergify mergify bot removed the automerge Merge this Pull Request automatically once CI passes label Nov 24, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 24, 2021

automerge label removed due to a CI failure

@vovacodes vovacodes force-pushed the feature/improve-esm-compatibility branch from 6cb3bec to 7ce38a4 Compare November 24, 2021 13:27
@jstarry jstarry changed the title fix: improve compatibility with native ESM in Node and the browser feat: improve compatibility with native ESM in Node and the browser Nov 24, 2021
@jstarry
Copy link
Contributor

jstarry commented Nov 24, 2021

Thanks for fixing the commit message, I just added a way to override this for next time: #21414

@jstarry jstarry added the automerge Merge this Pull Request automatically once CI passes label Nov 24, 2021
@codecov
Copy link

codecov bot commented Nov 24, 2021

Codecov Report

Merging #21373 (2c20266) into master (c3e5927) will decrease coverage by 11.3%.
The diff coverage is n/a.

❗ Current head 2c20266 differs from pull request most recent head 7ce38a4. Consider uploading reports for the commit 7ce38a4 to get more accurate results

@@             Coverage Diff             @@
##           master   #21373       +/-   ##
===========================================
- Coverage    81.5%    70.2%    -11.4%     
===========================================
  Files         500       35      -465     
  Lines      140604     2066   -138538     
  Branches        0      295      +295     
===========================================
- Hits       114704     1451   -113253     
+ Misses      25900      515    -25385     
- Partials        0      100      +100     

@hwl66
Copy link

hwl66 commented Nov 24, 2021 via email

@jstarry jstarry merged commit 1aebe65 into solana-labs:master Nov 24, 2021
@vovacodes vovacodes deleted the feature/improve-esm-compatibility branch November 24, 2021 15:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
automerge Merge this Pull Request automatically once CI passes community Community contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants