-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
rollup.config.ts
244 lines (229 loc) · 5.15 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
import { 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'
type Options = {
input: string
packageDir: string
umdExternal: RollupOptions['external']
external: RollupOptions['external']
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) => {
return buildConfigs({
name: pkg.packageDir,
packageDir: `packages/${pkg.packageDir}`,
jsName: pkg.jsName,
outputFile: pkg.packageDir,
entryFile: pkg.entryFile,
globals: pkg.globals ?? {},
esm: pkg.esm ?? true,
cjs: pkg.cjs ?? true,
umd: pkg.umd ?? true,
})
})
}
function buildConfigs(opts: {
esm: boolean
cjs: boolean
umd: boolean
packageDir: string
name: string
jsName: string
outputFile: string
entryFile: string
globals: Record<string, 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 ?? {}),
],
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({
input,
packageDir,
external,
banner,
}: Options): RollupOptions {
return {
// TYPES
external,
input,
output: {
format: 'es',
file: `${packageDir}/build/types/index.d.ts`,
banner,
},
plugins: [dts()],
}
}
function createBanner(libraryName: string) {
return `/**
* ${libraryName}
*
* Copyright (c) TanStack
*
* 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
*/`
}