命令行接口
Rollup 通常应该从命令行使用。你可以提供一个可选 的 Rollup 配置文件,以简化命令行使用并启用高级 Rollup 功能。
配置文件
Rollup 配置文件是可选的,但它们非常强大和方便,因此推荐使用。配置文件是一个 ES 模块,它导出一个默认对象,其中包含所需的选项:
/** @type {import('rollup').RollupOptions} */
// ---cut---
export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
}
};
通常,它被称为 rollup.config.js
或 rollup.config.mjs
,并位于项目的根目录中。除非使用 --configPlugin
或 --bundleConfigAsCjs
选项,否则 Rollup 将直接使用 Node 导入该文件。请注意,使用原生 Node ES 模块时存在一些注意事项,因为 Rollup 将遵 循 Node ESM 语义。
如果你想使用 require
和 module.exports
编写 CommonJS 模块的配置文件,你应该将文件扩展名更改为 .cjs
。
你也可以使用其他语言编写配置文件,比如 TypeScript。为此,安装相应的 Rollup 插件,例如 @rollup/plugin-typescript
,并使用 --configPlugin
选项:
rollup --config rollup.config.ts --configPlugin typescript
使用 --configPlugin
选项将始终强制将你的配置文件先转换为 CommonJS 格式。同时,查看 Config Intellisense 以获取在配置文件中使用 TypeScript 类型定义的更多方法。
配置文件支持下面列出的选项。有关每个选项的详细信息,请参阅选项大全:
// rollup.config.js
// 可以是数组(即多个输入源)
// ---cut-start---
/** @type {import('rollup').RollupOptions} */
// ---cut-end---
export default {
// 核心输入选项
external,
input, // 有条件地需要
plugins,
// 进阶输入选项
cache,
logLevel,
makeAbsoluteExternalsRelative,
maxParallelFileOps,
onLog,
onwarn,
preserveEntrySignatures,
strictDeprecations,
// 危险区域
context,
moduleContext,
preserveSymlinks,
shimMissingExports,
treeshake,
// 实验性
experimentalCacheExpiry,
experimentalLogSideEffects,
experimentalMinChunkSize,
perf,
// 必需(可以是数组,用于描述多个输出)
output: {
// 核心输出选项
dir,
file,
format,
globals,
name,
plugins,
// 进阶输出选项
assetFileNames,
banner,
chunkFileNames,
compact,
dynamicImportInCjs,
entryFileNames,
extend,
externalImportAttributes,
footer,
generatedCode,
hashCharacters,
hoistTransitiveImports,
importAttributesKey,
inlineDynamicImports,
interop,
intro,
manualChunks,
minifyInternalExports,
outro,
paths,
preserveModules,
preserveModulesRoot,
sourcemap,
sourcemapBaseUrl,
sourcemapDebugIds,
sourcemapExcludeSources,
sourcemapFile,
sourcemapFileNames,
sourcemapIgnoreList,
sourcemapPathTransform,
validate,
// 危险区域
amd,
esModule,
exports,
externalLiveBindings,
freeze,
indent,
noConflict,
sanitizeFileName,
strict,
systemNullSetters,
// 实验性
experimentalMinChunkSize
},
watch: {
buildDelay,
chokidar,
clearScreen,
exclude,
include,
skipWrite
}
};
你可以从配置文件中导出一个数组,以便一次从多个不相关的输入进行打包,即使在监视模式下也可以。要使用相同的输入打出不同的包,你需要为每个输入提供一个输出选项数组:
// rollup.config.js (building more than one bundle)
// ---cut-start---
/** @type {import('rollup').RollupOptions[]} */
// ---cut-end---
export default [
{
input: 'main-a.js',
output: {
file: 'dist/bundle-a.js',
format: 'cjs'
}
},
{
input: 'main-b.js',
output: [
{
file: 'dist/bundle-b1.js',
format: 'cjs'
},
{
file: 'dist/bundle-b2.js',
format: 'es'
}
]
}
];
如果你想异步创建 配置文件,Rollup 也可以处理解析为对象或数组的 Promise
。
// rollup.config.js
import fetch from 'node-fetch';
export default fetch('/some-remote-service-which-returns-actual-config');
类似地,你也可以这样做:
// rollup.config.js (Promise 解析为数组)
export default Promise.all([fetch('get-config-1'), fetch('get-config-2')]);
要使用配置文件来运行 Rollup,请传递 --config
或 -c
标志:
# 向 Rollup 传递自定义配置文件位置
rollup --config my.config.js
# 如果你没有传递文件名,Rollup 将会尝试
# 按照以下顺序加载配置文件:
# rollup.config.mjs -> rollup.config.cjs -> rollup.config.js
rollup --config
你还可以导出一个返回任何上述配置格式的函数。该函数将传递当前的命令行参数,以便你可以动态地调整你的配置以遵循例如 --silent
。如果你使用 config
作为前缀定义自己的命令行选项,你甚至可以自定义它们:
// rollup.config.js
import defaultConfig from './rollup.default.config.js';
import debugConfig from './rollup.debug.config.js';
// ---cut-start---
/** @type {import('rollup').RollupOptionsFunction} */
// ---cut-end---
export default commandLineArgs => {
if (commandLineArgs.configDebug === true) {
return debugConfig;
}
return defaultConfig;
};