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

Generate .mjs file to allow importing from Node.js in ESM mode (#1960) #2200

Closed
wants to merge 1 commit into from

Conversation

cdauth
Copy link

@cdauth cdauth commented Apr 4, 2022

This pull request fixes #1960 while attempting to not introduce any breaking changes.

In addition to each existing *.esm.js bundle, an identical *.esm.mjs bundle is created. This makes sure that existing apps/libraries that for some reason import the existing bundle by its filename don't break.

An exports map is added to the main package.json of redux-toolkit. This map makes sure that Node.js will use the CommonJS bundle in CommonJS mode and the ESM bundle in ESM mode. To not break any projects that import redux-toolkit in an unconventional way, a wildcard export "./*": "./*" is added to allow importing any sub paths as before.

Items for /query and /query/react are also added to the exports map. So far these have been their own directories with their own package.json. However, according to my research, the current layout is not supported by Node.js in ESM mode, because modules are required to live in node_modules/<package> or in node_modules/@<org>/<package>. Importing a package from a sub directory such as node_modules/@reduxjs/toolkit/query will raise an ERR_UNSUPPORTED_DIR_IMPORT error in Node.js. Adding these items to the exports map fixes these imports.

Unless I made some mistake, this pull request should not introduce any breaking changes, so it can be published with the next patch or minor release.

For the next major release, I think the *.esm.js bundles can be removed. It would also be worth to consider relying exclusively on the export map and removing the query and query/react sub packages.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 4, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a61119a:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration

@netlify
Copy link

netlify bot commented Apr 4, 2022

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit a61119a
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/627d04e1e197cd000858eb65
😎 Deploy Preview https://deploy-preview-2200--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@nevace
Copy link

nevace commented May 24, 2022

Would it be possible to get this merged please?

@markerikson
Copy link
Collaborator

markerikson commented May 24, 2022

Frankly, given what happened with Immer, I'm very hesitant to make any changes to our build system for the foreseeable future :( And I'm busy enough with other things that this is not at all on my priority list.

At a minimum, I'd need to see someone put together some example projects that use the CodeSandbox CI build as a dependency, and show using RTK in several different environments (Node ESM, Node CJS, Next, Vite, CRA) and all working successfully, before I'd start to feel potentially comfortable merging this.

@rschristian
Copy link
Contributor

Frankly, given what happened with Immer, I'm very hesitant to make any changes to our build system for the foreseeable future

Immer's issue was solved in a day or so, with the correct change being slated for the next major; not really much of an issue, no? Ideally avoided for sure, but wasn't a disaster.

Totally understand unease, and wouldn't change this in anything but a semver major myself, but right now RTK is simply invalid w/ ESM in Node. This is a big part of the ecosystem and growing quickly, due to the actions of some maintainers who've gone ESM-only.

show using RTK in several different environments (Node ESM, Node CJS, Next, Vite, CRA) and all working successfully

Keep in mind, some of these tools break from the spec rather often.... Next in particular I've seen a lot of issues in over the years. The implementation there for resolving modules seems to be far from static so would say that one should expect some amount of harmless noise from these.

Probably also worth mentioning which versions of these tools you want to maintain support for?

@markerikson
Copy link
Collaborator

markerikson commented May 24, 2022

As maintainer, my number one concern is don't break existing code.

I am also sympathetic with "RTK doesn't work in X environment" and want to support that.

But given how fragile all this stuff is, and that I don't have time to spend on this myself, I need you or other people in the community to do the legwork here before I'm willing to merge this.

I need to see working proof of this branch compiling and running okay in as many different environment setups as possible, ideally exercising all the different combinations of build outputs and module formats, so that I can see this isn't going to suddenly break things.

It's also worth pointing out that while we've talked about an RTK 2.0 in #958 , and that a 2.0 would involve dropping IE11 support and updating our build formats and whatnot, we don't have concrete plans to put that out any time soon.

So, either this has to be fully compatible in a minor release, or it's gotta be shelved until we are ready for a 2.0.

@rschristian
Copy link
Contributor

That's totally fair, and I hope my comment didn't come off as demanding or suggesting this needs to be done! Wasn't my intention. I'm not even a Redux user fwiw (though I have enjoyed reading about it from you over the years), I don't have any need to see this land myself.

I thought I'd just hop in as I've done a fair bit regarding build tools (preact-cli, wmr, microbundle, etc) and have spent way too many hours dealing with the Node exports setup in different environments.

I'd definitely +1 shelving for a 2.0, even if this as-is theoretically shouldn't break any modern tools. And while I know you never get as many beta users as you'd like, this is definitely the sort of thing that'd be good to test out that way. Might just need some quick patches afterwards is what I wanted to warn about, depending on the tools and if you want to support their (often incorrect) configs.

@markerikson markerikson added this to the 2.0 milestone May 30, 2022
@markerikson
Copy link
Collaborator

Superseded by the work I did in #3095 .

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.

Cannot import redux-toolkit from a Node.js ESM module
5 participants