Skip to content
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

Main package should be compiled for node env #655

Closed
quolpr opened this issue Jan 4, 2022 · 12 comments
Closed

Main package should be compiled for node env #655

quolpr opened this issue Jan 4, 2022 · 12 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@quolpr
Copy link

quolpr commented Jan 4, 2022

I had an issue with the axios library when I was using axios in the main package, the code was resolving to the XHR adapter. It was happening due to this field in package.json of axios:

  "browser": {
    "./lib/adapters/http.js": "./lib/adapters/xhr.js"
  },

It means that for browser env ./lib/adapters/http.js file should be replaced with ./lib/adapters/xhr.js, but main package is running under node environment, and it was causing the error. Here is the more detailed issue vitejs/vite#6382 .

So, I fixed it with ssr: true in the build section of config.vite.js. What do you think about this? I can provide PR if you want 🙂

@quolpr quolpr added the bug Something isn't working label Jan 4, 2022
@quolpr
Copy link
Author

quolpr commented Jan 4, 2022

Oops, the electron app will not work after compilation when ssr: true is provided; it fails to resolve dependencies(cause ssr: true doesn't bundle all deps to one file). So I am not sure how we could fix it 🤔 But we definitely need to let vite know that it is running in node env for the main package

@quolpr
Copy link
Author

quolpr commented Jan 4, 2022

Also related issue vitejs/vite#4405

@quolpr quolpr changed the title ssr: true for main Main package should be compiled for node env Jan 4, 2022
@cawa-93
Copy link
Owner

cawa-93 commented Jan 4, 2022

Hmmm...
Try add axios to external packages. That axios should resolve in runtime in electron environment

@quolpr
Copy link
Author

quolpr commented Jan 4, 2022

Wow, thanks for the fast response, I will check 🙂 Also related axios/axios#456

@quolpr
Copy link
Author

quolpr commented Jan 4, 2022

@cawa-93 this error happens when I running the compiled version

image

The yarn watch works like a charm

@quolpr
Copy link
Author

quolpr commented Jan 4, 2022

Btw I am getting the same error for ssr: true for compiled version, while yarn watch works correctly

@cawa-93
Copy link
Owner

cawa-93 commented Jan 4, 2022

🤔 I just tried run compilation with ssr: true and doesn't saw problems

@cawa-93
Copy link
Owner

cawa-93 commented Jan 4, 2022

It seems you can import axios from relative path:

import axios from '../../../node_modules/axios';

That force Vite does't resolve browser mapping and resolve only as node. I'm not sure how safe it is 🤷‍♂️

@quolpr
Copy link
Author

quolpr commented Jan 6, 2022

Oops, I used yarn workspaces, and that what the reason why some node packages were not loaded. Btw, I still think that the main package should use ssr: true, cause I suppose you will fail to use any native package(that uses gyp). As I understand later Vite will introduce this flag:

{
  resolve : {
    environment: 'node'
  }
}

@cawa-93
Copy link
Owner

cawa-93 commented Jan 6, 2022

Btw, I still think that the main package should use ssr: true,

You may be right. But I need more tests and research for that change

@cawa-93 cawa-93 added the help wanted Extra attention is needed label Jan 6, 2022
@cawa-93
Copy link
Owner

cawa-93 commented Jan 11, 2022

It seems build in ssr: true works fine for main and preload. But I can't dare use it because SSR support is still experimental.

@cawa-93
Copy link
Owner

cawa-93 commented Jun 9, 2022

Should be resolve in d002d6f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants