Skip to content

fix: Don’t break on circular imports#154

Merged
domenic merged 1 commit intojsdom:masterfrom
ExE-Boss:fix/avoid-breaking-on-circular-imports
Jan 3, 2020
Merged

fix: Don’t break on circular imports#154
domenic merged 1 commit intojsdom:masterfrom
ExE-Boss:fix/avoid-breaking-on-circular-imports

Conversation

@ExE-Boss
Copy link
Copy Markdown
Contributor

@ExE-Boss ExE-Boss commented Jan 2, 2020

Needed for jsdom/jsdom#2770 and converting Window to use WebIDL (jsdom/jsdom#2771).

Re‑assigning module.exports breaks circular require(…)s, as any module that was evaluated before module.exports was re-assigned will keep the reference to the old object, which is still empty.

By instead creating data properties on exports and treating it as a constant, then every reference will have all the properties after all modules have been evaluated.

This is also closer to how ECMAScript modules work.

@ExE-Boss ExE-Boss force-pushed the fix/avoid-breaking-on-circular-imports branch from b35a3d4 to d845f08 Compare January 2, 2020 01:16
@domenic
Copy link
Copy Markdown
Member

domenic commented Jan 2, 2020

As discussed in jsdom/jsdom#2770 (comment) I would prefer not to do this.

@ExE-Boss
Copy link
Copy Markdown
Contributor Author

ExE-Boss commented Jan 2, 2020

Well, this is also closer to how ECMAScript modules work.

@pmdartus
Copy link
Copy Markdown
Member

pmdartus commented Jan 2, 2020

Until we drop support for Node 10 (in May 2021) and start leveraging ES module, using exports. would greatly help circular dependencies.

I found myself introducing new circular dependency issues when working jsdom/jsdom#2548. There is one interesting case with StorageEvent (code). Adding support for custom elements, forced me to change the modules evaluation order, which had the unexpected effect of exhibiting the circular dependency between Storage and StorageEvent. The circular dependency between the interfaces can't be fixed, because the 2 interfaces depend on each other.

By instead creating data properties on exports and treating it as a constant, then every reference will have all the properties after all modules have been evaluated.

Using exports. instead of module.exports would allow me to move the require statement at the top of the file.

@domenic
Copy link
Copy Markdown
Member

domenic commented Jan 3, 2020

OK. While I still think that we should avoid circular dependencies whenever possible (e.g. we should merge attributes.js and Attr-impl.js), I can understand that the web platform we're implementing is not so cleanly layered, and so perhaps we should allow them in some cases.

@domenic domenic merged commit 8d89fc9 into jsdom:master Jan 3, 2020
@ExE-Boss ExE-Boss deleted the fix/avoid-breaking-on-circular-imports branch January 3, 2020 23:04
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.

3 participants