For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/basic/env-vars.md.
close
  • English
  • Environment variables

    This page describes how Rslib handles import.meta.env.* and process.env.* references in source code when generating library output.

    Default behavior

    Rslib handles built-in env preset references differently for each output format:

    Output formatimport.meta.envprocess.env.BASE_URL / process.env.ASSET_PREFIXOther process.env.*
    ESMPreservedPreservedPreserved
    CJSReplaced with undefinedPreservedPreserved
    UMD / IIFE / MFRsbuild's preset values are inlinedRsbuild's preset values are inlinedPreserved

    The preset values in the table follow Rsbuild's environment variable handling; see Rsbuild - Environment Variables for details.

    Preserved references can be replaced by a downstream build tool or resolved by the target runtime.

    Replacing with specific values at build time

    To replace an environment variable with a specific value when building the library, define it explicitly with source.define:

    rslib.config.ts
    import { defineConfig } from '@rslib/core';
    
    export default defineConfig({
      source: {
        define: {
          'import.meta.env.MODE': JSON.stringify('production'),
          'process.env.BASE_URL': JSON.stringify('/'),
        },
      },
    });