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 5.4.0 env var fails when bundling Vite-bundled library #17874

Closed
7 tasks done
bluwy opened this issue Aug 12, 2024 · 5 comments · Fixed by #17876
Closed
7 tasks done

Vite 5.4.0 env var fails when bundling Vite-bundled library #17874

bluwy opened this issue Aug 12, 2024 · 5 comments · Fixed by #17876
Labels
p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release

Comments

@bluwy
Copy link
Member

bluwy commented Aug 12, 2024

Describe the bug

This was brought up at #17648 (comment)

When a library uses Vite (library mode) to bundle and uses env var, the const __vite_import_meta_env__ = { may get included in the code. When that happens and we bundle that library in our app, we re-inject the variable declaration again here, which causes a runtime error:

// If there's bare `import.meta.env` references, prepend the banner
if (bareImportMetaEnvRe.test(result.code)) {
result.code = banner + result.code
if (result.map) {
const map = JSON.parse(result.map)
map.mappings = ';' + map.mappings
result.map = map
}
}

Reproduction

silverwind/vite-import-meta#1

Steps to reproduce

No response

System Info

n/a

Used Package Manager

npm

Logs

Click to expand
error during build:
[commonjs--resolver] node_modules/lib/dist/index.js (3392:6): Identifier "__vite_import_meta_env__" has already been declared
file: node_modules/lib/dist/index.js:3392:6

3390: const create = (createState) => createState ? createImpl(createState) : createImpl;
3391: const __vite_import_meta_env__ = { "BASE_URL": "/", "DEV": false, "MODE": "production", "PROD": true, "SSR": false };
3392: function createJSONStorage(getStorage, options2) {
            ^
3393:   let storage;
3394:   try {

    at getRollupError (node_modules/vite/node_modules/rollup/dist/es/shared/parseAst.js:392:41)
    at error (node_modules/vite/node_modules/rollup/dist/es/shared/parseAst.js:388:42)
    at Module.error (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:13944:16)
    at ModuleScope.addDeclaration (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:7362:21)
    at ModuleScope.addDeclaration (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:12075:22)
    at Identifier.declare (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:6960:39)
    at VariableDeclarator.declareDeclarator (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:10661:17)
    at VariableDeclaration.initialise (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:12343:24)
    at convertNode (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:13167:10)
    at convertNodeList (node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:13177:34)

Validations

@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 Aug 12, 2024
@bluwy
Copy link
Member Author

bluwy commented Aug 12, 2024

On top of the discussions at #17648, I think it may be easiest to increment the variable name if the existing one is already used. Performance might be affected a bit, but not every module would access env vars so it's probably fine.

Another solution might be to give a different variable name when building in library mode, but that would still cause problems if the Vite library bundles an internal Vite library too.

@yuzheng14
Copy link
Contributor

I think that we could use a virtual module like vite:importAnalysisBuild to let rollup handle the name.

@bluwy
Copy link
Member Author

bluwy commented Aug 12, 2024

I don't think we need to go that far to resolve this naming issue. Doing an includes check to see if the variable name is used, and if so, append a 1, 2, etc to the variable name should be enough.

@yuzheng14
Copy link
Contributor

I've created #17876 to fix this.

@pedro-rodrigues-la
Copy link

Also encountered this issue.
In my case I have a react library (created by me using vite) being used by another project (also using react and vite), both the library and the project uses the "import.meta" statement, no variable is declared with the same name between both but the env variable (default one created by vite) seems to be the one causing the conflict:

error during build: project/dist/project.js (2345:6): Identifier "__vite_import_meta_env__" has already been declared

@github-actions github-actions bot locked and limited conversation to collaborators Aug 30, 2024
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.

3 participants