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

Env variable is being replace in build time #357

Closed
7 tasks done
guidari opened this issue Aug 15, 2024 · 2 comments
Closed
7 tasks done

Env variable is being replace in build time #357

guidari opened this issue Aug 15, 2024 · 2 comments

Comments

@guidari
Copy link

guidari commented Aug 15, 2024

Describe the bug

I am using this config in my main.js to make the build to work. However, I have defined a global environment variable called __DEV__ in my project, and this variable is being replaced at build time, resulting in the following error: __DEV__ is not defined.

Screenshot 2024-08-13 at 17 01 27

With that in mind, we tried to define __DEV__ in the main.js as follows:

if (configType === 'DEVELOPMENT') {
      config.define = {
        __DEV__: 'true',
      };
    }
    if (configType === 'PRODUCTION') {
      config.define = {
        __DEV__: 'false',
      };
    }

However, a new error appears:

Screenshot 2024-08-15 at 11 19 30

How do we know this is replacing __DEV__ ? We have a component called ModalWrapper that uses __DEV__ and if we comment out the line with the variable, the errors goes away.

Here it is the main.js file in the last commit with the issue

We also tried to use our existing pipeline with babel-plugin-dev-expression but it didn't work.

Possible solution:
Maybe if we can enforce the order that babel will run we can fix it?

Reproduction

carbon-design-system/carbon#16910

Steps to reproduce

I paste the PR I'm working on. You can check the error in the commit:
fix: added babel back
75cc693

Go to Checks to see the CI error in vrt-runner (4)

System Info

System:
    OS: macOS 14.6
    CPU: (10) x64 Apple M1 Pro
    Memory: 32.66 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.14.2 - ~/.nvm/versions/node/v18.14.2/bin/node
    Yarn: 4.2.2 - /usr/local/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.14.2/bin/npm
    Watchman: 2023.08.07.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 127.0.6533.120
    Firefox: 129.0.1
    Safari: 17.6

Used Package Manager

yarn

Logs

No response

Validations

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Aug 16, 2024

The reproduction has many moving parts (babel, custom esbuild config + vite is mainly driven by storybook) and I don't think it's easy to spot anything quickly. We'll probably need a reproduction outside of storybook at least.

Btw from your PR, it's not clear which version of Vite and plugins you're using (or automatically installed by storybook?). Please make sure to provide the information.

I haven't tried to run your PR locally yet, but one thing I might try to debug the broken transform is to maybe add own transform to dump intermediate results, something like:

plugins: [
  {
    name: "debug-transform",
    // enforce: "pre", 
    transform(code, id) {
      if (id.includes("ModalWrapper.tsx")) console.log({ id, code })
    }
  }
]

I'm not sure if Storybook is compatible, but you might also be able to debug using https://github.com/antfu-collective/vite-plugin-inspect

Copy link

Hello @guidari. 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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 10, 2024
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

3 participants