fix: define process.env variables in atoms vite config to prevent 'process is undefined' errors#24665
Conversation
…ocess is undefined' errors - Add all process.env variables used by Next.js code to the define section - This ensures they are replaced at build time and don't cause runtime errors in Vite projects - Fixes #24213 Co-Authored-By: somay@cal.com <somaychauhan98@gmail.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
tested it in a local vite project, works 👍 |
How to Test:
The final entry will look something like this: "@calcom/atoms": "file:../cal.com/packages/platform/atoms" |
E2E results are ready! |
What does this PR do?
Fixes the "process is undefined" error that occurs when using
@calcom/atomsin Vite projects by defining allprocess.envvariables used by the bundled code at build time.Problem
When users install and use
@calcom/atomsin their Vite projects, they encounter a runtime error:Uncaught ReferenceError: process is not defined. This happens because:process.envvariablesprocessglobal by default (unlike webpack/Next.js)process.env.NODE_ENV,process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA, etc.Previously, users had to add a workaround to their own vite.config:
This PR fixes the issue at the source by defining all necessary
process.envvariables in the atoms build configuration.Solution
How It Works
The
defineoption in Vite's config performs static replacement at build time - it literally replaces the text in the code before bundling. For example,process.env.NODE_ENVbecomes"production"in the final bundle, eliminating any runtime dependency on theprocessobject.Example transformation:
Before (in source code):
After build (in bundled atoms):
Which then gets optimized away by the minifier since the condition is always false. The published atoms package has zero runtime dependencies on
process- it's all static values!Variables Defined
Added comprehensive
process.envvariable definitions to the Vite config'sdefinesection:1.
process.env.NODE_ENV- Set to the build mode (production/development)if (process.env.NODE_ENV !== "production") { console.warn(...) }2.
process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA- Git commit hash3.
process.env.NEXT_PUBLIC_CALCOM_VERSION- Cal.com version numberhttps://app.cal.com/icons/sprite.svg?v=${process.env.NEXT_PUBLIC_CALCOM_VERSION}4. Next.js Internal Flags (
__NEXT_ROUTER_BASEPATH,__NEXT_I18N_SUPPORT,__NEXT_MANUAL_TRAILING_SLASH,__NEXT_TRAILING_SLASH)falseor""because atoms doesn't use Next.js routing5.
"process.env": "{}"- The catch-allprocess.envreferences with an empty objectOrder Matters!
The specific variables must come before the catch-all
"process.env": "{}"because Vite processes them in order. If the catch-all was first, it would replace everything and the specific values wouldn't work.Why This Is Safe for All Environments
1. Build-time replacement only affects the atoms bundle
definevalues only apply when building the @calcom/atoms package itselfprocess.envreferences already replaced with static values2. Works in all environments
processobject needed - everything is already replaced with static valuesprocess.envhandling, but it's not needed since the values are already baked into the atoms bundleprocessdependencies3. No conflicts
process.envat runtime anymoreprocess.envhandling is completely separate and unaffectedWhat About
import.meta.env.VITE_*Variables?You might notice that atoms code also uses
import.meta.env.VITE_BOOKER_EMBED_OAUTH_CLIENT_IDandimport.meta.env.VITE_BOOKER_EMBED_API_URL. These are NOT included in this fix because:import.meta.env- notprocess.env.envfilesprocess.envvariables which are from bundled Next.js code and need static valuesThe
process.envvariables we're defining are different - they're from Next.js code that got bundled into atoms and aren't meant to be user-configurable.How should this be tested?
Create a new Vite project:
npm create vite@latest test-atoms -- --template react-ts cd test-atoms npm install @calcom/atomsUse atoms in your app:
Run the dev server:
Expected result: The app should load without "process is undefined" errors in the browser console
Test repository: https://github.com/ibex088/vite-test - Demonstrates the issue with current published version
Mandatory Tasks
Checklist
Link to Devin run: https://app.devin.ai/sessions/5c92cb6a95274cd08340f670739ad744
Requested by: @SomayChauhan (somay@cal.com)