|
| 1 | +--- |
| 2 | +title: Command Line Interface |
| 3 | +--- |
| 4 | + |
| 5 | +Rollup should typically be used from the command line. You can provide an optional Rollup configuration file to simplify command line usage and enable advanced Rollup functionality. |
| 6 | + |
| 7 | +### Configuration Files |
| 8 | + |
| 9 | +Rollup configuration files are optional, but they are powerful and convenient and thus **recommended**. |
| 10 | + |
| 11 | +A config file is an ES6 module that exports a default object with the desired options. Typically, it is called `rollup.config.js` and sits in the root directory of your project. |
| 12 | + |
| 13 | +Also you can use CJS modules syntax for the config file. |
| 14 | + |
| 15 | +```javascript |
| 16 | +module.exports = { |
| 17 | + input: 'src/main.js', |
| 18 | + output: { |
| 19 | + file: 'bundle.js', |
| 20 | + format: 'cjs' |
| 21 | + } |
| 22 | +}; |
| 23 | +``` |
| 24 | + |
| 25 | +It may be pertinent if you want to use the config file not only from the command line, but also from your custom scripts programmatically. |
| 26 | + |
| 27 | +Consult the [big list of options](guide/en#big-list-of-options) for details on each option you can include in your config file. |
| 28 | + |
| 29 | +```javascript |
| 30 | +// rollup.config.js |
| 31 | + |
| 32 | +export default { // can be an array (for multiple inputs) |
| 33 | + // core input options |
| 34 | + input, // required |
| 35 | + external, |
| 36 | + plugins, |
| 37 | + |
| 38 | + // advanced input options |
| 39 | + onwarn, |
| 40 | + perf, |
| 41 | + |
| 42 | + // danger zone |
| 43 | + acorn, |
| 44 | + acornInjectPlugins, |
| 45 | + treeshake, |
| 46 | + context, |
| 47 | + moduleContext, |
| 48 | + |
| 49 | + // experimental |
| 50 | + experimentalCodeSplitting, |
| 51 | + manualChunks, |
| 52 | + optimizeChunks, |
| 53 | + chunkGroupingSize, |
| 54 | + |
| 55 | + output: { // required (can be an array, for multiple outputs) |
| 56 | + // core output options |
| 57 | + format, // required |
| 58 | + file, |
| 59 | + dir, |
| 60 | + name, |
| 61 | + globals, |
| 62 | + |
| 63 | + // advanced output options |
| 64 | + paths, |
| 65 | + banner, |
| 66 | + footer, |
| 67 | + intro, |
| 68 | + outro, |
| 69 | + sourcemap, |
| 70 | + sourcemapFile, |
| 71 | + sourcemapPathTransform, |
| 72 | + interop, |
| 73 | + extend, |
| 74 | + |
| 75 | + // danger zone |
| 76 | + exports, |
| 77 | + amd, |
| 78 | + indent, |
| 79 | + strict, |
| 80 | + freeze, |
| 81 | + namespaceToStringTag, |
| 82 | + |
| 83 | + // experimental |
| 84 | + entryFileNames, |
| 85 | + chunkFileNames, |
| 86 | + assetFileNames |
| 87 | + }, |
| 88 | + |
| 89 | + watch: { |
| 90 | + chokidar, |
| 91 | + include, |
| 92 | + exclude, |
| 93 | + clearScreen |
| 94 | + } |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +You can export an **array** from your config file to build bundles from several different unrelated inputs at once, even in watch mode. To build different bundles with the same input, you supply an array of output options for each input: |
| 99 | + |
| 100 | +```javascript |
| 101 | +// rollup.config.js (building more than one bundle) |
| 102 | + |
| 103 | +export default [{ |
| 104 | + input: 'main-a.js', |
| 105 | + output: { |
| 106 | + file: 'dist/bundle-a.js', |
| 107 | + format: 'cjs' |
| 108 | + } |
| 109 | +}, { |
| 110 | + input: 'main-b.js', |
| 111 | + output: [ |
| 112 | + { |
| 113 | + file: 'dist/bundle-b1.js', |
| 114 | + format: 'cjs' |
| 115 | + }, |
| 116 | + { |
| 117 | + file: 'dist/bundle-b2.js', |
| 118 | + format: 'esm' |
| 119 | + } |
| 120 | + ] |
| 121 | +}]; |
| 122 | +``` |
| 123 | + |
| 124 | +If you want to create your config asynchronously, Rollup can also handle a `Promise` which resolves to an object or an array. |
| 125 | + |
| 126 | +```javascript |
| 127 | +// rollup.config.js |
| 128 | +import fetch from 'node-fetch'; |
| 129 | +export default fetch('/some-remote-service-or-file-which-returns-actual-config'); |
| 130 | +``` |
| 131 | + |
| 132 | +Similarly, you can do this as well: |
| 133 | + |
| 134 | +```javascript |
| 135 | +// rollup.config.js (Promise resolving an array) |
| 136 | +export default Promise.all([ |
| 137 | + fetch('get-config-1'), |
| 138 | + fetch('get-config-2') |
| 139 | +]) |
| 140 | +``` |
| 141 | + |
| 142 | +You *must* use a configuration file in order to do any of the following: |
| 143 | + |
| 144 | +- bundle one project into multiple output files |
| 145 | +- use Rollup plugins, such as [rollup-plugin-node-resolve](https://github.com/rollup/rollup-plugin-node-resolve) and [rollup-plugin-commonjs](https://github.com/rollup/rollup-plugin-commonjs) which let you load CommonJS modules from the Node.js ecosystem |
| 146 | + |
| 147 | +To use Rollup with a configuration file, pass the `--config` or `-c` flags. |
| 148 | + |
| 149 | +```console |
| 150 | +# use Rollup with a rollup.config.js file |
| 151 | +$ rollup --config |
| 152 | + |
| 153 | +# alternatively, specify a custom config file location |
| 154 | +$ rollup --config my.config.js |
| 155 | +``` |
| 156 | + |
| 157 | +You can also export a function that returns any of the above configuration formats. This function will be passed the current command line arguments so that you can dynamically adapt your configuration to respect e.g. `--silent`. You can even define your own command line options if you prefix them with `config`: |
| 158 | + |
| 159 | +```javascript |
| 160 | +// rollup.config.js |
| 161 | +import defaultConfig from './rollup.default.config.js'; |
| 162 | +import debugConfig from './rollup.debug.config.js'; |
| 163 | + |
| 164 | +export default commandLineArgs => { |
| 165 | + if (commandLineArgs.configDebug === true) { |
| 166 | + return debugConfig; |
| 167 | + } |
| 168 | + return defaultConfig; |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +If you now run `rollup --config --configDebug`, the debug configuration will be used. |
| 173 | + |
| 174 | + |
| 175 | +### Command line flags |
| 176 | + |
| 177 | +Many options have command line equivalents. Any arguments passed here will override the config file, if you're using one. See the [big list of options](guide/en#big-list-of-options) for details. |
| 178 | + |
| 179 | +```text |
| 180 | +-c, --config Use this config file (if argument is used but value |
| 181 | + is unspecified, defaults to rollup.config.js) |
| 182 | +-i, --input Input (alternative to <entry file>) |
| 183 | +-o, --file <output> Output (if absent, prints to stdout) |
| 184 | +-f, --format [esm] Type of output (amd, cjs, esm, iife, umd) |
| 185 | +-e, --external Comma-separate list of module IDs to exclude |
| 186 | +-g, --globals Comma-separate list of `module ID:Global` pairs |
| 187 | + Any module IDs defined here are added to external |
| 188 | +-n, --name Name for UMD export |
| 189 | +-m, --sourcemap Generate sourcemap (`-m inline` for inline map) |
| 190 | +--amd.id ID for AMD module (default is anonymous) |
| 191 | +--amd.define Function to use in place of `define` |
| 192 | +--no-strict Don't emit a `"use strict";` in the generated modules. |
| 193 | +--no-indent Don't indent result |
| 194 | +--environment <values> Environment variables passed to config file |
| 195 | +--noConflict Generate a noConflict method for UMD globals |
| 196 | +--no-treeshake Disable tree-shaking |
| 197 | +--intro Content to insert at top of bundle (inside wrapper) |
| 198 | +--outro Content to insert at end of bundle (inside wrapper) |
| 199 | +--banner Content to insert at top of bundle (outside wrapper) |
| 200 | +--footer Content to insert at end of bundle (outside wrapper) |
| 201 | +--no-interop Do not include interop block |
| 202 | +``` |
| 203 | + |
| 204 | +In addition, the following arguments can be used: |
| 205 | + |
| 206 | +#### `-h`/`--help` |
| 207 | + |
| 208 | +Print the help document. |
| 209 | + |
| 210 | +#### `-v`/`--version` |
| 211 | + |
| 212 | +Print the installed version number. |
| 213 | + |
| 214 | +#### `-w`/`--watch` |
| 215 | + |
| 216 | +Rebuild the bundle when its source files change on disk. |
| 217 | + |
| 218 | +#### `--silent` |
| 219 | + |
| 220 | +Don't print warnings to the console. |
| 221 | + |
| 222 | +#### `--environment <values>` |
| 223 | + |
| 224 | +Pass additional settings to the config file via `process.ENV`. |
| 225 | + |
| 226 | +```sh |
| 227 | +rollup -c --environment INCLUDE_DEPS,BUILD:production |
| 228 | +``` |
| 229 | + |
| 230 | +will set `process.env.INCLUDE_DEPS === 'true'` and `process.env.BUILD === 'production'`. You can use this option several times. In that case, subsequently set variables will overwrite previous definitions. This enables you for instance to overwrite environment variables in `package.json` scripts: |
| 231 | + |
| 232 | +```json |
| 233 | +// in package.json |
| 234 | +{ |
| 235 | + "scripts": { |
| 236 | + "build": "rollup -c --environment INCLUDE_DEPS,BUILD:production" |
| 237 | + } |
| 238 | +} |
| 239 | +``` |
| 240 | + |
| 241 | +If you call this script via: |
| 242 | + |
| 243 | +```console |
| 244 | +npm run build -- --environment BUILD:development |
| 245 | +``` |
| 246 | + |
| 247 | +then the config file will receive `process.env.INCLUDE_DEPS === 'true'` and `process.env.BUILD === 'development'`. |
0 commit comments