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

optimizeDeps.exclude removes default property from exports #10258

Closed
7 tasks done
sagargurtu opened this issue Sep 27, 2022 · 0 comments · Fixed by #10406
Closed
7 tasks done

optimizeDeps.exclude removes default property from exports #10258

sagargurtu opened this issue Sep 27, 2022 · 0 comments · Fixed by #10406
Labels
feat: deps optimizer Esbuild Dependencies Optimization p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@sagargurtu
Copy link
Contributor

sagargurtu commented Sep 27, 2022

Describe the bug

When a dependency is added in optimizeDeps.exclude, it injects __reExport during dependency pre-bundling (link) which removes default property.

Steps to reproduce:

  • Copy the project from https://stackblitz.com/edit/vitejs-vite-u5bre3 to a local dev environment since StackBlitz doesn't show node_modules/ directory.
  • The vite configuration is doing two things:
    • Excluding react from optimizeDeps.
    • Declaring a plugin that marks react imports as external and replaces them with external ESM URLs.
  • Start dev server and open the URL. It throws error in browser console.
  • Search node_modules/.vite/deps for a chunk file that contains // external:react. It has the following code snippet:
    // external:react
    var react_exports = {};
    import * as react_star from "react";
    var init_react = __esm({
      "external:react"() {
        __reExport(react_exports, react_star);
      }
    });
    
  • __reExport removes default property from react import:
    var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
    
  • Comment out lines 7-12 (both inclusive) in src/index.tsx. Refresh the webpage, it loads the UI without error.
  • Alternatively, comment out optimizeDeps and plugin implementation from vite config. Refresh the webpage, it loads the UI without error.
  • Issue: theming.esm.js is trying to access React.Component but it is undefined because react_exports does not contain "default" property.

Reproduction

https://stackblitz.com/edit/vitejs-vite-u5bre3

System Info

System:
  OS: Linux 5.0 undefined
  CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Memory: 0 Bytes / 0 Bytes
  Shell: 1.0 - /bin/jsh
Binaries:
  Node: 16.14.2 - /usr/local/bin/node
  Yarn: 1.22.10 - /usr/local/bin/yarn
  npm: 7.17.0 - /usr/local/bin/npm
npmPackages:
  @vitejs/plugin-react: ^2.1.0 => 2.1.0 
  vite: ^3.1.3 => 3.1.3

Used Package Manager

npm

Logs

Console Error:

Uncaught TypeError: Cannot read properties of undefined (reading 'Component')
    at createThemeProvider (theming.js?v=288fcf31:403:27)
    at createTheming (theming.js?v=288fcf31:443:20)
    at theming.js?v=288fcf31:447:22

Validations

@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label Oct 10, 2022
@sapphi-red sapphi-red added pending triage feat: deps optimizer Esbuild Dependencies Optimization labels Oct 10, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Dec 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: deps optimizer Esbuild Dependencies Optimization p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants