Environment Context

Environment context is a read-only object that provides some context information about the current environment.

In Rsbuild's hooks, you can get the environment context object through the environment or environments parameter.

type EnvironmentContext = {
  name: string;
  browserslist: string[];
  config: NormalizedEnvironmentConfig;
  distPath: string;
  entry: RsbuildEntry;
  htmlPaths: Record<string, string>;
  tsconfigPath?: string;
};

name

The unique name of the current environment is used to distinguish and locate the environment, corresponds to the key in the environments configuration.

  • Type: string

browserslist

The range of target browsers that the project is compatible with. See details in Browserslist.

  • Type: string[]

config

The Rsbuild environment configuration after normalization.

  • Type:
type NormalizedEnvironmentConfig = DeepReadonly<{
  dev: NormalizedDevConfig;
  html: NormalizedHtmlConfig;
  tools: NormalizedToolsConfig;
  source: NormalizedSourceConfig;
  server: NormalizedServerConfig;
  output: NormalizedOutputConfig;
  plugins?: RsbuildPlugins;
  security: NormalizedSecurityConfig;
  performance: NormalizedPerformanceConfig;
  moduleFederation?: ModuleFederationConfig;
}>;

distPath

The absolute path of the output directory, corresponding to the output.distPath.root config in RsbuildConfig.

  • Type: string

entry

The entry object from the source.entry option.

  • Type:
type RsbuildEntry = Record<string, string | string[] | EntryDescription>;

htmlPaths

The path information for all HTML assets.

This API will return an object, the key is the entry name and the value is the relative path of the HTML file in the dist directory.

  • Type:
type htmlPaths = Record<string, string>;

tsconfigPath

The absolute path of the tsconfig.json file, or undefined if the tsconfig.json file does not exist in current project.

  • Type:
type TsconfigPath = string | undefined;