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

build(esm): Use explicit .mjs extension for ESM, with CJS interop #7261

Merged
merged 2 commits into from
Dec 11, 2021

Conversation

evocateur
Copy link
Contributor

Building off of #7244, hopefully addressing CRA issues (#7251) and react-hook-form/resolvers#301.

I believe the root cause for the CRA issue is that RHF uses the namespace import form (import * as React from 'react') when it should be using a default import (import React from 'react'). React itself is not ESM, it's just a default export that Babel et al make-believe is a series of named exports, thus the error message folks are encountering in non-ESM tooling.

I was surprised RHF isn't using esModuleInterop: true in its tsconfig, and that has been addressed here. The test mock changes (one unused, one needing __esModule: true flags) were triggered by this tsconfig change.

Refs #7095
Refs #7088
Refs react-hook-form/resolvers#271
Refs vercel/next.js#30750

This ensures that Node always treats the ESM output as ESM, despite the package.json (implicit) type of commonjs. Bundlers that expect ESM, such as Next v12, no longer explode when encountering this module.

Refs react-hook-form#7244
Refs react-hook-form#7095
Refs react-hook-form#7088
Refs react-hook-form/resolvers#271
Refs vercel/next.js#30750
@bluebill1049
Copy link
Member

thank you so much @evocateur 👍

@bluebill1049
Copy link
Member

going to release a beta on this.

@bluebill1049 bluebill1049 merged commit ef770c5 into react-hook-form:beta Dec 11, 2021
bluebill1049 added a commit that referenced this pull request Dec 13, 2021
… (#7262)

* build(esm): Use explicit .mjs extension for ESM, with CJS interop (#7261)

* build(esm): Use explicit .mjs extension for ESM

This ensures that Node always treats the ESM output as ESM, despite the package.json (implicit) type of commonjs. Bundlers that expect ESM, such as Next v12, no longer explode when encountering this module.

Refs #7244
Refs #7095
Refs #7088
Refs react-hook-form/resolvers#271
Refs vercel/next.js#30750

* fix(esm-interop): Import non-ESM React as default

* 7.21.3-beta.0

* update with v2 compress

* fix lint error and update packages

Co-authored-by: Daniel Stockman <5605+evocateur@users.noreply.github.com>
@evocateur evocateur deleted the fix-esm-interop branch December 13, 2021 17:04
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

2 participants