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

Replace "require" is not always needed #10795

Closed
7 tasks done
zorn-v opened this issue Nov 5, 2022 · 4 comments
Closed
7 tasks done

Replace "require" is not always needed #10795

zorn-v opened this issue Nov 5, 2022 · 4 comments
Labels
wontfix This will not be worked on

Comments

@zorn-v
Copy link

zorn-v commented Nov 5, 2022

Describe the bug

Some packages (as uniqid) make checks like if(typeof require !== 'undefined') to ensure that we are in node env.
But vite replace that require to _require which is defined and then "module as been externalized"...

I think if it is not function call, there is no need to transform that check.

Reproduction

https://stackblitz.com/edit/vitejs-vite-dpwfwp?file=main.js
adamhalasz/uniqid#48

Steps to reproduce

npm create vite vite-project -- --template vanilla
cd vite-project
npm i
npm i uniqid
echo "import uniqid from 'uniqid'" >> main.js
npm run dev

Then open http://localhost:5173/ in browser and check browser console

System Info

System:
    OS: Linux 5.19 Ubuntu 22.10 22.10 (Kinetic Kudu)
    CPU: (4) x64 Intel(R) Core(TM) i3-7100 CPU @ 3.90GHz
    Memory: 10.53 GB / 15.57 GB
    Container: Yes
    Shell: 5.2.2 - /bin/bash
  Binaries:
    Node: 16.18.0 - /usr/local/bin/node
    npm: 8.19.2 - /usr/local/bin/npm
  Browsers:
    Chromium: 107.0.5304.87
    Firefox: 106.0.5
  npmPackages:
    @vitejs/plugin-vue: ^3.2.0 => 3.2.0 
    vite: ^3.2.2 => 3.2.2

Used Package Manager

npm

Logs

No response

Validations

@github-actions
Copy link

github-actions bot commented Nov 6, 2022

Hello @zorn-v. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@zorn-v
Copy link
Author

zorn-v commented Nov 6, 2022

Added steps to reproduce

@suguanYang
Copy link
Contributor

@sapphi-red
Copy link
Member

Yeah, this is how esbuild treats require.

It's better to use conditional exports instead of runtime check.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2022
@sapphi-red sapphi-red added the wontfix This will not be worked on label Nov 7, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Nov 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants