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

SSR import in development can't resolve default import #5706

Closed
7 tasks done
cyco130 opened this issue Nov 16, 2021 · 2 comments · Fixed by #5923
Closed
7 tasks done

SSR import in development can't resolve default import #5706

cyco130 opened this issue Nov 16, 2021 · 2 comments · Fixed by #5923
Labels
feat: ssr p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Milestone

Comments

@cyco130
Copy link
Contributor

cyco130 commented Nov 16, 2021

Describe the bug

For some packages, when one does import whatever from "whatever", the default export ends up in whatever.default instead of whatever as expected. This only happens in 2.7.0 beta (2.6 is fine), in development mode of SSR. Everything works in the production build and in development in the browser.

Reproduction

This repo illustrates the issue with styled-components: https://github.com/cyco130/vite-issue-ssr-default-import

  • Clone the repo
  • Install dependencies with npm install
  • Start dev server with npm run dev
  • Open the link in the browser
  • Observe the message "BEEP! imported.default has what we expect in imported!" on the terminal
  • Also observe the lack thereof in the browser

The problem is caused by src/App.jsx where styled-component doesn't work in dev SSR mode when correctly imported but it does work when one accesses imported.default.

The issue appears to be caused by the proxyESM function in ssrModuleLoader.ts not following the xxx.default chain as many times as style-component's build system expects. The following change seems to solve the issue but might be too heavy-handed:

  let defaultExport = mod;
  while ("default" in defaultExport && defaultExport.__esModule) {
    defaultExport = defaultExport.default;
  }

System Info

System:
    OS: Linux 5.4 Linux Mint 20.2 (Uma)
    CPU: (8) x64 Intel(R) Core(TM) i7-3630QM CPU @ 2.40GHz
    Memory: 4.80 GB / 15.53 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 14.18.1 - /usr/local/bin/node
    Yarn: 1.22.11 - ~/.npm-global/bin/yarn
    npm: 7.24.1 - ~/.npm-global/bin/npm
  Browsers:
    Chrome: 96.0.4664.45
    Firefox: 94.0
  npmPackages:
    @vitejs/plugin-react: ^1.1.0-beta.0 => 1.1.0-beta.0 
    vite: ^2.7.0-beta.6 => 2.7.0-beta.6

Used Package Manager

npm

Logs

No response

Validations

@patak-dev patak-dev added p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) and removed pending triage labels Nov 19, 2021
@Niputi Niputi added this to the 2.7 milestone Nov 23, 2021
@brillout
Copy link
Contributor

@cyco130 How about this?

  let defaultExport = mod;
  const visited = new WeakMap()
  while ("default" in defaultExport && defaultExport.__esModule) {
    if (visited.has(defaultExport)) {
      break
    }
    visited.set(defaultExport, true);
    defaultExport = defaultExport.default;
  }

@cyco130
Copy link
Contributor Author

cyco130 commented Nov 26, 2021

@brillout Much better. I did run into a pathological module that broke mine today.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants