forked from didi/LogicFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
163 lines (147 loc) · 4.48 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* eslint-env node */
import fs from 'node:fs'
import path from 'node:path'
import { startCase, camelCase } from 'lodash'
import colors from 'colors/safe'
import babel from '@rollup/plugin-babel'
import alias from '@rollup/plugin-alias'
import terser from '@rollup/plugin-terser'
import replace from '@rollup/plugin-replace'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import fileSize from 'rollup-plugin-filesize'
import nodePolyfills from 'rollup-plugin-polyfill-node'
import { visualizer } from 'rollup-plugin-visualizer'
export function formatName(name) {
const realName = name
.replace(/^@/, '')
.replace(/^logicflow\//, '')
.replace(/\//, '-')
// PascalCase
return startCase(camelCase(realName)).replace(/ /g, '')
}
export function makeOutput() {
const cwd = process.cwd()
const pkg = JSON.parse(
fs.readFileSync(path.join(cwd, 'package.json'), 'utf-8'),
)
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 rollupConfig(config = {}) {
let { output } = config
const { plugins = [], 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.min.js',
sourcemap: true,
exports: 'named',
...item,
})
// extra external modules
if (item && item.globals) {
Object.keys(item.globals).forEach((key) => {
if (!external.includes(key)) {
external.push(key)
}
})
}
})
return {
input: './src/index.ts',
output: outputs,
plugins: [
babel({ babelHelpers: 'bundled' }),
resolve(),
commonjs(),
typescript({
declaration: false,
sourceMap: true,
inlineSources: true,
}),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
}),
alias({
entries: [
{ find: 'react', replacement: 'preact/compact' },
{ find: 'react-dom/test-utils', replacement: 'preact/test-utils' },
{ find: 'react-dom', replacement: 'preact/compact' },
{ find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' },
],
}),
nodePolyfills(),
terser({
format: {
comments: false,
},
compress: {
drop_console: true,
},
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,
visualizer(),
],
external,
...others,
}
}
const defaultConfig = rollupConfig()
export default defaultConfig