-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrollup.config.js
More file actions
59 lines (54 loc) · 2.37 KB
/
rollup.config.js
File metadata and controls
59 lines (54 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import babel from 'rollup-plugin-babel' // babel转换
import resolve from 'rollup-plugin-node-resolve' // rollup不知道如何去处理外部依赖,所以需要resolve插件帮忙解析
import commonjs from 'rollup-plugin-commonjs' // 一些库导出成你可以正常导入的ES6模块,目前,npm中的大多数包都是以CommonJS模块的形式出现的,所以需要使用commonjs插件将cjs模块转换成esm模块供rollup处理;rollup-plugin-commonjs应该用在其他插件转换你的模块之前 - 这是为了防止其他插件的改变破坏CommonJS的检测。
import minify from 'rollup-plugin-babel-minify'
import json from 'rollup-plugin-json' // 将json转为es6模块
import typescript from 'rollup-plugin-typescript2' // 编译ts
import sourceMaps from 'rollup-plugin-sourcemaps' // 生成sourcemap
// rollup支持以下格式
// amd – 异步模块定义,用于像RequireJS这样的模块加载器
// cjs – CommonJS,适用于 Node 和 Browserify/Webpack
// es – 将软件包保存为ES模块文件
// iife – 一个自动执行的功能,适合作为<script>标签。(如果要为应用程序创建一个捆绑包,您可能想要使用它,因为它会使文件大小变小。)
// umd – 通用模块定义,以amd,cjs 和 iife 为一体
// 所以正常情况下打包es及umd格式即可
const GLOBAL_NAME = 'DrawingBoard'
const pkg = require('./package.json')
export default {
input: 'src/main.ts',
output: [
{
file: pkg.main,
format: 'umd',
// umd模式需要指定name
name: GLOBAL_NAME,
sourcemap: true,
},
{
file: pkg.module,
format: 'es',
sourcemap: true,
},
],
plugins: [
json(),
typescript({ useTsconfigDeclarationDir: true }),
// plugins顺序有讲究
// 正常情况,resolve,commonjs应该在第一第二位置
// 但此库使用了es6 class的类属性提案,commonjs无法解析,需要使用@babel/plugin-proposal-class-properties;所以将babel提前到首位
babel({
exclude: 'node_modules/**',
}),
resolve(),
commonjs(),
// 压缩,应该使用在production模式
// 移除comments
minify({ comments: false, removeConsole: true, removeDebugger: true }),
sourceMaps(),
],
external: [
// 配置额外库
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
],
}