-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
Feat/exports-maps for consistent behaviour of esm imports in node and browser environments #1528
Feat/exports-maps for consistent behaviour of esm imports in node and browser environments #1528
Conversation
I am not as deeply involved into the modules mess in our ecosystem, but I think @TrySound is, what do you think? |
Would dropping cjs actually simplify the situation? |
Yes dropping cjs can be one of the solution, but that means the packages need to switch to having type:module field in it. And the main fields needs to point to esm. I will give it a thought today, on what could be implications of totally dropping it. But using exports to provide both makes it seemless for existing users. |
I would be interested in 2 things:
|
I think this guide provides some extensive information on what options are available: https://webpack.js.org/guides/package-exports/ adding “exports” field definitely contributes to the ecosystem of esm and allows tools like jspm and skypack to do their job well and create optimized builds for every entrypoint listed. overall trying to support all existing tools via “exports” is hard and tedious but is very beneficial to the future of ESM adoption |
That clarifies a lot of things, so @vovacodes how save is it to drop cjs support to packages at the moment. And going pure esm. I am trying to find the same for my stuff too, so curious to know more about this one. |
My answer for all the bottom three questions 2,3,4 will be yes. It makes things easier to maintain, yes it's a easy fix for users if they use But the only down side is, |
One more thing is missing here, we currently still use webpack for karma tests, which is now failing, try |
Sure will fix it, just wanted to know if the library is interested in making the adoption for esm before fixing all the tests failures :) |
Since it ticks most of the boxes, yes I think we do, but would love to hear what @TrySound thinks |
yeah, better fix failures to not miss some esm issues. |
Will be on it 👍 |
Hi @oleg008 can you helo me in in understanding this test-case alone I have a very limited knowledge on the internals, and trying to understand the use-case. But still lacking to narrow down. If you can help me with the use-case of |
26e013c
to
f7480af
Compare
You mean this particular line?
|
Thank you, i will try to solve the last test case too 🤞 😄 |
Heya, i spend the last weekend too on this. Looks like the test fails only when using the when i import |
Ok I figured out what the problem here is, it has nothing to do with your change. Tests were not cleaned up everywhere and your change has probably changed the order of execution of tests. |
Ahh, any suggestions to get them passing, i have very limited to no knowledge over mocha and stuff. Anything helps :) |
After trying to fix it, turns out both are a problem - not everything was cleaned up and your change made the dev mode load /tests/utils.js import a different jss, because it uses a relative path. Most likely before that relative and absolute path was resolving to the same jss build and now they resolve to different once |
@@ -7,4 +7,9 @@ if (globalThis[ns] == null) globalThis[ns] = 0 | |||
// the current version with just one short number and use it for classes generation | |||
// we use a counter. Also it is more accurate, because user can manually reevaluate | |||
// the module. | |||
export default globalThis[ns]++ | |||
|
|||
export const setVersion = (version: number) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just discovered this change, why was this necessary with regards to esm imports?
Closing this one, here is a new PR based on this #1552 |
I wonder if this can be released with minor version increment or needs a major |
Thanks to contributors for the awesome project 🚀 .
I have been using it in one of my projects, from a while. And recently i started moving it into pure esm package. And my package targets both
browser
andnode
environments.And so, i noticed how different
bundlers
andnode
resolve a package. Here is the reason,node
relies onexports
map or.mjs
extension of a package to consider it as aesm
module. But for bundlers it is not needed, since bundlers usemodule
field to do the same. And i see,jss
already usesmodule
field 😄And coming back to my use-case, so here is the
tricky
part. In Node, it starts to resolvemain
field and so with thecjs
andesm
interop in Node js. I need some notation like this to use the packages.Works good in node, since the only way to import from
cjs
in a module env in node. Is to import everything and then usejss.default()
But, when we bundle the code it breaks. Since, bundlers use
module
field and resolve theesm.js
bundle and so only the below works.So, we are left with two different ways to import when targeting both environments. Which is not so ideal when same package need to be bundled in both ways.
Solution - 1
To force bundlers to use
cjs.js
file so both browsers and node env uses the same resolution strategy. Which looks likeThis helps, but every user with the above use-case like mine need to go through this process.
Solution 2
Adding
exports
map which helps in resolving dependencies depending on the environment. Fully supported by node and bundlers and esm based CDN's like skypack, jspmSo, i went ahead and made those changes and so now resolving the package will be same in node and browsers.
Summary of changes made
esm
bundle fromrollup
should have.mjs
extension or the package itself should havetype: module
property. But since,jss
supportsumd
,cjs
too. It's good to have.mjs
as extension than switching the whole package tomodule
.module
field and addedexports
fields in all packages.@babel/[email protected]
. Because, below7.12.0
again it doesn't useexports
fields. And again bundlers ornode
breaks with esm errors. And from7.12.0
they adoptedexports
.is-in-browser
. Again the same, if node tried to resolvejss
it importscjs
version ofis-in-browser
and in latest version2.0
ofis-in-browser
we haveexports
adopted.webpack
andkarma-webpack
to 5 to support `exports resolution in webpack.Disadvantages
I personally didn't come across any, since even lower versions of
bundlers
andnode
can still work in the same way as they worked before.PS
Please feel free to close the PR if it's not in the scope, i can solve it using solution-1. But did the changes and opened a PR, so package can adopt and offer by default for future versions 😄
The
diff
is little big, but most of the changes comes fromyarn.lock
since i upgraded few packages. Please delete it and generate a new one from your requirements. At the moment 2 tests are breaking, and will look into them if the project can choose to adoptexports
.Thanks again for all the work 😄