forked from ouduidui/ga-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
61 lines (58 loc) · 2.27 KB
/
rollup.config.js
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
60
61
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import {terser} from "rollup-plugin-terser";
import {eslint} from "rollup-plugin-eslint";
import babel from "rollup-plugin-babel";
import pkg from './package.json';
import dts from "rollup-plugin-dts";
import fs from "fs";
export default [
{
input: 'src/index.ts', // 打包入口
output: { // 打包出口
file: pkg.browser, // 最终打包出来的文件路径和文件名
format: 'umd', // umd是兼容amd/cjs/iife的通用打包格式,适合浏览器
name: 'index', // umd必填,否则报错
},
plugins: [ // 打包插件
resolve(), // 查找和打包node_modules中的第三方模块
commonjs(), // 将 CommonJS 转换成 ES2015 模块供 Rollup 处理
typescript(), // 解析TypeScript
eslint({ // 代码检测
throwOnError: true,
throwOnWarning: true,
include: ['src/**'],
exclude: ['node_modules/**']
}),
babel({ // 将ES6新特性转换为ES5
exclude: 'node_modules/**', // 防止打包node_modules下的文件
runtimeHelpers: true, // 使plugin-transform-runtime生效
}),
terser(), // 压缩JS代码
updatePkgVersion() // 更新package.json的版本号
]
},
{
// 生成 .d.ts 类型声明文件
input: 'src/index.ts',
output: { // 打包出口
file: 'dist/index.d.ts', // 最终打包出来的文件路径和文件名
format: "es"
},
plugins: [dts()],
}
];
function updatePkgVersion() {
return {
name: 'update-package-version',
buildEnd() {
const pkgStr = fs.readFileSync("./package.json", "utf-8");
const version = pkgStr ? JSON.parse(pkgStr).version : '';
const distPkgStr = fs.readFileSync("./dist/package.json", "utf-8");
const distPkg = distPkgStr ? JSON.parse(distPkgStr) : {};
distPkg.version = version;
fs.writeFileSync('./dist/package.json', JSON.stringify(distPkg, null, 4));
}
}
}