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
Unable to load formatjs in jest with ESM (Unexpected token 'export') #4128
Comments
I tried to see if I can create a PR for this, but not really familiar with bazel. |
This happens only to the latest version, as it adds the exports field without the type field. e.g. |
Having the same issue indeed. I do not really know how to create a PR for this either |
btw targeting Actually, you probably don't even need that in the first place. |
Having this issue too, I think.
|
…t-parser,@formatjs/intl-displaynames,@formatjs/intl-listformat,intl-messageformat,@formatjs/ecma402-abstract,@formatjs/intl-numberformat,@formatjs/icu-skeleton-parser): Revert esm conditional exports (#4129) The ESM export chanegs are not properly tested and therefore broke in various scenarios. Fixes #4128, #4127, #4126 This reverts commit e0d593c.
Which package?
Almost all
@formatjs
packages.Describe the bug
When writing a library written in TypeSript, using
jest
andts-jest
,we need to use
extensionsToTreatAsEsm
in indicates what files should be treated as ESM,in order to use code such as
import.meta
Here is an example:
The problem with that is then
jest
will rely on thetype
field of the nearestpackage.json
to determine if the file should be processed as ESM or not.The
@formatjs
packages does not specify such field. Using@formatjs/intl
as an example:This means
./lib/index.js
(just like./index.js
) is treated as CJS thus getting `Unexpected token 'export' error:To Reproduce
Codesandbox URL
Reproducible Steps/Repo
Here is a repro:
https://github.com/repobuddy/repobuddy/tree/formatjs-issue/testcases/jsdom-formatjs
Checkout the
jsdom-formatjs
branch, run:You can see the error.
Then, you can comment out
extensionsToTreatAsEsm: ['.ts']
in thejest.config.js
and runpnpm test
again, which you can see theimport.meta
error:Expected behavior
Able to run the code in such environment.
Screenshots
Desktop (please complete the following information):
Additional context
The fix is to add
type: module
to a package.json underlib
folder:- package.json // keep as-is - index.js - lib - package.json // add this, containing `{ "type": "module" }` - index.js
Recommended solution
Optimally, I would recommend completely separating cjs and esm output
What I normally do is organize the output like this:
and the package.json contains:
The text was updated successfully, but these errors were encountered: