-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
254 lines (238 loc) · 5.46 KB
/
rollup.config.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { ExternalOption, RollupOptions } from 'rollup'
import babel from '@rollup/plugin-babel'
import { terser } from 'rollup-plugin-terser'
// @ts-ignore
import size from 'rollup-plugin-size'
import visualizer from 'rollup-plugin-visualizer'
import replace from '@rollup/plugin-replace'
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import path from 'path'
// import svelte from 'rollup-plugin-svelte'
import dts from 'rollup-plugin-dts'
//
import { packages } from './scripts/config'
import { readJsonSync } from 'fs-extra'
import { Package } from './scripts/types'
type Options = {
input: string
packageDir: string
umdExternal: RollupOptions['external']
external: RollupOptions['external'] | any[]
banner: string
jsName: string
globals: Record<string, string>
}
const umdDevPlugin = (type: 'development' | 'production') =>
replace({
'process.env.NODE_ENV': `"${type}"`,
delimiters: ['', ''],
preventAssignment: true,
})
const babelPlugin = babel({
babelHelpers: 'bundled',
exclude: /node_modules/,
extensions: ['.ts', '.tsx'],
})
export default function rollup(options: RollupOptions): RollupOptions[] {
return packages.flatMap((pkg: Package) => {
return pkg.builds.flatMap((build) =>
buildConfigs({
name: [pkg.name, build.entryFile].join('/'),
packageDir: `packages/${pkg.packageDir}`,
jsName: build.jsName,
outputFile: pkg.packageDir,
entryFile: build.entryFile,
globals: build.globals ?? {},
esm: build.esm ?? true,
cjs: build.cjs ?? true,
umd: build.umd ?? true,
externals: build.externals || [],
}),
)
})
}
function buildConfigs(opts: {
esm: boolean
cjs: boolean
umd: boolean
packageDir: string
name: string
jsName: string
outputFile: string
entryFile: string
globals: Record<string, string>
externals: string[]
}): RollupOptions[] {
const input = path.resolve(opts.packageDir, opts.entryFile)
const packageJson =
readJsonSync(
path.resolve(process.cwd(), opts.packageDir, 'package.json'),
) ?? {}
const banner = createBanner(opts.name)
const options: Options = {
input,
jsName: opts.jsName,
packageDir: opts.packageDir,
external: [
...Object.keys(packageJson.dependencies ?? {}),
...Object.keys(packageJson.peerDependencies ?? {}),
...opts.externals,
],
umdExternal: Object.keys(packageJson.peerDependencies ?? {}),
banner,
globals: opts.globals,
}
return [
opts.esm ? esm(options) : null,
opts.cjs ? cjs(options) : null,
opts.umd ? umdDev(options) : null,
opts.umd ? umdProd(options) : null,
types(options),
].filter(Boolean) as any
}
function esm({ input, packageDir, external, banner }: Options): RollupOptions {
return {
// ESM
external,
input,
output: {
format: 'esm',
sourcemap: true,
dir: `${packageDir}/build/esm`,
banner,
},
plugins: [
// svelte({
// compilerOptions: {
// hydratable: true,
// },
// }),
babelPlugin,
nodeResolve({ extensions: ['.ts', '.tsx'] }),
],
}
}
function cjs({ input, external, packageDir, banner }: Options): RollupOptions {
return {
// CJS
external,
input,
output: {
format: 'cjs',
sourcemap: true,
dir: `${packageDir}/build/cjs`,
preserveModules: true,
exports: 'named',
banner,
},
plugins: [
// svelte(),
babelPlugin,
commonjs(),
nodeResolve({ extensions: ['.ts', '.tsx'] }),
],
}
}
function umdDev({
input,
umdExternal,
packageDir,
globals,
banner,
jsName,
}: Options): RollupOptions {
return {
// UMD (Dev)
external: umdExternal,
input,
output: {
format: 'umd',
sourcemap: true,
file: `${packageDir}/build/umd/index.development.js`,
name: jsName,
globals,
banner,
},
plugins: [
// svelte(),
babelPlugin,
commonjs(),
nodeResolve({ extensions: ['.ts', '.tsx'] }),
umdDevPlugin('development'),
],
}
}
function umdProd({
input,
umdExternal,
packageDir,
globals,
banner,
jsName,
}: Options): RollupOptions {
return {
// UMD (Prod)
external: umdExternal,
input,
output: {
format: 'umd',
sourcemap: true,
file: `${packageDir}/build/umd/index.production.js`,
name: jsName,
globals,
banner,
},
plugins: [
// svelte(),
babelPlugin,
commonjs(),
nodeResolve({ extensions: ['.ts', '.tsx'] }),
umdDevPlugin('production'),
terser(),
size({}),
visualizer({
filename: `${packageDir}/build/stats-html.html`,
gzipSize: true,
}),
visualizer({
filename: `${packageDir}/build/stats-react.json`,
template: 'raw-data',
gzipSize: true,
}),
],
}
}
function types({
jsName,
input,
packageDir,
external,
banner,
}: Options): RollupOptions {
return {
// TYPES
external,
input,
output: {
format: 'es',
file: `${packageDir}/build/types/${
path.basename(input).split('.')[0]
}.d.ts`,
banner,
},
plugins: [dts()],
}
}
function createBanner(libraryName: string) {
return `/**
* ${libraryName}
*
* Copyright (c) ChezPaul
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/`
}