Rslib Documentation

repository·main·Indexed 21 days ago

https://github.com/web-infra-dev/rslib

A specialized library development tool for creating JavaScript and TypeScript libraries. Built on the Rsbuild and Rspack ecosystem, Rslib supports high-performance builds with flexible output formats (ESM, CJS, UMD), declaration generation via rsbuild-plugin-dts, and Module Federation. It provides workflows for bundling components in React, Vue, SolidJS, and Preact, as well as Node.js packages for Express.

Tokens
84.9K
Snippets
349
Records
456
Agent score
74%

What's inside Rslib

  1. What is Rslib and why use it?

    main

    Rslib is a library development tool based on Rspack and Rsbuild. It is designed to simplify the creation of JavaScript and UI component libraries by providing a unified, high-performance build experience.

    Key benefits include:

    • Multi-Format Output: Support for ESM, CJS, UMD, and Module Federation within a single configuration.
    • Ecosystem Reuse: Leverages the Rspack and Rsbuild ecosystems, allowing you to use existing Rsbuild plugins and loaders (e.g., for Sass, Less, Tailwind CSS).
    • Comprehensive Resource Handling: Built-in support for processing styles, static assets (images, fonts, etc.), and TypeScript declaration files (d.ts).
    • Bundle & Bundleless Modes: Supports both traditional bundling and bundleless build modes, including path redirection for ESM compatibility.
    • Performance: Powered by Rspack (implemented in Rust) for fast build speeds.
  2. Introduction to Rslib

    main

    Rslib is a library development tool built on top of Rsbuild. It is designed to simplify the process of building component or utility libraries by providing out-of-the-box support for complex build requirements.

    Key capabilities include:

    • Language Compilation: Supports TypeScript, JSX, Sass, Less, CSS Modules, Wasm, and more.
    • Build Modes: Offers both Bundle and Bundleless options.
    • Output Formats: Generates ESM, CJS, and UMD formats for maximum compatibility.
    • Type Support: Includes declaration file generation (with isolated declarations).
    • Advanced Features: Supports Module Federation, asset compression, PostCSS, and Lightning CSS.

    Rslib leverages the performance of Rspack and the configuration ecosystem of Rsbuild, allowing developers to share build configurations between application and library projects.

  3. What is Rslib

    main

    Rslib is a library development tool built on top of Rsbuild. It provides optimized configurations and plugins designed specifically for library authors, allowing them to leverage the ecosystem of Webpack and Rspack.

    Key capabilities include:

    • Easy Library Creation: Simple workflows for creating high-quality JavaScript and TypeScript libraries.
    • Streamlined Build Configuration: Out-of-the-box build capabilities with concise configurations for diverse library needs.
    • Complete Development Lifecycle: Best practices for building, debugging, documenting, and testing libraries.
  4. Overview of Rslib

    main

    Rslib is a library development tool built on top of Rsbuild. It is designed to simplify the creation of high-quality JavaScript and TypeScript libraries by leveraging the configuration and plugin ecosystem of Rsbuild, Rspack, and Webpack.

    Key capabilities include:

    • Multi-language support: Compiles TypeScript, JSX, Sass, Less, CSS Modules, Wasm, and more.
    • Flexible build modes: Supports both bundle and bundleless build strategies.
    • Multiple output formats: Generates ESM, CJS, and UMD formats for maximum compatibility.
    • Type support: Generates declaration files, including isolated declarations.
    • Advanced features: Built-in support for Module Federation, asset compression, PostCSS, and Lightning CSS.
  5. Use llms.txt for AI documentation discovery

    main

    Rslib provides two files following the llms.txt standard to help LLMs discover and use project documentation:

    • llms.txt: A structured index containing titles, links, and brief descriptions. Use this when you want the AI to fetch specific pages on demand to save tokens.
    • llms-full.txt: A single file containing the complete content of every documentation page. Use this when you need the AI to have a comprehensive understanding of Rslib and your tool supports large context windows.

    URLs:

    • https://rslib.rs/llms.txt
    • https://rslib.rs/llms-full.txt
  6. Shared vs Non-shared lib configurations

    main

    Rslib distinguishes between configurations that can be shared across all outputs and those that must be unique to a specific output.

    Shared Configurations

    These can be placed either at the top level (to apply to all outputs) or inside a specific lib item:

    • autoExtension
    • banner
    • bundle
    • externalHelpers
    • footer
    • format
    • outBase
    • redirect
    • shims
    • syntax
    • wasm

    Non-shared Configurations

    These cannot be used as shared configurations and must be defined within a specific lib item to configure an individual output:

    • dts
    • experiments
    • id
    • umdName
  7. Compare bundle vs bundleless output structures

    main

    The choice between bundle: true and bundle: false significantly changes your dist directory structure.

    Bundle Mode (bundle: true)

    All source files are merged into a single output file. Example Output:

    . 
    ├── dist
    │   └── index.js
    ├── src
    │   ├── index.ts
    │   ├── foo.ts
    │   └── bar.ts
    └── package.json

    Bundleless Mode (bundle: false)

    Source files are transformed into multiple files, maintaining a structure similar to src. Example Output:

    . 
    ├── dist
    │   ├── index.js
    │   ├── foo.js
    │   └── bar.js
    ├── src
    │   ├── index.ts
    │   ├── foo.ts
    │   └── bar.ts
    └── package.json
  8. Understand Rslib configuration structure

    main

    Rslib configuration is split into two main types of options that govern how your library is built:

    1. Lib configurations: Define library outputs, such as output formats (ESM, CJS), output structures, and syntax targets. These are defined within the lib array.
    2. Rsbuild configurations: Control the underlying compilation and build behavior (module resolution, source processing, plugins). These can be shared across all outputs or specific to one.

    Merging Logic:

    • Configurations inside a lib item apply only to that specific output.
    • Configurations outside the lib field are shared across all outputs.
    • Rslib merges shared configuration with each lib item. If a property is defined in both, the lib item's value takes precedence.
    // Example: Shared syntax is es2021, but CJS output overrides it to es2020
    export default {
      lib: [
        { format: 'esm' }, // Inherits es2021
        { format: 'cjs', syntax: 'es2020' }, // Overrides to es2020
      ],
      syntax: 'es2021',
    };
  9. Core features of Rslib

    main

    Rslib focuses on three main pillars for library developers:

    1. Easy to Configure: Provides ready-to-use build capabilities to minimize initial configuration overhead.
    2. Performance Oriented: Integrates high-performance Rust-based tools including Rspack, SWC, and Lightning CSS for fast builds and development.
    3. Plugin Ecosystem: Since it is powered by Rsbuild, it supports the Rsbuild plugin system and is compatible with most webpack plugins and all Rspack plugins.