rxjs icon indicating copy to clipboard operation
rxjs copied to clipboard

ESM version is never used

Open akanass opened this issue 3 years ago • 8 comments

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.

akanass avatar May 02 '21 23:05 akanass

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 avatar Aug 09 '21 15:08 benlesh

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 avatar Aug 11 '21 20:08 benlesh

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

shrinktofit avatar Sep 03 '21 03:09 shrinktofit

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.

kwonoj avatar Sep 03 '21 03:09 kwonoj

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

Great to hear, looking forwad to RxJS 8! 🥳

Ronsku avatar Sep 06 '21 22:09 Ronsku

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'

grosch avatar Jul 04 '22 10:07 grosch

Also looking very much forward to this

Ronsku avatar Jul 12 '22 18:07 Ronsku

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

Ronsku avatar Sep 10 '22 14:09 Ronsku

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.

devversion avatar Dec 09 '22 14:12 devversion

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

benlesh avatar Feb 08 '23 21:02 benlesh

@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.

demensky avatar Feb 08 '23 23:02 demensky