Skip to content

circular dependency (invalid esm) in v5 at /dist/es/Seq.js and dist/es/Collection.js #2019

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

Closed
iambumblehead opened this issue Sep 18, 2024 · 5 comments

Comments

@iambumblehead
Copy link

iambumblehead commented Sep 18, 2024

Collection.js imports Seq.js and Seq.js imports Collection.js

Because circular dependencies aren't supported by esm, the files aren't usable as-is and must be transpiled.

Also, these lines in Seq.js look strange.

if ( Collection ) Seq.__proto__ = Collection;
Seq.prototype = Object.create( Collection && Collection.prototype );
Seq.prototype.constructor = Seq;

When Collection is imported through a circular dependency, its value is falsey undefined which causes a runtime error when it is passed to Object.create

related #1961

@iambumblehead
Copy link
Author

building immutable with this patch at the 5.x branch gives me an es module that is working here

diff --git a/resources/rollup-config.mjs b/resources/rollup-config.mjs
index bf32fc38..99c070b8 100644
--- a/resources/rollup-config.mjs
+++ b/resources/rollup-config.mjs
@@ -36,10 +36,8 @@ export default [
       {
         banner: copyright,
         name: 'Immutable',
-        dir: path.join(DIST_DIR, 'es'),
+        file: path.join(DIST_DIR, 'immutable.es.js'),
         format: 'es',
-        preserveModules: true,
-        preserveModulesRoot: SRC_DIR,
         sourcemap: false,
       },
     ],

@kyle-leonhard
Copy link

I ran into this as well when upgrading from 4.3.7 to 5.0.0.

TypeError: Object prototype may only be an Object or null: undefined
 ❯ node_modules/.pnpm/immutable@5.0.0/node_modules/immutable/dist/es/Seq.js:61:26
     59|
     60|   Seq.prototype.cacheResult = function cacheResult () {
     61|     if (!this._cache && this.__iterateUncached) {
       |                          ^
     62|       this._cache = this.entrySeq().toArray();
     63|       this.size = this._cache.length;
 ❯ node_modules/.pnpm/immutable@5.0.0/node_modules/immutable/dist/es/Seq.js:117:2
 ❯ node_modules/.pnpm/immutable@5.0.0/node_modules/immutable/dist/es/Collection.js:1:31

@jdeniau
Copy link
Member

jdeniau commented Nov 5, 2024

@kyle-leonhard do,you have a reproducible exemple? Can you provide your nodejs version and / or your bundler?

@kyle-leonhard
Copy link

kyle-leonhard commented Nov 5, 2024

@jdeniau, thanks for the reply! I don't have a minimal repro, but here's a PR demonstrating the issue: PR. The workflow running the tests is using nodejs version 22.11.0 config, but I'm able to reproduce locally with version 23.1.0 as well.

Let me know if I can provide any additional context!

@jdeniau
Copy link
Member

jdeniau commented Nov 8, 2024

@kyle-leonhard this should be resolved in 5.0.2 thanks to @iambumblehead

@jdeniau jdeniau closed this as completed Nov 8, 2024
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 a pull request may close this issue.

3 participants