forked from antvis/X6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
131 lines (117 loc) · 3.55 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import fs from 'fs'
import path from 'path'
import _ from 'lodash'
import colors from 'colors/safe.js'
import terser from '@rollup/plugin-terser'
import replace from '@rollup/plugin-replace'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import filesize from 'rollup-plugin-filesize'
import typescript from '@rollup/plugin-typescript'
function formatName(name) {
const realName = name
.replace(/^@/, '')
.replace(/^antv\//, '')
.replace(/\//, '-')
// PascalCase
return _.startCase(_.camelCase(realName)).replace(/ /g, '')
}
function makeOutput() {
const cwd = process.cwd()
const pkg = JSON.parse(
fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'),
)
const peerDependencies = pkg.peerDependencies
const output = { name: formatName(pkg.name) }
if (peerDependencies) {
const globals = {}
Object.keys(peerDependencies).forEach((mod) => {
globals[mod] = formatName(mod)
})
output.globals = globals
}
return output
}
export function config(config = {}) {
let { plugins = [], output, external = [], ...others } = config
if (output == null) {
output = makeOutput()
}
const arr = Array.isArray(output) ? output : [output]
const outputs = []
arr.forEach((item) => {
outputs.push({
format: 'umd',
file: 'dist/index.js',
sourcemap: true,
...item,
})
// extra external modules
if (item.globals) {
Object.keys(item.globals).forEach((key) => {
if (!external.includes(key)) {
external.push(key)
}
})
}
})
return {
input: './src/index.ts',
output: outputs,
plugins: [
typescript({ declaration: false }),
resolve(),
commonjs(),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
}),
terser({ sourceMap: true }),
filesize({
reporter: [
async (options, bundle, result) => {
return import('boxen').then((mod) => {
const boxen = mod.default
const primaryColor = options.theme === 'dark' ? 'green' : 'black'
const secondaryColor =
options.theme === 'dark' ? 'yellow' : 'blue'
const title = colors[primaryColor].bold
const value = colors[secondaryColor]
const lines = [
`${title('Bundle Format:')} ${value(bundle.format)}`,
`${title('Bundle Name:')} ${value(bundle.name)}`,
]
const globals = bundle.globals
const mods = Object.keys(globals)
if (mods.length) {
lines.push(title('External Globals:'))
mods.forEach((mod) => {
lines.push(value(` ${mod}: ${globals[mod]}`))
})
lines.push('')
}
lines.push(
[
`${title('Destination:')} ${value(bundle.file)}`,
`${title('Bundle Size:')} ${value(result.bundleSize)}`,
`${title('Minified Size:')} ${value(result.minSize)}`,
`${title('Gzipped Size:')} ${value(result.gzipSize)}`,
].join('\n'),
)
return boxen(lines.join('\n'), {
padding: 1,
dimBorder: true,
borderStyle: 'classic',
})
})
},
],
}),
...plugins,
],
external,
...others,
}
}
const defaultConfig = config()
export default defaultConfig