Rolldown Documentation

website·Indexed 37 days ago

https://rolldown.rs

A high-performance JavaScript bundler. Documentation covers installation, core concepts like automatic and manual code splitting, dead code elimination, and support for CommonJS and non-ESM output formats. Includes a detailed API reference for InputOptions, including configuration for plugins, external modules, and platform presets.

Tokens
98.9K
Snippets
494
Records
789
Agent score
72%

What's inside Rolldown

  1. Overview of Rolldown bundler

    Rolldown is a high-performance JavaScript bundler written in Rust. While primarily designed to serve as the underlying bundler for Vite (replacing both esbuild and Rollup), it is also a standalone, general-purpose bundler. It aims to provide the performance of esbuild with the ecosystem compatibility and plugin API of Rollup.
  2. Overview of Rolldown CommonJS (CJS) support

    Rolldown provides native, first-class support for CommonJS modules without requiring additional plugins. It preserves on-demand execution semantics, meaning CJS modules are only executed when they are actually required, maintaining lazy-loading behavior.
  3. Overview of Manual Code Splitting in Rolldown

    Manual code splitting allows developers to complement automatic code splitting by explicitly defining how modules should be grouped into chunks. This is primarily used to optimize loading performance and minimize cache invalidation. Manual and automatic splitting coexist: if a module is not captured by a manual splitting rule, it is handled by the automatic splitting logic.
  4. Access the PluginContext interface

    The PluginContext interface extends MinimalPluginContext and is extended by TransformPluginContext. It provides essential utilities for plugins to interact with the Rolldown build process, such as accessing the file system via fs and retrieving metadata via meta.
  5. Understand Dead Code Elimination (DCE) in Rolldown

    Dead code elimination (DCE) is an optimization technique used by Rolldown to remove unused code from the final bundle to reduce size and improve load speed. Rolldown removes code only if it meets two conditions: it is not used (the value is never accessed) and it has no side effects (removing it does not change the program's behavior).
    // math.js
    export function add(a, b) { return a + b; }
    export function multiply(a, b) { return a * b; }
    
    // main.js
    import { add } from './math.js';
    console.log(add(2, 3));
    // Result: 'multiply' is removed because it is unused and side-effect-free.
  6. Use the OutputChunk interface to inspect generated bundle chunks

    The OutputChunk interface provides detailed information about a specific chunk within a generated bundle. It extends ExternalMemoryHandle and contains metadata regarding the chunk's code, imports, exports, and entry status.
    interface OutputChunk extends ExternalMemoryHandle {
      type: "chunk";
      code: string;
      fileName: string;
      name: string;
      imports: string[];
      dynamicImports: string[];
      exports: string[];
      isEntry: boolean;
      isDynamicEntry: boolean;
      facadeModuleId: string | null;
      map: SourceMap | null;
      sourcemapFileName: string | null;
      preliminaryFileName: string;
      moduleIds: string[];
      modules: { [id: string]: RenderedModule };
    }
  7. Understand automatic code splitting in Rolldown

    Automatic code splitting is the process of creating chunks from modules based on predefined rules. It is distinct from manual code splitting and is not directly controllable by the user. Rolldown generates two primary types of chunks: Entry chunks and Common chunks.
  8. Use PluginContextMeta to access Rolldown environment metadata

    The PluginContextMeta interface provides metadata about the current Rolldown execution environment. It is used by plugins to determine the version of the tool being used or whether the process is running in watch mode.
  9. Use the OutputAsset interface to access bundle asset metadata

    The OutputAsset interface provides information about an asset within a generated bundle. It extends ExternalMemoryHandle and is identified by the type property set to 'asset'.
    interface OutputAsset extends ExternalMemoryHandle {
      type: 'asset';
      fileName: string;
      names: string[];
      originalFileNames: string[];
      source: string | Uint8Array<ArrayBufferLike>;
    }
  10. Analyze parsed modules with the moduleParsed hook

    The moduleParsed hook is called each time a module has been fully parsed. It waits until all imports are resolved, ensuring moduleInfo.importedIds and moduleInfo.dynamicallyImportedIds are accurate. However, information about importing modules (who imports this module) may be incomplete; use buildEnd for that information.
  11. Use the ModuleInfo interface to access module metadata

    The ModuleInfo interface (which extends ModuleOptions) provides comprehensive information about a module within the Rolldown bundling process. It is used to track module relationships (imports/exports), source code, and module formats.
    interface ModuleInfo extends ModuleOptions {
      id: string;
      code: string | null;
      exports: string[];
      importedIds: string[];
      importers: string[];
      dynamicallyImportedIds: string[];
      dynamicImporters: string[];
      isEntry: boolean;
      inputFormat: "es" | "cjs" | "unknown";
      description?: string;
    }
  12. Use the RolldownBuild interface to manage bundles

    The RolldownBuild interface is the bundle object returned by the rolldown function. It provides methods to generate output and manage the lifecycle of the build process. If the bundle is closed (checked via the closed accessor), calling other methods on the instance will throw an error.