CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-rslib--core

The Rsbuild-based library development tool.

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration

Type-safe configuration definition and loading system with support for both static and function-based configurations. Provides autocomplete support and flexible configuration patterns.

Capabilities

Define Configuration

Helper function for type-safe configuration definition with autocomplete support.

/**
 * Helper function for type-safe configuration definition with autocomplete support
 * @param config - Static configuration object, sync function, or async function
 * @returns Configuration export with proper typing
 */
function defineConfig(config: RslibConfig): RslibConfig;
function defineConfig(config: RslibConfigSyncFn): RslibConfigSyncFn;
function defineConfig(config: RslibConfigAsyncFn): RslibConfigAsyncFn;

type RslibConfigExport = RslibConfig | RslibConfigSyncFn | RslibConfigAsyncFn;

Usage Examples:

import { defineConfig } from "@rslib/core";

// Static configuration
export default defineConfig({
  lib: [
    { format: "esm", dts: true },
    { format: "cjs" }
  ]
});

// Function-based configuration
export default defineConfig((env) => ({
  lib: [
    {
      format: env.command === "build" ? "esm" : "cjs",
      dts: env.command === "build"
    }
  ]
}));

// Async configuration
export default defineConfig(async (env) => {
  const isDev = env.envMode === "development";
  return {
    lib: [
      {
        format: "esm",
        dts: !isDev,
        bundle: !isDev
      }
    ]
  };
});

Load Configuration

Loads and resolves Rslib configuration from the file system.

/**
 * Loads and resolves Rslib configuration from file system
 * @param options - Configuration loading options
 * @returns Promise with config content and file path
 */
function loadConfig(options?: LoadConfigOptions): Promise<{
  content: RslibConfig;
  filePath: string;
}>;

interface LoadConfigOptions {
  /** Current working directory */
  cwd?: string;
  /** Custom configuration file path */
  path?: string;
  /** Environment mode */
  envMode?: string;
}

Usage Examples:

import { loadConfig, build } from "@rslib/core";

// Load default configuration
const { content: config, filePath } = await loadConfig();
console.log(`Loaded config from: ${filePath}`);

// Load with custom options
const result = await loadConfig({
  path: "./custom.config.js",
  cwd: "./src",
  envMode: "development"
});

// Use loaded config
await build(result.content);

Configuration Function Types

Type definitions for function-based configurations:

/**
 * Synchronous configuration function
 * @param env - Configuration parameters including environment and command
 * @returns Configuration object
 */
type RslibConfigSyncFn = (env: ConfigParams) => RslibConfig;

/**
 * Asynchronous configuration function
 * @param env - Configuration parameters including environment and command
 * @returns Promise resolving to configuration object
 */
type RslibConfigAsyncFn = (env: ConfigParams) => Promise<RslibConfig>;

interface ConfigParams {
  /** Environment (development/production) */
  env: string;
  /** CLI command being executed */
  command: string;
  /** Environment mode */
  envMode?: string;
}

Core Configuration Types

Main Configuration Interface

/**
 * Main configuration interface for Rslib
 * Extends Rsbuild configuration with library-specific options
 */
interface RslibConfig extends RsbuildConfig {
  /** Array of library configurations */
  lib: LibConfig[];
  /** Output configuration */
  output?: RslibOutputConfig;
}

interface RslibOutputConfig {
  /** Target output directory */
  target?: string;
  /** Additional output options */
  [key: string]: any;
}

Library Configuration

/**
 * Individual library configuration
 * Extends Rsbuild environment configuration
 */
interface LibConfig extends EnvironmentConfig {
  /** Unique library identifier */
  id?: string;
  /** Output format (esm, cjs, umd, mf, iife) */
  format?: Format;
  /** Whether to bundle the library */
  bundle?: boolean;
  /** Auto file extension setting */
  autoExtension?: boolean;
  /** Auto externalization settings */
  autoExternal?: AutoExternal;
  /** Target syntax/browsers */
  syntax?: Syntax;
  /** Whether to import SWC helper functions from @swc/helpers */
  externalHelpers?: boolean;
  /** TypeScript declaration settings */
  dts?: Dts;
  /** CommonJS/ESM shims configuration */
  shims?: Shims;
  /** Import path redirection settings */
  redirect?: Redirect;
  /** UMD bundle export name */
  umdName?: Rspack.LibraryName;
  /** The base directory of the output files */
  outBase?: string;
  /** File banners */
  banner?: BannerAndFooter;
  /** File footers */
  footer?: BannerAndFooter;
}

Configuration Patterns

Environment-based Configuration

import { defineConfig } from "@rslib/core";

export default defineConfig((env) => {
  const isDev = env.envMode === "development";
  const isProd = env.env === "production";
  
  return {
    lib: [
      {
        format: "esm",
        dts: isProd,
        bundle: isProd,
        autoExternal: {
          dependencies: true,
          peerDependencies: isProd
        }
      },
      ...(isProd ? [{
        format: "cjs",
        autoExternal: { dependencies: true }
      }] : [])
    ]
  };
});

Command-based Configuration

export default defineConfig((env) => {
  const isInspect = env.command === "inspect";
  
  return {
    lib: [
      {
        format: "esm",
        dts: !isInspect,
        bundle: !isInspect
      }
    ]
  };
});

Async Configuration Loading

export default defineConfig(async (env) => {
  // Load external configuration
  const packageJson = await import("./package.json");
  const hasTypescript = packageJson.devDependencies?.typescript;
  
  return {
    lib: [
      {
        format: "esm",
        dts: !!hasTypescript,
        syntax: env.env === "production" ? "es2018" : "esnext"
      }
    ]
  };
});

Multiple Library Configurations

export default defineConfig({
  lib: [
    {
      id: "browser",
      format: "esm",
      syntax: ["chrome >= 87", "firefox >= 78"],
      dts: true
    },
    {
      id: "node",
      format: "cjs", 
      syntax: "node18",
      shims: {
        esm: {
          __dirname: true,
          __filename: true
        }
      }
    },
    {
      id: "universal",
      format: "umd",
      umdName: "MyLib",
      syntax: "es5"
    }
  ]
});

Advanced Configuration

Unstable Compose Function

/**
 * Unstable API for composing Rsbuild configuration from Rslib config
 * WARNING: This API is marked as unstable and may change
 * @param rslibConfig - Rslib configuration object
 * @returns Array of Rsbuild configurations with library info
 */
function unstable_composeCreateRsbuildConfig(
  rslibConfig: RslibConfig
): Promise<RsbuildConfigWithLibInfo[]>;

interface RsbuildConfigWithLibInfo {
  id?: string;
  format: Format;
  config: EnvironmentConfig;
}

This function allows advanced composition of Rsbuild configurations from Rslib config but should be used with caution as it's marked unstable and may change in future versions.

Usage Example:

import { unstable_composeCreateRsbuildConfig, defineConfig } from "@rslib/core";

const rslibConfig = defineConfig({
  lib: [
    { format: "esm", dts: true },
    { format: "cjs" }
  ]
});

// Get composed Rsbuild configurations
const configs = await unstable_composeCreateRsbuildConfig(rslibConfig);
// Result: Array of { id?, format, config } objects

Install with Tessl CLI

npx tessl i tessl/npm-rslib--core

docs

build.md

cli.md

configuration.md

index.md

tile.json