Skip to content

[fix] added "browser" conditional to "exports" in package.json #6881

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

Merged
merged 1 commit into from
Nov 4, 2021

Conversation

bf-software
Copy link
Contributor

fixes: #6877

This will allow electron renderers to avoid loading ssr.mjs when importing svelte and instead load index.mjs like a normal browser would. Electron renderers are like normal browsers, but they also can access node functions, which is why both the "node", and "browser" conditionals are present when resolving module names using package.json's "exports" feature.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with [feat], [fix], [chore], or [docs].
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

From the linked issue, I think this is a fair change to support webpack + electron, without having an "electron" condition. (or maybe we want that?) (not needed)

@benmccann
Copy link
Member

lgtm. Though I do somewhat wonder if we shouldn't change the condition from browser to electron to clarify why the condition is there because otherwise it doesn't make much sense as there aren't many platforms that would match both browser and node

@dummdidumm
Copy link
Member

I'm not sure if this would work - do the bundler in question and the elctron loader search for a electron condition in exports?

@bluwy
Copy link
Member

bluwy commented Nov 4, 2021

For webpack, it will use the electron condition for electron builds, according to https://webpack.js.org/guides/package-exports/#target-environment

@bf-software
Copy link
Contributor Author

Please also note that electron uses at minimum 2 different processes for a single electron app: the main process and 1 or more renderer processes. In Windows for example, you start your app by running electron.exe yourscript.js. This is the main process and is basically just like running node.exe your-script.js -- meaning it has no DOM access, it only has node access. Then your-script.js can launch additional electron.exe your-window.html processes. These are the renderer processes that present your app's GUI to the user--and these have both node and DOM (browser) access. So when targeting the renderer, we definitely want Svelte to act like its in a browser (because it is). But, it's probably possible for someone to use Svelte in the main process with server side rendering, like maybe if they were converting an SSR web app to a desktop app? So in that case, when targeting the main process, we would want Svelte to act like it's being used in a node server. In both cases, electron is present in the conditionals.

@bluwy
Copy link
Member

bluwy commented Nov 4, 2021

Thanks for the explanation @bf-software. In that case we should stick with browser then.

@benmccann benmccann merged commit 4d4f959 into sveltejs:master Nov 4, 2021
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.

Webpack for Electron Renderer is switching to ssr.mjs because of package.json-->exports.node
4 participants