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

vite:define can't process std-env #15367

Closed
7 tasks done
danielroe opened this issue Dec 16, 2023 · 5 comments
Closed
7 tasks done

vite:define can't process std-env #15367

danielroe opened this issue Dec 16, 2023 · 5 comments
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@danielroe
Copy link
Contributor

danielroe commented Dec 16, 2023

Describe the bug

std-env contains some environment-agnostic utilities (see source).

The current vite:define plugin can't process this (see compiled library), and there seems to be no way to opt-out of processing a file with the define plugin.

Linked upstream issue: nuxt/nuxt#24793

Reproduction

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

Steps to reproduce

npm run dev (which will run the build command)

System Info

StackBlitz

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Dec 16, 2023

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

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Dec 17, 2023

It looks like esbuild's bug. I was testing https://unpkg.com/std-env@3.6.0/dist/index.mjs on esbuild playground with define picked from:

Object.assign(processEnv, {
'process.env': `{}`,
'global.process.env': `{}`,
'globalThis.process.env': `{}`,
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
'global.process.env.NODE_ENV': JSON.stringify(nodeEnv),
'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv),
})

And I found that applying { define: { "globalThis.process.env": "{}" } } to the source code globalThis.process?.env[o] triggers a panic.

Here are playground links:

@hi-ogawa
Copy link
Collaborator

I just thought of the quick workaround to replace offending globalThis.process?.env[o] via plugin. This also verifies that the underlying issue is only this exact expression:

https://stackblitz.com/edit/vitejs-vite-k6fmaw?file=vite.config.ts

import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    {
      name: 'workaround-esbuild-define-panic',
      enforce: 'pre',
      transform(code) {
        return code.replace(
          'globalThis.process?.env[o]',
          '(globalThis.process?.env)[o]'
        );
      },
    },
  ],
});

@hi-ogawa
Copy link
Collaborator

This should be fixed in esbuild v0.19.10 https://github.com/evanw/esbuild/releases/tag/v0.19.10

I confirmed vite build succeeds on stackblitz via package.json "overrides": { "esbuild": "0.19.10" }

https://stackblitz.com/edit/vitejs-vite-21ssdn?file=package.json

@sapphi-red sapphi-red added bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 19, 2023
@sapphi-red
Copy link
Member

@hi-ogawa Thanks for investigating and the fix 💚

@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2024
crearis added a commit to theaterpedia/theaterpedia-core that referenced this issue Jan 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

3 participants