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

Invalid commonjs default import in prod builds #13009

Closed
7 tasks done
marvinhagemeister opened this issue Apr 26, 2023 · 2 comments
Closed
7 tasks done

Invalid commonjs default import in prod builds #13009

marvinhagemeister opened this issue Apr 26, 2023 · 2 comments

Comments

@marvinhagemeister
Copy link
Contributor

Describe the bug

We received a bug report (see: preactjs/preset-vite#75) for the Preact vite preset that there is an issue with importing react-datetime. Upon further inspection this issue isn't related to Preact and can be reproduced with the React preset too. I was able to narrow it down to a difference in how default exports from commonjs modules are treated.

import DateTime from 'react-datetime';

if (typeof DateTime !== 'function') {
  // We hit this path only in prod builds
  throw new Error('This is a bug');
}
console.log('it works');

In development mode everything works as expected. But in production mode the default export is wrapped into an object: { default: ... } with a default property which contains the expected value. This additional indirection causes production builds to be incorrect.

Reproduction

https://stackblitz.com/edit/vitejs-vite-k6uuun?file=main.js

Steps to reproduce

  1. Run npm install
  2. Run npm run build
  3. Run npm run preview
  4. Check console

System Info

Not relevant, happens on all systems.

  System:
    OS: macOS 13.3.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 90.64 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 19.7.0 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.5.0 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 112.0.5615.137
    Chrome Canary: 114.0.5734.0
    Firefox: 111.0.1
    Safari: 16.4
  npmPackages:
    @vitejs/plugin-react: ^4.0.0 => 4.0.0 
    vite: ^4.3.2 => 4.3.2

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Apr 26, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

This didn't work with 4.3.2 but it worked with the current main branch (5731ac9caaef629e892e20394f0cc73c565d9a87). I guess rollup/plugins#1455 fixed this.
This will work in the next version. 👍

@github-actions github-actions bot locked and limited conversation to collaborators May 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants