JavaScript API

Rsbuild provides a comprehensive JavaScript API for developers to build higher-level tools or frameworks on top of Rsbuild.

Rsbuild's JavaScript API can be used in Node.js, Deno, or Bun.

Access example

Here is a basic example of how to access the Rsbuild JavaScript API.

1. Install Rsbuild

You need to install the @rsbuild/core package:

npm
yarn
pnpm
bun
npm add @rsbuild/core -D

2. Create an Rsbuild instance

You can call the createRsbuild method to create an Rsbuild instance:

import { createRsbuild } from '@rsbuild/core';

const rsbuild = await createRsbuild();

The createRsbuild method provides some options, which you can learn more about in the API - createRsbuild.

3. Call Rsbuild instance method

The Rsbuild instance provides several methods that you can use according to your scenario.

For local development, we recommend using the rsbuild.startDevServer method, which starts a local dev server.

await rsbuild.startDevServer();

After successfully starting the dev server, you will see the following logs:

  ➜ Local:    http://localhost:3000
  ➜ Network:  http://192.168.0.1:3000

For production deployment, we recommend using the rsbuild.build method, which builds the production outputs.

await rsbuild.build();

For more introduction of Rsbuild instance methods, please read the Rsbuild Instance chapter.

After completing these three steps, you have learned the basic usage of Rsbuild. Next, you can customize the build process through Rsbuild plugins and configurations.

Exports format

Rsbuild provides exports in both ES Modules and CommonJS formats:

index.mjs
import { createRsbuild } from '@rsbuild/core';
index.cjs
const { createRsbuild } = require('@rsbuild/core');

We recommend using the ES modules format, which better aligns with community standards.