The Rsbuild-based library development tool.
—
Core build functionality for compiling library projects using Rsbuild as the underlying build system. Supports multiple output formats, TypeScript compilation, and advanced features like Module Federation.
Main build function that compiles library projects using Rsbuild with support for multiple output formats.
/**
* Main build function that compiles library projects using Rsbuild
* @param config - Rslib configuration object
* @param options - Build options including watch mode and library selection
* @returns Promise resolving to Rsbuild instance
*/
function build(config: RslibConfig, options?: BuildOptions): Promise<RsbuildInstance>;
interface BuildOptions extends CommonOptions {
/** Enable watch mode for automatic rebuilds */
watch?: boolean;
}Usage Examples:
import { build, loadConfig } from "@rslib/core";
// Basic build
const { content: config } = await loadConfig();
const rsbuildInstance = await build(config);
// Build with watch mode
await build(config, { watch: true });
// Build specific libraries only
await build(config, { lib: ["esm", "cjs"] });Inspects and analyzes the generated bundle configuration without actually building.
/**
* Inspects and analyzes the generated bundle configuration
* @param config - Rslib configuration object
* @param options - Inspection options including mode and output settings
* @returns Promise resolving to Rsbuild instance
*/
function inspect(config: RslibConfig, options?: InspectOptions): Promise<RsbuildInstance>;
interface InspectOptions extends CommonOptions {
/** Build mode (development or production) */
mode?: RsbuildMode;
/** Output directory for inspection results */
output?: string;
/** Enable verbose output */
verbose?: boolean;
}Usage Examples:
import { inspect, loadConfig } from "@rslib/core";
// Basic inspection
const { content: config } = await loadConfig();
await inspect(config);
// Inspect with verbose output
await inspect(config, {
verbose: true,
mode: 'development',
output: './inspect-output'
});Starts Module Federation development server for MF format builds.
/**
* Starts Module Federation development server for MF format builds
* @param config - Rslib configuration object
* @param options - Common CLI options including library selection
* @returns Promise resolving to Rsbuild instance or undefined
*/
function startMFDevServer(
config: RslibConfig,
options?: CommonOptions
): Promise<RsbuildInstance | undefined>;Usage Examples:
import { startMFDevServer, loadConfig } from "@rslib/core";
// Start MF dev server
const { content: config } = await loadConfig();
const server = await startMFDevServer(config);
// Start with specific library
await startMFDevServer(config, { lib: ["mf"] });Supported output formats for library builds:
type Format = 'esm' | 'cjs' | 'umd' | 'mf' | 'iife';esm: ES Modules formatcjs: CommonJS formatumd: Universal Module Definitionmf: Module Federation formatiife: Immediately Invoked Function ExpressionIndividual library configuration within the build system:
interface LibConfig extends EnvironmentConfig {
/** Unique library identifier */
id?: string;
/** Output format */
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;
}
interface AutoExternal {
/** Externalize dependencies */
dependencies?: boolean;
/** Externalize optional dependencies */
optionalDependencies?: boolean;
/** Externalize peer dependencies */
peerDependencies?: boolean;
/** Externalize dev dependencies */
devDependencies?: boolean;
}
type Syntax = EcmaScriptVersion | string[];
type EcmaScriptVersion =
| 'es5' | 'es6' | 'es2015' | 'es2016' | 'es2017' | 'es2018'
| 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'es2023'
| 'es2024' | 'esnext';
interface BannerAndFooter {
/** JavaScript banner/footer */
js?: string;
/** CSS banner/footer */
css?: string;
/** TypeScript declaration banner/footer */
dts?: string;
}TypeScript declaration file configuration:
interface Dts {
/** Bundle declarations */
bundle?: boolean | { bundledPackages?: string[] };
/** Output directory */
distPath?: string;
/** Build project references */
build?: boolean;
/** Abort on errors */
abortOnError?: boolean;
/** Auto extension setting */
autoExtension?: boolean;
/** Path aliases */
alias?: Record<string, string>;
}Shims for cross-format compatibility:
interface Shims {
/** CommonJS shims */
cjs?: { 'import.meta.url'?: boolean };
/** ESM shims */
esm?: {
__filename?: boolean;
__dirname?: boolean;
require?: boolean;
};
}
interface Redirect {
/** JavaScript import redirection */
js?: JsRedirect;
/** Style import redirection */
style?: StyleRedirect;
/** Asset import redirection */
asset?: AssetRedirect;
/** Declaration import redirection */
dts?: DtsRedirect;
}
interface JsRedirect {
/** Whether to automatically redirect the import paths of JavaScript output files */
path?: boolean;
/** Whether to automatically redirect the file extension to import paths */
extension?: boolean;
}
interface StyleRedirect {
/** Whether to automatically redirect the import paths of style output files */
path?: boolean;
/** Whether to automatically redirect the file extension to import paths */
extension?: boolean;
}
interface AssetRedirect {
/** Whether to automatically redirect the import paths of asset output files */
path?: boolean;
/** Whether to automatically redirect the file extension to import paths */
extension?: boolean;
}
interface DtsRedirect {
/** Whether to automatically redirect the import paths of TypeScript declaration output files */
path?: boolean;
/** Whether to automatically redirect the file extension to import paths */
extension?: boolean;
}import { defineConfig } from "@rslib/core";
export default defineConfig({
lib: [
{
format: "esm",
dts: true,
autoExternal: {
dependencies: true,
peerDependencies: true
}
},
{
format: "cjs",
autoExternal: {
dependencies: true
}
},
{
format: "umd",
umdName: "MyLib",
autoExternal: {
peerDependencies: true
}
}
]
});export default defineConfig({
lib: [
{
id: "main",
format: "esm",
bundle: false,
dts: {
bundle: true,
distPath: "./types"
},
shims: {
esm: {
__dirname: true,
__filename: true
}
},
redirect: {
js: false,
style: false
}
}
]
});Install with Tessl CLI
npx tessl i tessl/npm-rslib--core