--- url: /guide/in-depth/use-strict.md --- # `"use strict"` Before we dive into the details, let's first add a bit of context. * ESM (ECMAScript Modules) is always in strict mode implicitly. [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#strict_mode_for_modules). This means that if you're using ESM, you don't need to add `"use strict"` at the top of your files. It's enabled by default. * Strict mode isn't just a subset: it intentionally has different semantics from normal code. ## `format: 'esm'` You don't need to care about `"use strict"`, if all your files are ESM. Things will be a bit complicated, if your files contain commonjs modules. Since you want to emit ESM output, you have to ensure that your code of commonjs files is valid in strict mode. Rolldown will parse it in strict mode, and will throw an error if it's not valid. With `format: 'esm'`, rolldown will parse you code in strict mode, and will throw an error that is related to strict mode. After parsing, rolldown will simply removes `"use strict"` from every module if it's present. Emitted code will not contain `"use strict"`, because ESM is already in strict mode. ## `format: 'cjs'` There will be several strategies to handle `"use strict"` while emitting commonjs output. Rollup will always add `"use strict"` at the top of the output file, because it only accepts ESM input. Esbuild emit `"use strict"` conditionally([link](https://github.com/evanw/esbuild/issues/2264#issuecomment-1138927861)) but not perfectly. Esbuild also explains that it's almost not possible to handle `"use strict"` perfectly with mixed ESM and CJS modules with enabling scope hoisting([link](https://github.com/evanw/esbuild/issues/2381#issuecomment-1179765091)). Rolldown choose to emit runnable code in maximum possibility. Rolldown will only add `"use strict"` in each chunk, if all modules of that chunk are satisfied in following conditions: * ESM * Commonjs module with `"use strict"` at the top of the file Otherwise, it will not add `"use strict"` in the chunk. Notice that, this changes the semantics of the original ESM code, because `"use strict"` is not added in the chunk. With `format: 'cjs'`, rolldown will first parse your code in strict mode, and will re-parse it in non-strict mode if it throws an error in the first parsing. See here for more details about what are affected by `"use strict"` [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#changes_in_strict_mode). --- --- url: /guide/in-depth/advanced-chunks.md --- # Advanced Chunks Advanced chunks are a powerful feature that allows you manually control the chunking of your code. This is useful when you want to optimize the loading of your application by splitting it into smaller, more manageable pieces. ## Limitations ### Why there's always a `runtime.js` chunk? tl;dr: If you used `advancedChunks` option, rolldown will forcefully generate a `runtime.js` chunk to ensure that the runtime code is always executed before any other chunks. The `runtime.js` chunk is a special chunk that **only** contains the runtime code necessary for loading and executing your application. It is generated forcefully by the bundler to ensure that the runtime code is always executed before any other chunks. Since advanced chunks allows you to move modules between chunks, it's easily to create a circular import in the output code. This can lead to a situation where the runtime code is not executed before the other chunks, causing errors in your application. A example output code with circular import: ```js // first.js import { __esm, __export, init_second, value$1 as value } from './second.js'; var first_exports = {}; __export(first_exports, { value: () => value$1 }); var value$1; var init_first = __esm({ 'first.js'() { init_second(); // ... }, }); export { first_exports, init_first, value$1 as value }; // main.js import { first_exports, init_first } from './first.js'; import { __esm, init_second, second_exports } from './second.js'; var init_main = __esm({ 'main.js'() { init_first(); init_second(); // ... }, }); init_main(); // second.js import { init_first, value } from './first.js'; var __esm = '...'; var __export = '...'; var second_exports = {}; __export(second_exports, { value: () => value$1 }); var value$1; var init_second = __esm({ 'second.js'() { init_first(); // ... }, }); export { __esm, __export, init_second, second_exports, value$1 }; ``` When we run `node ./main.js`, the traversal order of the modules would be `main.js` -> `first.js` -> `second.js`. The module execution order would be `second.js` -> `first.js` -> `main.js`. `second.js` tries to call `__esm` function before it gets initialized. This will lead to a runtime error which is trying to call `undefined` as a function. With forcefully generated `runtime.js`, the bundler ensures any chunk that depends on runtime code would first load `runtime.js` before executing itself. This guarantees that the runtime code is always executed before any other chunks, preventing circular import issues. --- --- url: /reference/bundler-api.md --- # Bundler API :::warning 🚧 Under Construction We are working on creating a more detailed reference. For now, please refer to [Getting Started](/guide/getting-started#using-the-api). ::: --- --- url: /guide/in-depth/bundling-cjs.md --- # Bundling CJS Rolldown provides first-class support for CommonJS modules. This document explains how Rolldown handles CJS modules and their interoperability with ES modules. ## Key Features ### Native CJS Support Rolldown automatically recognizes and processes CommonJS modules without requiring any additional plugins or packages. This native support means: * No need to install extra dependencies * Better performance compared to plugin-based solutions ### On-demand Execution Rolldown preserves the on-demand execution semantics of CommonJS modules, which is a key feature of the CommonJS module system. This means modules are only executed when they are actually required. Here's an example: ```js // index.js import { value } from './foo.js'; const getFooExports = () => require('./foo.js'); // foo.js module.exports = { value: 'foo' }; ``` When bundled, it produces: ```js // #region rolldown:runtime // ...runtime code // #endregion // #region foo.js var require_foo = __commonJS({ 'foo.js'(exports, module) { module.exports = { value: 'foo' }; }, }); // #endregion // #region index.js const getFooExports = () => require_foo(); // #endregion ``` In this example, the `foo.js` module won't be executed until `getFooExports()` is called, maintaining the lazy-loading behavior of CommonJS. ### ESM/CJS Interoperability Rolldown provides seamless interoperability between ES modules and CommonJS modules. Example of ESM importing from CJS: ```js // index.js import { value } from './foo.js'; console.log(value); // foo.js module.exports = { value: 'foo' }; ``` Bundled output: ```js // #region rolldown:runtime // ...runtime code // #endregion // #region foo.js var require_foo = __commonJS({ 'foo.js'(exports, module) { module.exports = { value: 'foo' }; }, }); // #endregion // #region index.js var import_foo = __toESM(require_foo()); console.log(import_foo.value); // #endregion ``` The `__toESM` helper ensures that CommonJS exports are properly converted to ES module format, allowing seamless access to the exported values. ## `require` external modules When [`platform: 'node'`](../features.md#platform-presets) is set, Rolldown will generate a `require` function from [`module.createRequire`](https://nodejs.org/docs/latest/api/module.html#modulecreaterequirefilename). For other platforms, Rolldown will leave it as it is, so ensure that the running environment provides a `require` function or inject one manually. For example, you can inject the `require` function that returns the value obtained by `import` by using [`inject` feature](../features.md#inject). ::: code-group ```js [rolldown.config.js] import path from 'node:path'; export default { inject: { require: path.resolve('./require.js'), }, }; ``` ```js [require.js] import fs from 'node:fs'; export default (id) => { if (id === 'node:fs') { return fs; } throw new Error(`Requiring ${JSON.stringify(id)} is not allowed.`); }; ``` ::: ## Future Plans Rolldown's first-class support for CommonJS modules enables several potential optimizations: * Advanced tree-shaking capabilities for CommonJS modules * Better dead code elimination --- --- url: /reference/cli.md --- # Command Line Interface :::warning 🚧 Under Construction For now this is just the output of `rolldown --help`. ::: ```sh USAGE rolldown -c or rolldown OPTIONS --config -c, Path to the config file (default: rolldown.config.js). --dir -d, Output directory, defaults to dist if file is not set. --external -e, Comma-separated list of module ids to exclude from the bundle ,.... --format -f, Output format of the generated bundle (supports esm, cjs, and iife). --globals -g, Global variable of UMD / IIFE dependencies (syntax: key=value). --help -h, Show help. --minify -m, Minify the bundled file. --name -n, Name for UMD / IIFE format outputs. --file -o, Single output file. --platform -p, Platform for which the code should be generated (node, browser, neutral). --sourcemap -s, Generate sourcemap (-s inline for inline, or pass the -s on the last argument if you want to generate .map file). --version -v, Show version number. --watch -w, Watch files in bundle and rebuild on changes. --advanced-chunks.min-share-count Minimum share count of the chunk. --advanced-chunks.min-size Minimum size of the chunk. --asset-file-names Name pattern for asset files. --banner Code to insert the top of the bundled file (outside the wrapper function). --checks.circular-dependency Whether to emit warnings when detecting circular dependencies. --chunk-file-names Name pattern for emitted secondary chunks. --comments Control comments in the output. --css-chunk-file-names Name pattern for emitted css secondary chunks. --css-entry-file-names Name pattern for emitted css entry chunks. --cwd Current working directory. --define Define global variables. --drop-labels Remove labeled statements with these label names. --entry-file-names Name pattern for emitted entry chunks. --es-module Always generate __esModule marks in non-ESM formats, defaults to if-default-prop (use --no-esModule to always disable). --exports Specify a export mode (auto, named, default, none). --extend Extend global variable defined by name in IIFE / UMD formats. --footer