vite-env-only is a suite of Vite plugins designed to facilitate the isolation of client-side and server-side code within a single codebase. It provides two primary functionalities: `denyImports` to prevent specific modules or files from being bundled into client or server builds, and `envOnlyMacros` (`serverOnly$`, `clientOnly$`) to conditionally include or exclude expressions based on the target environment. The current stable version is 3.0.3, with a release cadence that includes regular patch updates for improved dead code elimination and occasional minor/major releases for new features or architectural changes. A key differentiator from Vite's native `import.meta.env.SSR` is its ability to perform dead-code elimination in development builds, not just production, leading to more accurate environment simulation and smaller bundles during development.
npm install vite-env-onlyVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to configure both `denyImports` and `envOnlyMacros` in `vite.config.ts`, along with basic usage of `serverOnly$` and `clientOnly$` macros in application code, highlighting type safety.
Update your `vite.config.ts` imports from `import plugin from 'vite-env-only'` to `import { envOnlyMacros, denyImports } from 'vite-env-only'`.Change your application code imports for macros from `import { serverOnly$ } from 'vite-env-only'` to `import { serverOnly$ } from 'vite-env-only/macros'`.Account for `undefined` in your TypeScript code using type narrowing (`if (value !== undefined)`) or non-null assertions (`value!`) if you are certain it will be present in the target environment.
Upgrade to `vite-env-only@2.4.0` or newer to utilize glob pattern matching for import denial rules.
Change your `vite.config.ts` import from `import plugin from 'vite-env-only'` to named imports like `import { envOnlyMacros } from 'vite-env-only'`.Ensure you are importing macros from the correct path: `import { serverOnly$ } from 'vite-env-only/macros'`.Refactor your code to ensure that modules/files explicitly denied for a specific environment are not imported or referenced in that environment's code path. Use macros like `serverOnly$` or `clientOnly$` to conditionally exclude code.