UNPKG

@samual/rolldown-config

Version:
112 lines (73 loc) • 4.26 kB
# Samual's Rolldown Config Samual's personal opinionated [Rolldown](https://rolldown.rs/) configuration for building JavaScript and TypeScript files. [Contribute](https://codeberg.org/Samual/rolldown-config) or [open an issue](https://codeberg.org/Samual/rolldown-config/issues). ## Install Install **Rolldown** and [`@samual/rolldown-config`](https://npmx.dev/package/@samual/rolldown-config): ```sh npm install rolldown @samual/rolldown-config --save-dev ``` ## Usage Add this Rolldown configuration file to your project root folder: ```js // rolldown.config.js import { rolldownConfig } from "@samual/rolldown-config" export default rolldownConfig() ``` Run `./node_modules/.bin/rolldown --config` [See the official Rolldown Getting Started Guide for more information.](https://rolldown.rs/guide/getting-started) ## What It Does The build reads every `.js` and `.ts` file in the source folder and its sub-folders. It skips test files with the extension `.test.js` or `.test.ts`. It skips declaration files with the extension `.d.ts`. The build writes the output files to the output folder. It keeps the folder structure of the source folder. For example, the file `src/foo.ts` becomes `dist/foo.js`. The file `src/bar/baz.ts` becomes `dist/bar/baz.js`. The output keeps every export of the source files. The configuration treats an import as external (not included in the output files) when the path is not relative and not absolute. Relative and absolute imports go into the output files. The configuration also enables tree shaking (removal of unused code). It tells Rolldown that your modules have no side effects. A side effect changes something outside the function. ## Transforms This build configuration uses [Babel](https://babeljs.io/), [Terser](https://terser.org/), and [Prettier](https://prettier.io/) to transform code. ### Babel Babel transforms files with the extension `.ts` to JavaScript. It targets Node 22.12. It also runs the plugins [Babel Plugin HERE](https://npmx.dev/package/babel-plugin-here) and [Babel Plugin Vitest](https://npmx.dev/package/babel-plugin-vitest). ### Terser Terser makes the output code smaller. It removes unused code but keeps the original variable names. ### Prettier Prettier formats the output code. It uses tabs and a line width of 120 characters. It removes semicolons and trailing commas (commas after the last item). It removes the parentheses around the single parameter of an arrow function. ## Options You can pass an object to `rolldownConfig()`. Every property in the object is optional and represents an option. ### `sourcePath` - Type: `string` - Default: `"src"` Sets the source folder. #### Example Change the source folder to `source`. ```js rolldownConfig({ sourcePath: "source" }) ``` ### `rolldownOptions` - See: [Official Rolldown docs.](https://rolldown.rs/reference/) Overrides the Rolldown configuration. #### Example Set the output folder to `build`. ```js rolldownConfig({ rolldownOptions: { output: { dir: "build" } } }) ``` ### `babelOptions` - See: [Official Babel docs.](https://babeljs.io/docs/options) Overrides the Babel configuration. ### `terserOptions` - See: [Official Terser docs.](https://terser.org/docs/options/) Overrides the Terser configuration. ### `prettierOptions` - See: [Official Prettier docs.](https://prettier.io/docs/options) Overrides the Prettier configuration. ### `experimental` Options under `experimental` may not be safe and are not subject to [Semver](https://semver.org/). ### `experimental.noSideEffects` - Type: `boolean` - Default: `false` If set to `true`, marks pure functions. A pure function has no side effects. The build inserts the comment `/*@__NO_SIDE_EFFECTS__*/` before each pure function in the output code. The comment helps Rolldown remove unused calls during tree shaking. > **Warning:** If a function with side effects counts as pure by mistake, the build can remove a call that your code needs. ### `experimental.disablePrettier` - Type: `boolean` - Default: `false` If set to `true`, disables use of Prettier. The build writes all output without formatting. ### `experimental.dts` - Type: `boolean` - Default: `false` If set to `true`, the build writes `.d.ts` files to the output folder.