Rollup Output Configuration and Building Output Management
I. Detailed Explanation of output Configuration
The output field in the Rollup configuration file defines post-bundling behavior, including output directories, filenames, formats, and multi-output management.
1.1 Output Directory
The outDir option specifies the directory where bundled files are placed; it can be set to an absolute path or a relative path.
output: {
outDir: 'dist/',
outFile: 'bundle.js',
format: 'cjs'
}
If outDir is not specified, Rollup defaults to placing output files in the dist/ directory.
1.2 Output Filename
The outFile option defines the name of a single output file and can be used in conjunction with outDir.
output: {
outDir: 'dist/',
outFile: 'bundle.js',
format: 'esm'
}
For multi-file outputs, use an array named outFiles to specify multiple files.
1.3 Output Format
The format option controls the file format of the output and supports cjs (CommonJS), esm (ECMAScript Module), and umd (Universal Module Definition).
output: {
format: 'esm',
entry: 'src/index.js',
outDir: 'dist/'
}
Configuring the format allows flexible selection of bundled file types.
1.4 Multi-output Management
Rollup supports multi-output configurations by defining multiple output files via an array named outFiles.
output: {
outFiles: [
'dist/bundle.cjs',
'dist/bundle.esm'
]
}
This configuration enables generating different format output files from the same source code.
II. Build Output Management
Rollup's output management involves dynamic adjustment of output directories, collaborative work with multi-output scenarios, and custom output logic.
2.1 Dynamic Output Directory
By dynamically specifying an outDir, hardcoded paths can be avoided.
output: {
outDir: 'build/',
format: 'cjs'
}
Rollup automatically places the generated files in the build/ directory without requiring manual management.
2.2 Collaborative Work of Multi-output Scenarios
When multiple outFiles are configured, Rollup automatically merges output files to ensure unique filenames for each file.
output: {
outFile: 'bundle.cjs',
outFiles: [
'dist/bundle.cjs',
'dist/bundle.esm'
]
}
This configuration enables generating multiple format output files from a single source code entry point.
2.3 Custom Output Logic
Custom rollup plugins can be used to define custom output directories and filenames.
export default function rollup() {
return {
input: 'src/index.js',
output: {
outDir: 'build/',
outFile: 'bundle.js',
format: 'esm',
plugins: [customPlugin()]
}
};
}
Plugins can further control output behavior, such as dynamically generating output directories.
III. Practical Recommendations
- Default Configuration: Use
outDirandoutFileto simplify output management and avoid manually specifying paths. - Multi-output Scenarios: Generate multi-format output files using
outFilesto meet requirements for different environments. - Dynamic Paths: Leverage
outDirto achieve dynamic adjustment of the output directory, enhancing flexibility. - Plugin Extension: Combine custom plugins to manage output directories and filenames, achieving finer-grained control.
IV. Summary
The Rollup output configuration is a core component for managing bundled outputs; fields such as outDir, outFile, and format allow flexible control of post-bundling behavior. By combining multi-output management features, it becomes possible to generate multiple format output files from a single source code entry point. Reasonable configuration of output paths and filenames helps improve build efficiency and maintainability of the final outputs.