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

ESM version is never used #6321

Closed
akanass opened this issue May 2, 2021 · 11 comments
Closed

ESM version is never used #6321

akanass opened this issue May 2, 2021 · 11 comments
Labels
8.x Issues and PRs for version 8.x AGENDA ITEM Flagged for discussion at core team meetings

Comments

@akanass
Copy link

akanass commented May 2, 2021

Bug Report

Current Behavior
ES5 version is always used in client project when we build it with rollup or webpack even if we set mainFields to es2015 to take package.json attribute like in previous version.

CJS version is always used in node project even if we want to use module in latest node runtime.

This is due to the exports attribute in package.json which only declares node and default values.

Expected behavior
ESM version should be used when client project is built and we are using rxjs with import in javascript or typescript code and we want to build a script compatible with HTML <script type="module"> in HTML

ESM version should be used in node project if in package.json we set type:module attribute and we used it in latest runtime.

Reproduction
In a client project,

import { ajax, AjaxConfig, AjaxResponse } from 'rxjs/ajax';
import { Observable } from 'rxjs';

After that, you have to build the client project and you will see that only ES5 version is taken and not ESM version even if we set mainFields to es2015 in webpack or rollup config.

In a Node project,

import { Observable } from 'rxjs';

Build your project and in package.json set type:module and you will see that only CJS version is taken.

Environment

  • RxJS version: 7.0.0

Possible Solution

You can see all possibilities here and we have to make a proposition to change package.json:exports attribute

You can see examples in the link provided before and they are explaining how to manage node and module version.

If we don't change the exports attribute, we should allow build system to take the good version we want using main, module or es2015 attributes from package.json like it was possible in previous version especially for client project.

@cartant cartant added the 7.x Issues and PRs for version 7.x label May 5, 2021
@benlesh
Copy link
Member

benlesh commented Aug 9, 2021

I'm looking through the solutions here and none of them seem ideal: https://webpack.js.org/guides/package-exports/#target-environment-independent-packages

One thing I know comes up is situations where people are doing instanceof checks. The "stateless" solution for Node seems like it would cause issues. One gotcha is RxJS Observables are often used as a return type from third party packages. Some may use ESM, and other CJS, if that's the future there.

In other news: God, I hate module systems.

@benlesh benlesh added the AGENDA ITEM Flagged for discussion at core team meetings label Aug 9, 2021
@benlesh
Copy link
Member

benlesh commented Aug 11, 2021

Core Team Meeting: We probably can't act on this during version 7, because it would incur a breaking change for some folks. But we definitely want to move to modern ESM in version 8, probably ES2018+.

@benlesh benlesh added 8.x Issues and PRs for version 8.x and removed 7.x Issues and PRs for version 7.x labels Aug 11, 2021
@shrinktofit
Copy link

shrinktofit commented Sep 3, 2021

Could you add an simple package.json into dist/esm5 with content: { "type": "module" } so Node native ESM system can treat modules under this directory ESM modules?

Another import thing to note: ESM modules under dist/esm5/ use extension-less module specifiers which would not be resolved on neither browser native ESM or Node native ESM.

image

@kwonoj
Copy link
Member

kwonoj commented Sep 3, 2021

None of published module in rx is native esm so we can't / won't specify separate pkg manifest to esm. Supporting native esm is another topic core team discusses heavily to ensure avoiding potential regressions with ecosystem support, but this specific topic is not intend to make changes to current esm into native one.

@Ronsku
Copy link

Ronsku commented Sep 6, 2021

But we definitely want to move to modern ESM in version 8

Great to hear, looking forwad to RxJS 8! 🥳

@grosch
Copy link

grosch commented Jul 4, 2022

Any updates on this? I tried using he 8.0.0-alpha.3 build but I'm still getting this failure when using ESM and trying to import:

import {TestScheduler} from 'rxjs/testing'

SyntaxError: The requested module 'rxjs/testing' does not provide an export named 'TestScheduler'

@Ronsku
Copy link

Ronsku commented Jul 12, 2022

Also looking very much forward to this

@Ronsku
Copy link

Ronsku commented Sep 10, 2022

2 months has passed again, does anyone have any updates, suggestions, workarounds or help?

@devversion
Copy link

It seems like, even if you would resolve to the ESM artifacts, the .js files are not considered as ESM because there is no corresponding package.json with type: module. So, either there needs to be a such a file in dist/esmX or the files should use the .mjs extension.

@benlesh
Copy link
Member

benlesh commented Feb 8, 2023

v8 is going to consolidate on ES2022 and get rid of ES5. That should resolve this issue.

@benlesh benlesh closed this as completed Feb 8, 2023
@demensky
Copy link
Contributor

demensky commented Feb 8, 2023

@benlesh, can you provide a link to the ES2022 issue?

I only found mention of ES2021:

Ensure we're publishing only ES2021 or later and CJS. Drop tslib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.x Issues and PRs for version 8.x AGENDA ITEM Flagged for discussion at core team meetings
Projects
None yet
Development

No branches or pull requests

9 participants