一、rollup 概念
Rollup 是一个 JavaScript 模块打包器,可以将小块代码编译成大块复杂的代码,例如 library 或应用程序。
二、安装
npm install --global rollup
三、简单实例(对于NodeJS):
方式1
- 创建 main.ts
console.log('rollup')
- 打包
# compile to a CommonJS module ('cjs')
$ rollup main.ts --file main.js --format cjs
- 运行
node main.js
方式2
- 创建 main.ts
console.log('rollup')
- 创建rollup.config.js
// rollup.config.js
export default {
input: 'main.ts',
output: {
file: 'main.js',
format: 'cjs'
}
};
- 打包
rollup -c
备注:
- 如果出错:Unexpected token (Note that you need plugins to import files that are not JavaScript)
- npm install rollup-plugin-typescript2
- rollup.config.js
import typescript from "rollup-plugin-typescript2";
export default {
input: 'xrark.ts',
output: {
file: 'main.js',
format: 'cjs'
},
plugins: [typescript()],
};