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

feat: Support .cts extension with ts-node 10+ #3017

Merged
merged 12 commits into from Sep 13, 2022

Conversation

phated
Copy link

@phated phated commented Aug 5, 2022

  • I talked about the limitations in the ESM PR and suggestion in the ts-node transpile PR

Due to the way hardhat registers tasks, v3 can't support ESM (as I outlined at #1994 (comment)); however, TypeScript added ESM support and many projects are converting to ESM-only. The ts-node team added support for the .cts extension in TypeStrong/ts-node#1694, which allows individual files to be resolved as commonjs (see https://github.com/TypeStrong/ts-node#module-type-overrides).

This change should allow a monorepo to become "type": "module" and still use hardhat in commonjs mode. 🎉

@changeset-bot
Copy link

changeset-bot bot commented Aug 5, 2022

⚠️ No Changeset found

Latest commit: 2489ac5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Aug 5, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
hardhat ✅ Ready (Inspect) Visit Preview Sep 13, 2022 at 0:57AM (UTC)
hardhat-storybook ✅ Ready (Inspect) Visit Preview Sep 13, 2022 at 0:57AM (UTC)

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2022

This issue is also being tracked on Linear.

We use Linear to manage our development process, but we keep the conversations on Github.

LINEAR-ID: aa761df6-e663-4f74-a192-ba44dc37a0f4

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2022

Thanks for submitting this PR!

Unfortunately, it has some linter errors, so we can't merge it yet. Can you please fix them?

Running yarn lint:fix in the root of the repository may fix them automatically.

@fvictorio
Copy link
Member

@alcuadrado this looks good to me. Do you want to take a look?

@fvictorio
Copy link
Member

Hmm, this might need some changes to the Hardhat config loading logic.

@robwilkes
Copy link

robwilkes commented Aug 11, 2022

edit: on further consideration I don't think this solves anything. we can rename all the files to .cts, which allows the project itself to be ESM compatible and we could load ESM modules into .ts files, but we wouldn't be able to load ESM modules into .cts files, which would basically be all the hardhat project files. whilst great it we can use hardhat inside a ESM project, it doesn't really allow the use of ESM modules, unless those are standalone scripts/modules not imported into any hardhat files.

this whole ESM thing is a mess.


I tried this and it doesn't seem to be detecting my .cts config file. and if I manually pass --config hardhat.config.cts I get mocha errors when running hardhat test. many modules are starting to move to ESM only, surely the best to way enable support for those is to have the hardhat project marked as ESM but hardhat support/load .cts files. surely this wouldn't take much to get over the line, it feels part way there already.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/rob/src/test/node/hardhat-esm/test/Lock.ts
require() of ES modules is not supported.
require() of /Users/rob/src/test/node/hardhat-esm/test/Lock.ts from /Users/rob/src/test/node/hardhat-esm/node_modules/mocha/lib/mocha.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/rob/src/test/node/hardhat-esm/package.json.

    at createErrRequireEsm (/Users/rob/src/test/node/hardhat-esm/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/Users/rob/src/test/node/hardhat-esm/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/Users/rob/src/test/node/hardhat-esm/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at /Users/rob/src/test/node/hardhat-esm/node_modules/mocha/lib/mocha.js:414:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/rob/src/test/node/hardhat-esm/node_modules/mocha/lib/mocha.js:411:14) {
  code: 'ERR_REQUIRE_ESM'
}
Waiting for the debugger to disconnect...
error Command failed with exit code 1.

I tried editing this in package.json and renaming to Lock.cts but it doesn't seem to detect it and reports 0 passing.
"test": "mocha --recursive \"test/**/*.cts\"",

oh snap it worked.

hardhat-esm yarn hardhat --config hardhat.config.cts test
Debugger attached.
Debugger attached.
yarn run v1.22.15
$ /Users/rob/src/test/node/hardhat-esm/node_modules/.bin/hardhat --config hardhat.config.cts test
Debugger attached.


  Lock
    Deployment
      ✔ Should set the right unlockTime (687ms)
      ✔ Should set the right owner
      ✔ Should receive and store the funds to lock
      ✔ Should fail if the unlockTime is not in the future
    Withdrawals
      Validations
        ✔ Should revert with the right error if called too soon
        ✔ Should revert with the right error if called from another account
        ✔ Shouldn't fail if the unlockTime has arrived and the owner calls it
      Events
        ✔ Should emit an event on withdrawals
      Transfers
        ✔ Should transfer the funds to the owner


  9 passing (897ms)

it's hardcoded here
image

I'd love to contribute but I've only been working with node for about 2 weeks

@phated
Copy link
Author

phated commented Aug 11, 2022

@robwilkes this is the very first, minor step into getting some semblance of interopt with type: module projects. Please hang tight until we can get things working together. As for this feature specifically, it requires ts-node 10+ and typescript 4.7+ otherwise it won't detect the extension correctly.

@gitpoap-bot
Copy link

gitpoap-bot bot commented Sep 13, 2022

Woohoo, your important contribution to this open-source project has earned you a GitPOAP!

GitPOAP: 2022 Hardhat Contributor:
GitPOAP: 2022 Hardhat Contributor GitPOAP Badge

Head on over to GitPOAP.io and connect your GitHub account to mint!

@fvictorio
Copy link
Member

Merging this to the esm feature branch, because I think these PRs will be easier to review and test together.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants