Skip to content

feat: add support for 'electron' condition in conditional exports #59002

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Momena-akhtar
Copy link

Description

This PR adds support for resolving "electron" condition in both ESM and CommonJS module resolution, based on process.versions.electron. Useful for runtime-specific exports for Electron apps.

Resolves electron/electron#47670

Add conditions.add('electron') after getConditionsSet() result.
Adds 'electron' to CommonJS conditions when in Electron.
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Jul 9, 2025
@ljharb
Copy link
Member

ljharb commented Jul 9, 2025

How does adding this string to conditions alter resolution for non-electron users?

@ShogunPanda
Copy link
Contributor

Hi!
I understand the need for this but I don't like the ad-hoc approach.
Could you rewrite in a way that is generic? Such as an API that registers custom functions to trigger conditions.

Something like (totally made up API, just giving an hint):

$ node --import conditions-register.js index.js

And in conditions-register.js:

import { registerCondition } from 'node:module'

registerCondition((nmPath, request, conditions) {
  if (process.versions?.electron && conditions?.add) {
    conditions.add('electron');
  }
})

@Momena-akhtar
Copy link
Author

Hi! I understand the need for this but I don't like the ad-hoc approach. Could you rewrite in a way that is generic? Such as an API that registers custom functions to trigger conditions.

Something like (totally made up API, just giving an hint):

$ node --import conditions-register.js index.js

And in conditions-register.js:

import { registerCondition } from 'node:module'

registerCondition((nmPath, request, conditions) {
  if (process.versions?.electron && conditions?.add) {
    conditions.add('electron');
  }
})

Will definitely give it a try and open the PR!

@@ -638,6 +638,12 @@ const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/;
*/
function resolveExports(nmPath, request, conditions) {
// The implementation's behavior is meant to mirror resolution in ESM.

//Add electron
if (process.versions?.electron && conditions?.add) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally really don't like to have such downstream (electron) code into upstream (node)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support electron conditional exports
5 participants