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

[5.0 beta 13] Issue with define replacement using esbuild #14783

Closed
7 tasks done
smeng9 opened this issue Oct 27, 2023 · 2 comments · Fixed by #14786
Closed
7 tasks done

[5.0 beta 13] Issue with define replacement using esbuild #14783

smeng9 opened this issue Oct 27, 2023 · 2 comments · Fixed by #14786
Labels
p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release

Comments

@smeng9
Copy link
Contributor

smeng9 commented Oct 27, 2023

Describe the bug

I am an author of npm package and in my published package it contains expression like import.meta.env.VITE_API_ORIGIN so my downstream user can use environment variable to override it. I publish my package using vite and I use the define: {"import.meta.env.VITE_API_ORIGIN": "import.meta.env.VITE_API_ORIGIN"} in the vite.config.ts to preserve this expression during transpilation. This works well in v4 and v5 beta until the breaking change in #11151 and I have not found a workaround yet. I have tried using @rollup/plugin-replace but also no luck there.

Reproduction

https://stackblitz.com/edit/vitejs-vite-yht5de?file=vite.config.ts,src%2Findex.ts,package.json,dist%2Findex.js&terminal=dev

Steps to reproduce

Run yarn followed by yarn build

Then go to the dist/index.js file

When using vite 4.5.0, the output is

const t = import.meta.env.VITE_API_ORIGIN;
export {
  t as myurl
};

My users who import this package can use a .env file to change this variable.

When using vite 5.0.0-beta.13, the output becomes

var e = { BASE_URL: "/", MODE: "production", DEV: !1, PROD: !0, SSR: !1 };
const t = e.VITE_API_ORIGIN;
export {
  t as myurl
};

The difference here is import.meta.env expression disappears and my downstream users can no longer use environment variable to perform replacement in the library.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^4.1.0 => 4.1.0 
    vite: ^5.0.0-beta.13 => 5.0.0-beta.13

Used Package Manager

yarn

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Oct 27, 2023

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

@bluwy bluwy added p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release and removed pending triage labels Oct 28, 2023
@bluwy
Copy link
Member

bluwy commented Oct 28, 2023

Thanks for the report. It seems like an optimization I made here:

// user can define keys with the same values to declare that some keys
// should not be replaced. in this case, we delete references of the key
// so they aren't replaced in the first place.
const val = config.define[key]
if (key === val) {
delete processNodeEnv[key]
delete importMetaKeys[key]
continue
}

Was actually causing issues as the unreplaced import.meta.env.VITE_API_ORIGIN identifier was still getting replaced by the import.meta.env object. I think removing that should work.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants