-
Notifications
You must be signed in to change notification settings - Fork 11
/
rollup.config.js
107 lines (98 loc) · 2.44 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import path from 'path'
import ts from 'rollup-plugin-typescript2'
import dts from 'rollup-plugin-dts'
const resolve = (p) => path.resolve(__dirname, p)
let formats = process.env.FORMATS
const needBuildTypes = !!process.env.BUILDTYPES
const pkgJson = require(resolve('./package.json'))
const outputConfigMap = {
esm: {
file: resolve(`dist/vue.reuse.esm.js`),
format: 'es',
},
cjs: {
file: resolve(`dist/vue.reuse.cjs.js`),
format: 'cjs',
},
umd: {
file: resolve(`dist/vue.reuse.js`),
format: 'umd',
name: 'VueReuse'
},
}
let RollupConfigs = []
formats = formats ? formats.split('-') : Object.keys(outputConfigMap)
formats.forEach(format => {
RollupConfigs.push(createConfig(format, outputConfigMap[format]))
})
needBuildTypes &&
RollupConfigs.push({
input: resolve(`dist/src/index.d.ts`),
output: {
file: resolve(`dist/vue.reuse.d.ts`),
format: 'es',
},
plugins: [dts()],
})
export default RollupConfigs
function createConfig(format, output) {
if (format === 'umd') {
output.sourcemap = !!process.env.SOURCEMAP
}
// 打包ts
const tsPlugin = ts({
check: process.env.NODE_ENV === 'production',
tsconfigDefaults: resolve('tsconfig.json'),
cacheRoot: resolve('./node_modules/.ts2_cache'),
tsconfigOverride: {
compilerOptions: {
target: 'es5',
sourceMap: !!process.env.SOURCEMAP,
removeComments: true,
declaration: needBuildTypes,
declarationMap: needBuildTypes,
},
include: [`src/`],
exclude: ['__test__/**'],
},
})
// external
const external = [
...Object.keys(pkgJson.peerDependencies || {}),
...Object.keys(pkgJson.dependencies || {}),
...(pkgJson.external || [])
]
// node plugins
const nodePlugins =
format === 'cjs'
? []
: [
require('@rollup/plugin-node-resolve').nodeResolve({
referBuiltins: true,
}),
require('@rollup/plugin-commonjs')({
sourceMap: true,
}),
]
// prod plugins
const prodPlugins =
process.env.NODE_ENV === 'production'
? [
require('rollup-plugin-terser').terser({
module: format === 'esm',
compress: {
ecma: 2015,
},
})
]
: []
return {
input: resolve('src/index.ts'),
output,
plugins: [tsPlugin, ...nodePlugins, ...prodPlugins],
external,
treeshake: {
moduleSideEffects: false,
},
}
}